一个正向取前缀和,一个反向取,最后枚举断点。
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int n,sum;
int a[200005];
int front[200005];
int back[200005];
int main(){
ios::sync_with_stdio(false);
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
front[1]=a[1];
for(int i=2;i<=n;i++){
front[i]=max(front[i-1],0)+a[i];
}
for(int i=2;i<=n;i++){
front[i]=max(front[i-1],front[i]);
cout<<front[i]<<" ";
}
cout<<"\n";
back[n]=a[n];
for(int i=n-1;i>=1;i--){
back[i]=max(back[i+1],0)+a[i];
}
for(int i=n-1;i>=1;i--){
back[i]=max(back[i+1],back[i]);
cout<<back[i]<<" ";
}
int ans=-1e9;
for(int i=2;i<n;i++){
ans=max(ans,front[i-1]+back[i+1]);
}
cout<<ans;
return 0;
}
标签:200005,int,max,back,long,双子,front,最大
From: https://www.cnblogs.com/sadlin/p/18426851