51nod 1254 最大子段和 V2
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int n;
ll a[50005];
ll sum[50005];
ll lmax[50005],rmax[50005];
int main(){ios::sync_with_stdio(false);cin>>n;for(int i=1;i<=n;i++){cin>>a[i];sum[i]=sum[i-1]+a[i];}for(int i=0;i<n;i++){lmax[i+1]=max(lmax[i],a[i+1]);rmax[n-i]=max(rmax[n-i+1],a[n-i]);}int maxi=n;ll ans=0,ssum=0;for(int i=n;i>=1;i--){if(sum[i]>=sum[maxi]){maxi=i;ssum=sum[i]-a[i];}ssum=max(ssum,sum[maxi]-a[i]);ans=max(ans,ssum-sum[i-1]+max(lmax[i-1],rmax[maxi+1])); }cout<<ans;return 0;
}