int f[1003][1003]; int a[1003]; int sum[1003]; inlineintg(int i, int j){ return sum[j] - sum[i - 1]; } intmain(){ int n; while(cin >> n){ memset(f, INF, sizeof f); for (int i = 0; i < n; i++){ cin >> a[i]; f[i][i] = 0; if (i == 0) sum[i] = a[i]; else sum[i] = sum[i - 1] + a[i]; } for (int i = n; i < 2 * n; ++i){ f[i][i] = 0; sum[i] = sum[i - 1] + a[i % n]; }
for (int len = 1; len < 2 * n; len++){ //len代表区间的长度 for (int i = 0; i < 2 * n; ++i){ //i代表区间的左端点 int j = i + len; // j 代表曲线右端点 if (j >= 2 * n) continue; for (int k = i; k <= j; ++k) f[i][j] = min(f[i][j], f[i][k] + f[k + 1][j] + g(i, k) + g(k + 1, j)); } } int ans = INF; for (int i = 0; i < n; ++i) ans = min(ans, f[i][i + n - 1]); cout << ans << "\n"; } return0; }