目录
1.题目描述
Contiguous Repainting
时间限制: 2.000 Sec 内存限制: 256 MB
题目描述
There are N squares aligned in a row. The i-th square from the left contains an integer ai.
Initially, all the squares are white. Snuke will perform the following operation some number of times:
Select K consecutive squares. Then, paint all of them white, or paint all of them black. Here, the colors of the squares are overwritten.
After Snuke finishes performing the operation, the score will be calculated as the sum of the integers contained in the black squares. Find the maximum possible score.
Constraints
1≤N≤105
1≤K≤N
ai is an integer.
|ai|≤109
输入
The input is given from Standard Input in the following format:
N K
a1 a2 … aN
输出
Print the maximum possible score.
样例输入 Copy
5 3
-10 10 -10 10 -10
样例输出 Copy
10
提示
Paint the following squares black: the second, third and fourth squares from the left.
标签
2.AC
#include <iostream>
#include <cstdio>
using namespace std;
int n, k;
long long a[100005], sum[100005], sump[100005], ans;
int main () {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) {
scanf ("%lld", &a[i]);
sum[i] = sum[i-1] + a[i];
sump[i] = sump[i-1] + a[i]*(a[i]>0);
}
for (int i = k; i <= n; i++) {
long long temp = sum[i] - sum[i-k];
temp = sump[n] - sump[i] + sump[i-k] + temp*(temp>0);
ans = max (ans, temp);
}
cout<<ans;
return 0;
}