先预处理出前缀和,然后就相当于找i-m到i的最小值,所以用单调队列维护。
#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<set>
#include<map>
#include<bitset>
#include<unordered_map>
using namespace std;
#define LL long long
#define eps (1e-9)
typedef unsigned long long ull;
const int maxn = 3e5 + 10;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const int bas = 131;
const LL mod = 1e6 + 3;
LL a[maxn];
LL sum[maxn], q[maxn];
int main()
{
int n, m;
scanf("%d%d", &n, &m);
LL ans;
for (int i = 1; i <= n; i++)
{
scanf("%lld", &a[i]);
sum[i] = sum[i - 1] + a[i];
if (i == 1)ans = a[1];
else ans = max(ans, a[i]);
}
int hh = 0, tt = 0;
for (int i = 1; i <= n; i++)
{
if(i - q[hh] > m)hh++;
ans = max(ans, sum[i] - sum[q[hh]]);
while (hh <= tt && sum[q[tt]] >= sum[i])tt--;
q[++tt] = i;
}
printf("%lld", ans);
return 0;
}