本题维护单调栈采用deque
# include <bits/stdc++.h>
using namespace std;
#define int long long
signed main()
{
int n,m;
cin>>n>>m;
vector<int>a(n+1),s(n+1);
for(int i=1;i<=n;i++)
cin>>a[i],s[i]=s[i-1]+a[i];
deque<int>q;
q.push_back(0);//插入编号0用于更新往后的值防止最大值从一开始的情况
int res=-1e18;
for(int i=1;i<=n;i++)
{
if(!q.empty()&&i-m>q.front())
q.pop_front();
res=max(res,s[i]-s[q.front()]);//序列小于等于m时更新最大值
while(!q.empty()&&s[i]<s[q.back()]+1)//保证序列严格单调递增
q.pop_back();
q.push_back(i);
}
cout<<res<<endl;
}