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

using namespace std;



int const N=4e2+5, M=1e5+5, /*OO = 0x3f3f3f3f,*/ MOD=1e9+7;

long long const OO = 0x3f3f3f3f3f3f3f3f;

int n;

long long arr[N];



long long mem[N][N];

long long solve(int s, int e)

{

    if(s==e)

        return 0;

    if(mem[s][e]!=-1)

        return mem[s][e];

    long long ret=OO;

    for(int i=s; i<e; i++)

        ret=min(ret,solve(s,i)+solve(i+1,e)+arr[e]-arr[s-1]);

    return mem[s][e]=ret;

}





int main()

{

//    freopen("input.txt","rt",stdin);

//    freopen("output.txt","wt",stdout);

    scanf("%d",&n);



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

    {

        scanf("%lld",arr+i);

        arr[i]+=arr[i-1];

    }

    memset(mem, -1, sizeof mem);

    printf("%lld\n",solve(1,n));





}


// Faster C++ solution for the same problem (measured speedup: 2.2748252969693175x):
// 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;

}