图1.
图2.
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 310;
int s[N];
int f[N][N];
int main () {
int n;
cin >> n;
for (int i = 1; i <= n; i ++) cin >> s[i]; // 为了使用前缀和
for (int i = 1; i <= n; i ++) s[i] += s[i - 1];
for (int len = 2; len <= n; len ++) //区间DP的枚举套路/顺序 1.先枚举区间长度 2.再枚举区间左端点(起点)
for (int i = 1; i + len - 1 <= n; i ++) {
int l = i, r = i + len - 1; //有了起点i和当前区间长度len就可以算出 左、右端点
f[l][r] = 1e8; //易错 ! ! !
for (int k = l; k < r; k ++) //枚举分界点
f[l][r] = min(f[l][r], f[l][k] + f[k + 1][r] + s[r] - s[l - 1]);
}
cout << f[1][n] << endl;
return 0;
}