题目传送门
1 /*
2 题意:取长度不小于m的序列使得和最大
3 贪心:先来一个前缀和,只要长度不小于m,从m开始,更新起点k最小值和ans最大值
4 */
5 #include <cstdio>
6 #include <algorithm>
7 using namespace std;
8
9 const int MAXN = 1e6 + 10;
10 const int INF = 0x3f3f3f3f;
11 int a[MAXN], sum[MAXN];
12
13 int main(void) //FZU 2013 A short problem
14 {
15 // freopen ("C.in", "r", stdin);
16
17 int t; scanf ("%d", &t);
18 while (t--)
19 {
20 int n, m; scanf ("%d%d", &n, &m);
21 for (int i=1; i<=n; ++i)
22 {
23 scanf ("%d", &a[i]); sum[i] = sum[i-1] + a[i];
24 }
25
26 int k = 0; int ans = -INF;
27 for (int i=m; i<=n; ++i)
28 {
29 ans = max (ans, sum[i] - k);
30 k = min (k, sum[i-m]);
31 }
32
33 printf ("%d
", ans);
34 }
35
36 return 0;
37 }