f(i, j) = min{ f(i,k-1)+f(k+1,j)+sum(i, j) - wk | i<=k<=j}
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N = 255; const int INF = 0x3f3f3f3f; int e[N]; int sum[N][N] ,dp[N][N]; int n; void getSum() { memset(sum,0,sizeof(sum)); for(int i = 1; i <= n; i++) { sum[i][i] = e[i]; } for(int i = 1; i <= n; i++) { for(int j = i+1; j <= n; j++) { sum[i][j] = sum[i][j-1] + e[j]; } } } int main() { while(scanf("%d",&n) != EOF) { for(int i = 1; i <= n; i++) { scanf("%d",&e[i]); } getSum(); memset(dp,0,sizeof(dp)); for (int d = 2; d <= n; ++d) { for (int l = 1; l + d - 1 <= n; ++l) { int r = l + d - 1; int ans = INF; for (int k = l; k <= r; ++k) ans = min(ans, dp[l][k-1] + dp[k+1][r] + sum[l][r] - e[k]); dp[l][r] = ans; } } printf("%d\n",dp[1][n]); } return 0; }