// Slower C++ solution:
//  Mayank Agarwal

//  IIIT Hyderabad







#include<bits/stdc++.h>

using namespace std;

const long long int M = 1000000007; 

#define ll long long int

#define ld double

#define pb push_back

#define mp make_pair

ll power(ll x, ll y,  ll p) 

{ 

    ll res = 1;      

    x = x % p;   

    while (y > 0) 

    { 

        if (y & 1) 

            res = (res*x) % p; 

        y = y>>1;x = (x*x) % p; 

    } return res%p; 

} 

ll modinver(ll a,ll b)

{

    return (power(a,1,M)*power(b,M-2,M))%M;

}

void SieveOfEratosthenes(int n) 

{   

    bool prime[10005]; 

    memset(prime, true, sizeof(prime)); 

  

    for (int p=2; p*p<=n; p++) 

    { 

        if (prime[p] == true) 

        { 

            for (int i=p*p; i<=n; i += p) 

                prime[i] = false; 

        } 

    } 

} 

ll dp[404][403];

int main()

{

    ios_base::sync_with_stdio(false);

    cin.tie(0);cout.tie(0);

    ll n;

    cin>>n;

    std::vector<ll> v(n);

    for (int i = 0; i <=403; ++i)

    {

    	for (int j = 0; j <=403; ++j)

    	{

    		dp[i][j]=1e17;

    	}

    }

    for(ll& x:v)

    {

    	cin>>x;

    }

    std::vector<ll> cum(n);

    cum[0]=v[0];

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

    {

    	cum[i]+=cum[i-1]+v[i];

    }

    for (int L = n-1; L >=0; --L)

    {

    	for (int R = L; R < n; ++R)

    	{

	    	if(L==R)

	    	{

	    		dp[L][R]=0;

	    		continue;

	    	}

    		for(int X=L;X<R;X++)

    		{

	    		

	    		if(L!=0)

	    			dp[L][R]=min({dp[L][R],cum[X]-cum[L-1]+dp[L][X]+cum[R]-cum[X]+dp[X+1][R]});

	    		else

	    			dp[L][R]=min({dp[L][R],cum[X]+dp[L][X]+cum[R]-cum[X]+dp[X+1][R]});

	    	}

    		// cout<<L<<" "<<R<<" "<<dp[L][R]<<'\n';

    	}

    }

    cout<<dp[0][n-1]<<'\n';

}


// Faster C++ solution for the same problem (measured speedup: 2.281912728356206x):
//  Mayank Agarwal

//  IIIT Hyderabad







#include<bits/stdc++.h>

using namespace std;

const long long int M = 1000000007; 

#define ll long long int

#define ld double

#define pb push_back

#define mp make_pair

ll power(ll x, ll y,  ll p) 

{ 

    ll res = 1;      

    x = x % p;   

    while (y > 0) 

    { 

        if (y & 1) 

            res = (res*x) % p; 

        y = y>>1;x = (x*x) % p; 

    } return res%p; 

} 

ll modinver(ll a,ll b)

{

    return (power(a,1,M)*power(b,M-2,M))%M;

}

void SieveOfEratosthenes(int n) 

{   

    bool prime[10005]; 

    memset(prime, true, sizeof(prime)); 

  

    for (int p=2; p*p<=n; p++) 

    { 

        if (prime[p] == true) 

        { 

            for (int i=p*p; i<=n; i += p) 

                prime[i] = false; 

        } 

    } 

} 

ll dp[550][555];

int main()

{

    ios_base::sync_with_stdio(false);

    cin.tie(0);cout.tie(0);

    ll n;

    cin>>n;

    std::vector<ll> v(n);

    std::vector<ll> cum(n);

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

    {

        cin>>v[i];

        if(i==0)

        {

            cum[i]=v[i];

        }

        else

        {

            cum[i]=cum[i-1]+v[i];

        }

    }

    for (int l = n-1; l >=0 ; --l)

    {

        for (int r = l; r < n; ++r)

        {

            dp[l][r]=1e18;

            if(l==r)

            {

                dp[l][r]=0;

                // continue;

            }

            for (int k = l; k <r; ++k)

            {

                if(l==0)

                {

                    dp[l][r]=min(dp[l][r],dp[l][k]+dp[k+1][r]+cum[r]);

                }

                else

                {

                    dp[l][r]=min(dp[l][r],dp[l][k]+dp[k+1][r]+cum[r]-cum[l-1]);

                }   

            }

            // cout<<l<<" "<<r<<" "<<dp[l][r]<<'\n';

        }

    }

    cout<<dp[0][n-1]<<'\n';

}