20求调
查看原帖
20求调
1053023
MoLing_111楼主2024/11/10 17:40
#include<bits/stdc++.h>
using namespace std;

long long n,p,ans=-9e18;
long long a[1000005],x[1000005],y[1000005],z[1000005];

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> p;
    for(int i=1;i<=n;i++){
        cin >> a[i];
    }
    for(int i=1;i<=n;i++){
        x[i]=max(x[i-1]+a[i],a[i]);
    }
    for(int i=1;i<=n;i++){
        if(i==1) y[i]=x[i];
        else y[i]=max(y[i-1],x[i]);
    }
    for(int i=1;i<=n;i++){
        if(i==1) z[1]=y[1];
        else if(i==2) z[i]=z[i-1]+y[i-1];
        else z[i]=max(z[i-1]%p,z[i-1]%p+y[i-1]);
        ans=max(ans,z[i]);
    }
    cout << ans%p;
    return 0;
}
2024/11/10 17:40
加载中...