简单区间DP。
#include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; const int maxn = 300; int n; int a[maxn], sum[maxn], dp[maxn][maxn]; int main() { while (~scanf("%d", &n)) { sum[0] = 0; for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + a[i]; memset(dp, 0, sizeof dp); for (int i = 2; i <= n; i++) { for (int j = 1; j+i-1 <= n; j++) { int st = j, en = j + i - 1; int ans = 0x7FFFFFFF; for (int k = st; k <= en; k++) ans = min(dp[st][k - 1] + dp[k + 1][en] + sum[en] - sum[st - 1] - a[k],ans); dp[st][en] = ans; } } printf("%d ", dp[1][n]); } return 0; }