#include<bits/stdc++.h>
#define fori(x,a,b) for (int x=a;x<=b;x++)
#define ll long long
using namespace std;
ll a[410],f[410][410],sum[410];
ll oo = LONG_MAX;

ll dp(int i,int j)
{
    if (f[i][j] > 0) return f[i][j];
    if (i==j) return 0;
    ll tg = oo;
    fori(k,i,j-1)
    {
        tg = min(dp(i,k) + dp(k+1,j) ,tg);
    }
    f[i][j] = tg + sum[j] - sum[i-1];
    return f[i][j];
}
int main()
{
   int n;
   cin>>n;
   fori(i,1,n)  cin>>a[i];
   fori(i,1,n) sum[i] = sum[i-1] + a[i];
   cout<<dp(1,n);
}
