Codeforces——961B. Lecture Sleep

本文是博主原创文章,未经允许不得转载。

我在博客园也同步发布了此文,链接 http://www.cnblogs.com/umbrellalalalala/p/8824209.html

题目来源 http://codeforces.com/problemset/problem/961/B

【题目】

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

【分析】

我们根据样例输入来分析大意:我现在要去听一个6分钟的讲座,根据样例输入的第三行,我在第1、2、4分钟是醒着的,其他时间睡着了。其中有一种清醒手段能让我连续三分钟保持清醒(样例输入第一行第二个),例如在第1、2、3分钟使用这个手段,那么将改变我在第3分钟的状态(本来是0,代表睡着,现在由于使用了手段,于是醒了)。样例输入的第二行代表每分钟讲座会讲多少个数学定理,在醒着的时候我可以将定理全部记下来,然而在睡着的时候我无法记笔记。现在要求计算我最多能记多少笔记(在使用清醒手段的情况下)。

【示例代码】

#include<stdio.h>
#include<stdlib.h>
#define MAX_N 100010
int a[MAX_N], t[MAX_N];

int main() {
	int n, k, i;
	long long sliding_window = 0, sum = 0, temp = 0;
	scanf("%d %d", &n, &k);
	for (int i = 0; i < n; i++) {
		scanf("%d", a + i);
	}
	for (int i = 0; i < n; i++) {
		scanf("%d", t + i);
		if (t[i] == 1)sum += a[i];
	}

	for (int i = 0; i < n; i++) {
		if (!t[i])temp += a[i];
		if (i - k >= 0 && !t[i - k])temp -= a[i - k];
		sliding_window = (temp > sliding_window) ? temp : sliding_window;
		
	}
	printf("%I64d\n", sum + sliding_window);
	system("pause");
	return 0;
}
发布了36 篇原创文章 · 获赞 41 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/umbrellalalalala/article/details/79891969