题解 白书上 总和一定的 dp 题;经典题
1 #include<iostream> 2 #include<stdio.h> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 7 int dp[105][105],M,sum[105],arr[105]; 8 int main( ) 9 { 10 while( scanf("%d",&M) && M ) 11 { 12 memset( dp,0,sizeof(dp) ); 13 memset( sum,0,sizeof(sum) ); 14 for( int i = 1; i <= M; i++ ) 15 { 16 scanf("%d",&arr[i]); 17 sum[i] = sum[i-1]+arr[i]; 18 } 19 for( int i = 0; i <= M; i++ )dp[i][i] = arr[i]; 20 for( int i = 1; i < M; i++ ) 21 for( int j = 1; j+i <= M; j++ ) 22 { 23 int l = i+j; int ans = sum[l] - sum[j-1]; 24 int Min = 0; 25 for( int k = j; k <= l; k++ ) 26 { 27 Min = min( Min,dp[j][k] ); 28 Min = min( Min,dp[k][l] ); 29 } 30 dp[j][l] = ans - Min; 31 } 32 cout<<dp[1][M]*2-sum[M]<<endl; 33 } 34 return 0; 35 }