代码
#include <iostream>
using namespace std;
const int N = 3e2 + 10;
int n;
int s[N], dp[N][N];
int main()
{
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> s[i], s[i] += s[i - 1];
}
for (int len = 2; len <= n; len++) {
for (int i = 1; i + len - 1 <= n; i++) { //从1开始枚举左端点, i+len-1(右端点需要<=n
int l = i, r = i + len - 1;
dp[l][r] = 0x3f3f3f3f;
for (int k = l; k < r; k++) { // 枚举k从 1~r-1, 因为右边至少要有一个
dp[l][r] = min(dp[l][r], dp[l][k] + dp[k + 1][r] + s[r] - s[l - 1]);
}
}
}
cout << dp[1][n] << endl;
return 0;
}