dp
状态转移方程:d(i,j)=sum(i,j)-min{d(i+1,j),d(i+2,j)...d(j,j),d(i,j-1),d(i,j-2)...d(i,i),0}
可化为d(i,j)=sum(i,j)-min{f(i+1,j),g(i,j-1),0}
#include <iostream> #include <cstring> #include <cstdio> using namespace std; const int maxn=100+10; int arr[maxn]; int f[maxn][maxn],d[maxn][maxn],s[maxn],g[maxn][maxn]; int n; int min(int a,int b) { return a<b?a:b; } int main() { while(scanf("%d",&n)&&n) { int i,j,k; s[0]=0; for(i=1;i<=n;i++) { scanf("%d",&arr[i]); s[i]=s[i-1]+arr[i]; } for(i=1;i<=n;i++) f[i][i]=g[i][i]=d[i][i]=arr[i]; for(i=2;i<=n;i++) { for(j=1;j+i-1<=n;j++) { k=j+i-1; int tem=min(f[j+1][k],g[j][k-1]); d[j][k]=s[k]-s[j-1]-min(tem,0); f[j][k]=min(d[j][k],f[j+1][k]); g[j][k]=min(d[j][k],g[j][k-1]); } } printf("%d\n",2*d[1][n]-s[n]); } return 0; }