思路
前缀和。不用同时求出最合适的两个和,甚至随着数组的刷新,并不是最大的组合组成最后的结果。参考数据:-100 11 22 22 11 -100
代码
//数学考试
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
ll a[2000005];
int main() {
int _;
cin >> _;
while (_--) {
int n, k;
cin >> n >> k;
for (int i = 1; i <= n; i++)
cin >> a[i], a[i] += a[i - 1];
ll ma = -1e18, ans = -1e18;
for (int i = k; i + k <= n; i++) {
ma = max(ma, a[i] - a[i - k]);
ans = max(ans, ma + a[i + k] - a[i]);
}
cout << ans << endl;
}
return 0;
}