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.
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.
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 0Sample Output
16
题解:简单的一个尺取可过。
代码如下:#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define maxn 10001
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define lowbit(x) (x&(-x))
#define eps 0.00000001
int a[100011],b[100011];
int main()
{
int n,k,sum=0;
cin>>n>>k;
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(int i=0;i<n;i++)
{
scanf("%d",&b[i]);
if(b[i]==1)
sum+=a[i];
}
int s=0;
for(int j=0;j<k;j++)
if(b[j]==0)
s+=a[j];
int max1=s;
for(int i=0;i<n-k+1;i++)
{
if(b[i]==0&&b[i+k]==0)//查找一个k连续和的最大值,标记为1的不记入
s=s+a[i+k]-a[i];
else if(b[i]==1&&b[i+k]==0)
s=s+a[i+k];
else if(b[i]==0&&b[i+k]==1)
s=s-a[i];
if(s>max1)//找到其中最大值
max1=s;
}
printf("%d\n",sum+max1);
return 0;
}