// Original C++ solution (slower):
//Author:- S_Aditya

//Date created:- 24 February 2019



#include<bits/stdc++.h>

//#include <ext/pb_ds/assoc_container.hpp>

//#include <ext/pb_ds/tree_policy.hpp>

//using namespace __gnu_pbds;

using namespace std;

//typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

#define ll long long 

#define scn(n) scanf("%d",&n)

#define lscn(n) scanf("%lld",&n)

#define lpri(n) printf("%lld",n)

#define pri(n) printf("%d",n)

#define pln() printf("\n")

#define priln(n) printf("%d\n",n)

#define lpriln(n) printf("%lld\n",n)

#define rep(i,init,n) for(int i=init;i<n;i++)

#define pb push_back     

#define mp make_pair

#define F first

#define S second

#define gcd __gcd

#define inf INT_MAX

#define ninf INT_MIN

const ll mod=1e9+7;         

const int N=4e2+4;



ll dp[N][N],a[N];



ll rec(int i,int j)

{

    if(i==j)

    return 0;

    

    ll &ans=dp[i][j];

    

    if(ans!=-1)

    return ans;

    

    ans=1e18;

    

    ll sum=0;

    

    for(int k=i;k<=j;k++)

    sum+=a[k];

    

    for(int k=i;k<j;k++)

    {

        //partitioning the segment [i,j] as [i,k] and [k+1,j] and take the contribution

        //answer of dp[i,j] will be dp[i,k]+dp[k+1,j]+sum(i,j)

        //(visualise this process as a building up of a binary tree)

        ll here=rec(i,k)+rec(k+1,j)+sum;

        

        ans=min(ans,here);

    }

    

    return ans;

}



int main()                  

{

    int n;

    

    scn(n);

    

    rep(i,0,n)

    lscn(a[i]);

    

    memset(dp,-1,sizeof(dp));

    

    ll ans=rec(0,n-1);

    

    cout<<ans;

} 


// Optimized solution to the same problem (faster):
//Author:- S_Aditya

//Date created:- 24 February 2019



#include<bits/stdc++.h>

//#include <ext/pb_ds/assoc_container.hpp>

//#include <ext/pb_ds/tree_policy.hpp>

//using namespace __gnu_pbds;

using namespace std;

//typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

#define ll long long 

#define scn(n) scanf("%d",&n)

#define lscn(n) scanf("%lld",&n)

#define lpri(n) printf("%lld",n)

#define pri(n) printf("%d",n)

#define pln() printf("\n")

#define priln(n) printf("%d\n",n)

#define lpriln(n) printf("%lld\n",n)

#define rep(i,init,n) for(int i=init;i<n;i++)

#define pb push_back     

#define mp make_pair

#define F first

#define S second

#define gcd __gcd

#define inf INT_MAX

#define ninf INT_MIN

const ll mod=1e9+7;         

const int N=4e2+4;



ll dp[N][N],a[N];



int main()                  

{

    int n;

    

    scn(n);

    

    rep(i,0,n)

    lscn(a[i]);

    

    //dp[i][j] tells the optimum merging cost for the segment [i,j].

    //dp[i][i]=0, no cost of merging

    

    for(int len=2;len<=n;len++)

    {

        for(int i=0;i+len-1<n;i++)

        {

            int j=i+len-1;

            

            dp[i][j]=1e18;

            

            ll sum=0;

            

            for(int k=i;k<=j;k++)

            sum+=a[k];

            

            for(int k=i;k<j;k++)

            {

                dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]+sum);

            }

        }

    }

    

    ll ans=dp[0][n-1];

    

    printf("%lld",ans);

} 


// Fastest known solution for this problem: