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

#define ll long long

using namespace std;

const ll mod=1e9+7;

ll ps[100009],n,a[10009],dp[409][409];



ll f(ll l, ll r){

    if(l==r)return 0;

    ll tmp=1e17;

    if(dp[l][r]!=-1)return dp[l][r];

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

        tmp=min(tmp,f(l,i)+f(i+1,r)+ps[r]-ps[l-1]);

    }

    return dp[l][r]=tmp;

}



int main(){

    cin >> n;

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

        cin >> a[i];

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

    }

    memset(dp,-1,sizeof dp);

    cout << f(1,n) << endl;

}

// Faster C++ solution for the same problem (measured speedup: 2.313543909780174x):
// written by : soumya_27

#include<bits/stdc++.h>

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

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

#define int long long

#define ld long double

#define mod ((int)1e9+7)

#define maxx ((int)2e6+6)

#define pb push_back

#define ins insert

#define F first

#define S second

#define ins insert

#define pii pair <int , int>

#define lb lower_bound

#define ub upper_bound

#define usefile freopen("input.txt","r",stdin); freopen("ou(tp)ut.txt","w",stdout);

#define fast ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

#define test int t; cin >> t; while(t--)

#define all(x) (x).begin(), (x).end()

#define rall(x) (x).rbegin(), (x).rend()

int exp(int x,int y,int p){ int res=1; while(y) {if(y%2) res=(res*x%p)%p; x=(x*x)%p; y/=2; } return res;}

int expo(int x,int y){ int res=1; while(y) {if(y%2) res=(res*x%mod)%mod; x=(x*x)%mod; y/=2; } return res;}

int add(int a, int b) {return (a%mod + b%mod + mod)%mod;}

int sub(int a, int b) {return (a%mod - b%mod + mod)%mod;}

int mul(int a, int b) {return ((a%mod)*(b%mod) + mod)%mod;}

int inv(int x) {return expo(x, mod-2);}

#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)     

#define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false); debug("%s time : %.4fs", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC))

// int sv[2000006]={0};

// int fact[1000006]={0};

// void facto() {fact[0]=1;fact[1]=1;for(int i=2;i<1000006;i++)fact[i]=(fact[i-1]*i)%mod;}

// void sve() {sv[0]=1;sv[1]=1;for(int i=2;i*i<maxx;i++){if(!sv[i]){for(int j=i*i;j<maxx;j+=i)sv[j]=1;}}}//0 hai to prime

// int ncr(int n,int r) {int res=1; res=fact[n]; res=(res*(expo(fact[r],mod-2)))%mod; res=(res*(expo(fact[n-r],mod-2)))%mod; return res;}

// int npr(int n,int r) {int res=1; res=fact[n]; res=(res*(expo(fact[n-r],mod-2)))%mod; return res;}

// using namespace __gnu_pbds;

// #define p_set tree< pair<int,int> ,  null_type ,  less<pair<int,int>> ,  rb_tree_tag ,  tree_order_statistics_node_update>

// #define i_set tree< int ,  null_type ,  less< int > ,  rb_tree_tag ,  tree_order_statistics_node_update>

using namespace std;

#define N 405

#define inf 1e18

int d[N][N];

void solve()

{

    fast

    // test

    {

        cout << setprecision(10);

        int n, m, i, j, k, x, y, q;

        cin >> n;

        int a[n];

        for(i=0 ; i<n ; i++)

            cin >> a[i];

        int p[n] = {0};

        p[0] = a[0];

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

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

        for(i=0 ; i<n ; i++)

            for(j=0 ; j<n ; j++)

            {

                if(i != j)

                    d[i][j] = inf;

            }

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

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

            {

                j = i+len-1;

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

                {

                    int val = d[i][k] + d[k+1][j] + p[j];

                    if(i>0)

                        val -= p[i-1];

                    d[i][j] = min(d[i][j] , val);

                }

            }

        cout << d[0][n-1] << "\n";

    }

}



signed main()

{

    // time__("solve")

    {

        solve();

        

    }

    return 0;

}