1 solutions

  • 1
    @ 2025-11-1 15:13:26
    #include<bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    const int N=4e5+10;
    LL w[N],s[N];
    int q[N];
    int main()
    {
        int n;
        cin>>n;
        for(int i=1;i<=n;i++)
        {
            cin>>w[i];
            w[i+n]=w[i];
        }
        for(int i=1;i<=2*n;i++)
        {
            s[i]=s[i-1]+w[i];
        }
        LL ans=-1e18;
        int hh=0,tt=-1; //队头,队尾
        for(int i=1;i<=2*n;i++)
        {
            while(hh<=tt&&i-q[hh]+1>n+1) hh++; //当前区间元素过多
            ans=max(ans,s[i]-s[q[hh]]);//更新区间最值
            while(hh<=tt&&s[i]<s[q[tt]]) tt--; //去掉较大值
            q[++tt]=i;
        }
        cout<<ans;
        return 0;
    }
    
    • 1

    Information

    ID
    2570
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    10
    Tags
    # Submissions
    5
    Accepted
    1
    Uploaded By