1 solutions

  • 0
    @ 2025-11-5 20:05:16

    C++ :

    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    typedef __int128 LL;
    const int N=1100000;
    int n, p;
    LL a[N];
    LL b[N],c[N];
    const int SIZE=1<<25;
    char Buf[SIZE],*buf=Buf;
    #define getint(x) (x)=strtoll(buf,&buf,10)
    int main()
    {
        fread(Buf,SIZE,1,stdin);
        getint(n);getint(p);
        for (int i=1;i<=n;i++)
            getint(a[i]);
        b[1]=a[1];
        LL now=a[1]>0?a[1]:0;
        for(int i=2;i<=n;i++)
        {
            b[i]=b[i-1];
            now+=a[i];
            b[i]=now>b[i]?now:b[i];
            now=now>0?now:0;
        }
        c[1]=b[1];
        c[2]=c[1]+b[1];
        for(int i=3;i<=n;i++)
            c[i]=c[i-1]+(b[i-1]>0?b[i-1]:0);
        printf("%d\n", (int)((c[n]>c[1]?c[n]:c[1])%p));
        return 0;
    }
    
    • 1

    Information

    ID
    19993
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By