#include<bits/stdc++.h>
using namespace std;
int a[305], s[305];
int f[305][305];
int main(){
int n;
cin>>n;
for(int i = 1; i <= n; i++) cin>>a[i];
for(int i = 1; i <= n; i++) s[i] = s[i-1] + a[i];
for(int len = 2; len <= n; len++){
for(int i = 1, j; (j = i + len - 1) <= n; i++){
f[i][j] = 1e8;
for(int k = i; k < j; k++){
f[i][j] = min(f[i][j], f[i][k] + f[k+1][j] + s[j] - s[i-1]);
}
}
}
cout<<f[1][n]<<endl;
return 0;
}