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
Output
16
Note

In the sample case the better way is to use the secret technique at the beginning of the third minute. Then the number of theorems Mishka will be able to write down will be equal to 16.

题意:一个长达n分钟的讲座,老师每分钟会讲ai个知识点,Mishka每分钟会有一个状态,1表示清醒,0表示睡觉,清醒时会把当前老师的所有知识点记下来,你有一个可以让Mishka连续保持k分钟清醒的神奇药水,求Mishka可以记下的最多知识点的数量

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int N = 1e5+10;
LL a[N],t[N],sum[N],per[N];
int main(){
	int n,k;
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++){
		scanf("%lld",&a[i]);
		sum[i]=sum[i-1]+a[i];
	}
	for(int i=1;i<=n;i++){
		scanf("%lld",&t[i]);
		if(t[i]) per[i]=per[i-1]+a[i];
		else per[i]=per[i-1];
	}
	LL maxt=0;
	for(int i=1;i<=n-k+1;i++){
		LL x=per[i-1];
		LL y=sum[i+k-1]-sum[i-1];
		LL z=per[n]-per[i+k-1];
		maxt=max(maxt,x+y+z);
	}
	printf("%lld\n",maxt);
	return 0;
}

阅读更多
版权声明:欢迎评论,本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/islittlehappy/article/details/80346461
个人分类: Codeforces 找规律
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭