Lecture Sleep(思维+前缀和)

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.

题意:就是一堂上为0的时候你是睡着的,你可以使用一个技能,这段时间醒着的,尽量让自己听到更多的知识,醒的时间为k,也就是可以任选一段(i,i+k-1)

思路:枚举任意一段长度为k的区间,枚举的段就是这段的a数组的和,然后我们预处理一下a的前缀和和后缀和,这样每次枚举总的值就是枚举段之前的前缀和+枚举段的和+枚举段之后的后缀和,然后每次比较选取最大的。

                                                                                     

前缀和              枚举段             后缀和

           i-k      i-k+1                  i         i+1

求出总和比计较选最大的

code:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 1e5+5;
typedef long long ll;
ll a[N],f[N],b[N],c[N];
int main(){
    int n,k;
    cin >> n >> k;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
    }
    for(int i = 1; i <= n; i++){
        cin >> f[i];
    }
    for(int i = 1; i <= n; i++){
        if(f[i]) b[i] = b[i-1] + a[i];
        else b[i] = b[i-1];
    }
    for(int i = n; i > 0; i--){
        if(f[i]) c[i] = c[i+1] + a[i];
        else c[i] = c[i+1];
    }
    ll s = 0;//枚举段的和
    for(int i = 1; i <= k; i++){
        s += a[i];
    }
    ll ans = 0;
    for(int i = k; i <= n; i++){
        ans = max(b[i-k]+s+c[i+1],ans);//前一段前缀和+中间枚举段的和+之后的后缀和比较选取最大的
        s = s - a[i-k+1] + a[i+1];//枚举段整体右移一个单位,减去第一个加上该段最后一个的下一个
    }
    cout << ans << endl;
    return 0;
}


猜你喜欢

转载自blog.csdn.net/codeswarrior/article/details/80345734
今日推荐