#include<bits/stdc++.h>
using namespace std;
const int N = 310;
int n;
int dp[N][N],s[N];
//dp[i][j]代表区间[i,j]的最小代价
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++){
int l = i,r = i+len-1;
//这里记得初始化
dp[l][r]=1e9;
for(int j = l;j<r;j++)
dp[l][r]=min(dp[l][r],dp[l][j]+dp[j+1][r]+s[r]-s[l-1]);
}
}
cout<<dp[1][n];
return 0;
}