961B--Lecture Sleep

题目

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();
	}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值