P1714 切蛋糕

点击查看代码
#include<bits/stdc++.h>
using namespace std;

typedef long long LL;
const int N=5e5+10;
int a[N];
LL s[N];
int q[N];
int n,m;

int main()
{
    ios::sync_with_stdio(0),cin.tie(0);

    cin>>n>>m;

    q[0]=0;
    int hh=0,tt=0;

    for(int i=1;i<=n;i++) cin>>a[i];
    
    for(int i=1;i<=n;i++){
        s[i]=s[i-1]+a[i];
    }

    LL ans=-1e18;
    for(int i=1;i<=n;i++){
        if(tt>=hh&&i-m>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;
}

posted @ 2026-02-06 20:48  AnoSky  阅读(3)  评论(0)    收藏  举报