Problem Description
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.
Sample Input
6 3 1 3 5 2 5 4 1 1 0 1 0 0
Sample Output
16
题意:
在长达n分钟的讲座中,每分钟老师会讲ai个知识点,Mishka每分钟有一个状态,1表示清醒,可以将当前的知识点全部记下,0表示睡觉,不能记知识点,你有一个神奇的药水,可以让她连续k分钟保持清醒,求Mishka记下的最大知识点数量
思路:
在所有连续的k区间里面,只需要找到在每个区间睡觉导致丢失的分数的最大值,那么就把这个方法用在此区间,这样就能保证最后所得的分数最大。
代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
int a[maxn];
int t[maxn];
int pre[maxn];
int main()
{
int n,k;
int s=0;
int temp=0;
cin>>n>>k;
for(int i=1;i<=n;i++) cin>>a[i];
for(int i=1;i<=n;i++)
{
cin>>t[i];
if(t[i]) temp+=a[i];
}
memset(pre,0,sizeof(pre));
pre[0]=0;
for(int i=1;i<=n;i++)
{
if(t[i]) pre[i]=pre[i-1]+0;
if(!t[i]) pre[i]=pre[i-1]+a[i];
}
for(int i=1;i<=n;i++)
{
s=max(s,(pre[i+k-1]-pre[i-1]));
}
s+=temp;
cout<<s<<endl;
}