代码:
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 3e5 + 10;
int n, m, s[N], ans = -1e8;
deque<int> q;
signed main()
{
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
cin >> s[i];
s[i] += s[i - 1];
}
q.push_back(0);
for (int i = 1; i <= n; i++)
{
while (!q.empty() && q.front() < i - m)
q.pop_front();
ans = max(ans, s[i] - s[q.front()]);
while (!q.empty() && s[q.back()] >= s[i])
q.pop_back();
q.push_back(i);
}
cout << ans;
return 0;
}