C++ 代码
#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f3f3f3f
#define rep(i,a,b) for(auto i=a;i<=b;++i)
#define bep(i,a,b) for(auto i=a;i>=b;--i)
#define lowbit(x) x&(-x)
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define PI acos(-1)
#define pb push_back
#define eps 1e-6
const int mod = 1e9 + 7;
const int MOD = 1e4+7;
const int N = 1e6 + 10;
const int M = 1111;
int dx[]={-1, 0, 1, 0};
int dy[]={0, 1, 0, -1};
int dxy[][2]={{0,1},{1,0},{1,1},{-1,1}};
using namespace std;
int dp[M];
int vis[M][M];
void solve(){
int n;
cin>>n;
rep(i,1,n) {
cin>>dp[i];
dp[i]+=dp[i-1];
}
rep(i,1,n){
rep(j,1,n-i+1){
int r=i+j-1;
if(i==1) vis[j][r]=0;
else{
vis[j][r]=INF;
rep(k,j,r-1){
vis[j][r]=min(vis[j][r],vis[j][k]+vis[k+1][r]+dp[r]-dp[j-1]);
}
}
}
}
cout<<vis[1][n]<<endl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}