poj 2479 Maximum sum (最大字段和的变形)
题目链接:http://poj.org/problem?id=2479
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const int maxn = 50005; const int INF = 0x3f3f3f3f; int dp1[maxn]; int dp2[maxn]; int a[maxn]; int N; int main() { int T; cin>>T; while(T--){ scanf("%d",&N); for(int i=1;i<=N;i++) scanf("%d",&a[i]); dp1[0] = -INF; for(int i=1;i<=N;i++){ dp1[i] = max(dp1[i-1],0) + a[i]; } for(int i=2;i<=N;i++) dp1[i] = max(dp1[i],dp1[i-1]); dp2[N+1] = -INF; for(int i=N;i>=1;i--){ dp2[i] = max(dp2[i+1],0) + a[i]; } for(int i=N-1;i>=1;i--){ dp2[i] = max(dp2[i],dp2[i+1]); } int ans = -INF; for(int i=1;i<=N;i++) ans = max(ans,dp1[i]+dp2[i+1]); printf("%d\n",ans); } }