961B--Lecture Sleep

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/z16160102042/article/details/83513660

题目

Your friend Mishka and you attend a calculus lecture. Lecture lasts n minutes. Lecturer tells ai theorems during the i-th minute.

Mishka is really interested in calculus, though it is so hard to stay awake for all the time of lecture. You are given an array t of Mishka’s behavior. If Mishka is asleep during the i-th minute of the lecture then ti will be equal to 0, otherwise it will be equal to 1. When Mishka is awake he writes down all the theorems he is being told — ai during the i-th minute. Otherwise he writes nothing.

You know some secret technique to keep Mishka awake for k minutes straight. However you can use it only once. You can start using it at the beginning of any minute between 1 and n - k + 1. If you use it on some minute i then Mishka will be awake during minutes j such that and will write down all the theorems lecturer tells.

You task is to calculate the maximum number of theorems Mishka will be able to write down if you use your technique only once to wake him up.

Input

The first line of the input contains two integer numbers n and k (1 ≤ k ≤ n ≤ 105) — the duration of the lecture in minutes and the number of minutes you can keep Mishka awake.

The second line of the input contains n integer numbers a1, a2, … an (1 ≤ ai ≤ 104) — the number of theorems lecturer tells during the i-th minute.

The third line of the input contains n integer numbers t1, t2, … tn (0 ≤ ti ≤ 1) — type of Mishka’s behavior at the i-th minute of the lecture.

Output

Print only one integer — the maximum number of theorems Mishka will be able to write down if you use your technique only once to wake him up.

Example

input

6 3
1 3 5 2 5 4
1 1 0 1 0 0

oupt

16

题意:Mishka上课,但是他在上课的过程中会困。在课堂上,如果第 i 时刻保持清醒,那么他将会得到一定的分数,如果这时候在睡觉,就不会得到分数。
现在有一种秘密方法可以让Mishka在连续的k分钟之内都会连续保持清醒,但是这种方法只能用一次,问所能得到的最大分数值是多少。输入的第一行是n,k分别代表这节课有n分钟,使用秘密方法后能保持连续k分钟清醒。 第二行代表每分钟保持清醒听课可以获得的分数值。 第三行只有0和1,1代表清醒,0代表睡觉。

思路:先计算出Mishka清醒时候记下定理的个数,然后将其时刻的a[i];(i=0,1,2,3…)记为0,然后在所有连续的k区间里面,只需要找到在每个区间睡觉导致丢失的分数的最大值,那么就把这个方法用在此区间,这样就能保证最后所得的分数最大。对于区间每一次向后移动到 i 时,如果第 i 时刻睡觉,那么加上 i 时刻的分数值,如果 i-k 时刻睡觉,同时也要减去 i-k时刻的分数值,然后每次获取一下最大值。

AC-Code

import java.util.Scanner;

public class LectureSleep {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int k = sc.nextInt();
		int s1=0,max=0;
		int a[] = new int[n];
		int b[] = new int[n];
		for(int i = 0;i<n;i++)
		{
			a[i] = sc.nextInt();
		}
		for(int i = 0;i<n;i++)
		{
			b[i] = sc.nextInt();
			if(b[i]==0)
			{
				continue;
			}
			s1+=a[i];
			a[i]=0;
		}
		
		for(int i =0;i<k;i++)
		{
			if(b[i]==0)
			{
				max+=a[i];
			}
		}
		int s2 =max;
		for(int i =k;i<n;i++)
		{
			if(b[i]==0)
			{
				s2+=a[i];
			}
			if(b[i-k]==0)
			{
				s2-=a[i-k];
			}
			max = Math.max(max, s2);
		}
		System.out.println(s1+max);
		sc.close();
	}

}

猜你喜欢

转载自blog.csdn.net/z16160102042/article/details/83513660
今日推荐