You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

25 lines
772 B

2 years ago
#include <bits/stdc++.h>
using namespace std;
// Brute-force 暴力
const int N = 300010;
const int INF = 0x3f3f3f3f;
int s[N];
int n, m;
// TLE掉3个点,通过了 11/14个数据
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> s[i], s[i] += s[i - 1];
int res = -INF;
// 遍历每一个终点
for (int i = 1; i <= n; i++)
// 从终点向前找出所有区间在m之内的数字通过前缀和计算出区间的累加和保留最大值
// 在刚刚出发不久长度不够m的时候,即i<m,则i-m会出现负数
for (int j = i; j > max(0, i - m); j--)
res = max(res, s[i] - s[j - 1]); // j>0 && j> i-m --> j> max(0,i-m)
printf("%d\n", res);
return 0;
}