#include <iostream> #include <algorithm> using namespace std; const int N = 310; int n; int s[N];//前缀和 int f[N][N];//状态 int main() { scanf("%d", &n); for (int i = 1; i <= n; i ++ ) scanf("%d", &s[i]); for (int i = 1; i <= n; i ++ ) s[i] += s[i - 1]; //如果区间长度为1,那么合并的代价就为0,所以不用考虑,直接从2开始 for (int len = 2; len <= n; len ++ )//枚举区间长度 for (int i = 1; i + len - 1 <= n; i ++ ) { //枚举区间起点 int l = i, r = i + len - 1;//左右端点 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]); } printf("%d ", f[1][n]); return 0; }