P1714 切蛋糕
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int N=5e5+10;
int n,k;
long long s[N];
int q[N];
int main()
{
ios::sync_with_stdio(0),cin.tie(0);
cin>>n>>k;
for(int i=1;i<=n;i++){
cin>>s[i];
s[i]=s[i]+s[i-1];
}
q[0]=0;
int hh=0,tt=0;
long long ans=-1e9;
for(int i=1;i<=n;i++){
if(tt>=hh&&i-k>q[hh]) hh++;
ans=max(ans,s[i]-s[q[hh]]);
while(tt>=hh&&s[i]<=s[q[tt]]) tt--;
q[++tt]=i;
}
cout<<ans<<endl;
return 0;
}

浙公网安备 33010602011771号