// Slower C++ solution:
#include<bits/stdc++.h>

#define ll long long

#define ull unsigned long long

#define IO ios::sync_with_stdio(false), cin.tie(0)

#define endl '\n'

#define pi acos(-1.0)

#define lowbit(x) ((x)&(-(x)))

#define debug(x) cout<<x<<'\n'

#define pii pair<ll,pair<ll,ll> >

using namespace std;

const ll INF=0x3f3f3f3f3f3f3f3f;

const int inf = 0x3f3f3f3f;

const ll mod = 1e9+7;

const double eps = 1e-6;

ll gcd(ll a, ll b) {return b==0?a:gcd(b, a%b);}

ll lcm(ll a, ll b) {return a/gcd(a, b)*b;}

ll mul(ll a,ll b){ll ans=0;a%=mod,b%=mod;while(b) { if(b&1) ans = (ans+a)%mod; a = (a*2)%mod; b >>= 1; }return ans;}

ll powmod(ll a,ll b){ ll ans = 1, pow = a; while(b) { if(b&1) ans = (ans*pow)%mod; pow = (pow*pow)%mod; b >>= 1; } return ans;}

//(b/a)%p=b*a^(p-2)%p

//int pr(ll num){//判素

//     if(num == 1) return 0;

//     if(num ==2|| num==3 ) return 1 ;

//     if(num %6!= 1&&num %6!= 5) return 0 ;

//     int tmp =sqrt( num);

//     for(int i= 5;i <=tmp; i+=6 )

//          if(num %i== 0||num %(i+ 2)==0 )return 0 ;

//     return 1 ;

//}

int Day(int y,int m, int d){if(m==1||m==2) { m+=12; y--; }int w=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7;return w+1;}

//int pri[10000005];

//void getpri(){

//    memset(pri,true,sizeof(pri));

//    pri[0]=pri[1]=0;

//    for(int i=2;i<=10000000;i++){

//        if(pri[i]){

//            for(int j=2*i;j<=10000000;j+=i) pri[j]=0;

//        }

//    }

//}

//struct ac{

//    int x,y;

//    int fa;

//} pre[10000005];

//void init(){

//    for(int i=1; i<=10000005;++i) pre[i].fa=i;

//}

//int getf(int x){

//    if(pre[x].fa == x) return x;

//    else return pre[x].fa = getf(pre[x].fa);

//}

//void join(int x,int y){

//    int a = getf(x);

//    int b = getf(y);

//    if(a!=b) pre[b].fa=a;

//}

//大小写转换

//String s;

//Form(s.begin(),s.end(),s.begin(),::toupper);

//Form(s.begin(),s.end(),s.begin(),::tolower);

//(char)toupper('a')

//(char)tolower('B')

//#pragma comment(linker, "/STACK:1024000000,1024000000")//外挂开栈

//stable_sort 排序不处理相同元素

//map<int,int>::reverse_iterator rit;

//for(rit=mp.rbegin();rit!=mp.rend();rit++)

//vec动态二维数组

//vector<vector<int>> g(26,vector<int>(100005));

//初始化vec里面套了n个vec

//lower_bound(begin,end,num) 有序数列中返回第一个>=num的数字的地址

//upper_bound( begin,end,num) 返回第一个>num的数字的地址

const int maxn=405;

ll a[maxn],s[maxn];

ll dp[maxn][maxn];

int main(){

    int n;

    cin>>n;

    memset(dp,0x3f,sizeof(dp));

    for(int i=1;i<=n;++i){

        cin>>a[i];

        s[i]=s[i-1]+a[i];

        dp[i][i]=0;

    }

    for(int len=1;len<=n;++len){

        for(int j=1;j+len-1<=n;++j){

            int ends=j+len-1;

            for(int i=j;i<ends;++i){

                dp[j][ends] = min(dp[j][ends],dp[j][i]+dp[i+1][ends]+s[ends]-s[j-1]);

            }

        }

    }

    cout<<dp[1][n]<<endl;

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 2.3137992420429896x):
#include<iostream>

#include<cstring>

#define ll long long

#define pi acos(-1.0)

#define inf 0x3f3f3f3f

using namespace std;

const int maxn = 2e5+5;

ll dp[405][405], a[405], pre[405];

int n;



ll dfs(int l, int r){

	if(l >= r) return a[l];

	if(dp[l][r]) return dp[l][r];

	ll res = 1e17;

	for(int i = l; i < r; i++){

		res = min(res, dfs(l, i) + dfs(i+1, r));

	}

	return dp[l][r] = res + pre[r] - pre[l-1];

}



int main(){

	cin >> n;

	for(int i = 1; i <= n; i++){

		cin >> a[i];

		pre[i] = pre[i-1] + a[i];

	}



	cout << dfs(1, n) - pre[n] << endl;

	return 0;

}