#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1010;
int n;
int a[N],f[N][N];
int main()
{
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
for(int i = 1; i <= n; i++) a[i] += a[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;
f[l][r] = 1000000000;
for(int k = l; k < r; k++){
f[l][r] = min(f[l][r],f[l][k]+f[k+1][r]+a[r]-a[l-1]);
}
}
}
cout << f[1][n];
}