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.

32 lines
1.0 KiB

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
typedef long long LL;
int q[N];
LL s[N], f[N];
int main() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> s[i], s[i] += s[i - 1];
// 由于滑动窗口在i的左边需要讨论前缀和的s[l-1],第1个没有前序不符合整体的代码逻辑添加了一个哨兵
int hh = 0, tt = 0;
for (int i = 1; i <= n; i++) {
//  1、年龄大于m的老家伙们不管是不是实力够强大一概去死
while (hh <= tt && i - q[hh] > m) hh++;
// 因为滑动窗口在i 左侧,先使用再加入
f[i] = max(f[i - 1], f[max(0, q[hh] - 1)] + s[i] - s[q[hh]]);
//  2、不如我年轻并且不如我有实力的老家伙们去死
while (hh <= tt && f[i - 1] - s[i] >= f[max(0, q[tt] - 1)] - s[q[tt]]) tt--;
// 3、i入队列
q[++tt] = i;
}
// 输出结果
cout << f[n] << endl;
return 0;
}