//--------PREPROCESSOR--------PREPROCESSOR--------PREPROCESSOR--------PREPROCESSOR--------PREPROCESSOR--------PREPROCESSOR--------PREPROCESSOR
//--------PREPROCESSOR--------PREPROCESSOR--------PREPROCESSOR--------PREPROCESSOR--------PREPROCESSOR--------PREPROCESSOR--------PREPROCESSOR

// Important header files
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less
#include <iostream>
using namespace __gnu_pbds;
using namespace std;



#include <iostream>
#include<bits/stdc++.h>
using namespace std;
using namespace std::chrono;
#define REDL(a ,n) vll a(n);for(ll i=0;i<n;i++) cin>>a[i];
#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define all(X)  X.begin() ,  X.end()
#define reset(X) memset(X, 0, sizeof(X))
#define f(a,b,c) for(ll a=b;a<c;a++)
//-----------------------------TEMPLATES------------------TEMPLATES---------------------------TEMPLATES---------------------------------
//-----------------------------TEMPLATES------------------TEMPLATES---------------------------TEMPLATES---------------------------------



//--------------TYPEDEF--------------TYPEDEF--------------TYPEDEF--------------TYPEDEF--------------TYPEDEF--------------TYPEDEF--------------TYPEDEF----------------------------TYPEDEF
//--------------TYPEDEF--------------TYPEDEF--------------TYPEDEF--------------TYPEDEF--------------TYPEDEF--------------TYPEDEF--------------TYPEDEF----------------------------TYPEDEF

typedef  long long ll;
typedef complex<long double> cd;
typedef long double ld;
typedef pair<ll,ll> pll;
typedef pair<ll,ll> pii;
typedef vector<ll> vll;
// Declaring ordered_set
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
        tree_order_statistics_node_update>
        ordered_set;

//--------------CONSTANTS--------------CONSTANTS--------------CONSTANTS--------------CONSTANTS--------------CONSTANTS--------------CONSTANTS--------------CONSTANTS
//--------------CONSTANTS--------------CONSTANTS--------------CONSTANTS--------------CONSTANTS--------------CONSTANTS--------------CONSTANTS--------------CONSTANTS

const ll INF=LLONG_MAX-100;
const ll mod=1000000007;
//const ll mod=998244353;





//------------------IGNOREABLE_CONTENT------------------IGNOREABLE_CONTENT------------------IGNOREABLE_CONTENT------------------IGNOREABLE_CONTENT



// To find GCD of a and b

// To compute x raised to power y under modulo m
ll power(ll x, ll y, ll m);

// Function to find modular inverse of a under modulo m
// Assumption: m is prime
ll modInverse(ll a, ll m)
{
    ll g = __gcd(a, m);
    if (g != 1)
        cout << "Inverse doesn't exist";
    else
    {
        // If a and m are relatively prime, then modulo inverse
        // is a^(m-2) mode m
        return power(a, m-2, m);
    }
}

// To compute x^y under modulo m


//code to  look for-----------------------------------------------------------------
/*
const ll MAXN=1e6+100;
ll  prime[MAXN];
bool is_composite[MAXN];
ll phi[MAXN];

void sieve (ll n) {
	phi[1] = 1;
	ll primes_count=0;
	for (ll i = 2; i < n; ++i) {
		if (!is_composite[i]) {
			prime[primes_count]=i;
			primes_count++;
			phi[i] = i - 1;					//i is prime
		}
		for (ll j = 0; j < primes_count && i * prime[j] < n; ++j) {
			is_composite[i * prime[j]] = true;
			if (i % prime[j] == 0) {
				phi[i * prime[j]] = phi[i] * prime[j];	//prime[j] divides i
				break;
			} else {
				phi[i * prime[j]] = phi[i] * phi[prime[j]];	//prime[j] does not divide i
			}
		}
	}
}*/



void fft(vector<cd>&  a, bool invert)
{
    ll n=a.size();
    ld PI=acos(-1);
    for(ll i=1,j=0; i<n; i++)
    {
        ll bit=n>>1;
        for(; j&bit; bit>>=1)
        {
            j^=bit;
        }
        j^=bit;
        if(i<j)
            swap(a[i], a[j]);
    }
    for(ll i=2; i<=n; i<<=1)
    {
        double ang=2*PI/i*(invert?-1:1);
        cd wn(cos(ang), sin(ang));
        for(ll j=0; j<n; j+=i)
        {
            cd w(1);
            for(ll k=j; k<j+i/2; k++)
            {
                cd u=a[k], v=a[k+i/2]*w;
                a[k]=u+v;
                a[k+i/2]=u-v;
                w*=wn;
            }
        }

    }
    if(invert)
    {
        for(ll i=0; i<n; i++)
            a[i]/=n;
    }

}
vector<ll>  multfft(vector<ll> a, vector<ll>b)
{
    vector<cd> fa(all(a)), fb(all(b));
    ll n=1;
    while(fa.size()+fb.size()>n)
        n*=2;
    fa.resize(n);
    fb.resize(n);
    vector<ll> result(n);
    fft(fa,false);
    fft(fb,false);
    for(ll i=0; i<n; i++)
        fa[i]*=fb[i];
    fft(fa,true);
    for(ll i=0; i<n; i++)
        result[i]=round(fa[i].real());
    return result;

}
bool isPrime(ll n)
{
    // Corner case
    if (n <= 1)
        return false;

    // Check from 2 to n-1
    for (ll i=2; i<n; i++)
        if (n%i == 0)
            return false;

    return true;
}

//----------------------MAIN______________________________MAIN__________________MAIN

struct compare
{
    bool operator() (const pll& lhs, const pll& rhs) const
    {
        if(lhs.fi<rhs.se)
            return true;
        if(lhs.fi>rhs.se)
            return false;
        return lhs.se>rhs.se;
    }
};
ll power(ll x, ll y, ll p)
{
    ll res = 1;      // Initialize result

    x = x % p;  // Update x if it is more than or
    // equal to p

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res*x) % p;

        // y must be even now
        y = y>>1; // y = y/2
        x = (x*x) % p;
    }
    return res;
}
vll mult(vll freq, vll freq2,ll n )
{
    ll k=accumulate(all(freq), 0);
    if(k==0)
        return freq2;
    vll temp(n);
    for(ll i=0; i<n; i++)
    {
        for(ll j=0; j<n; j++)
        {
            temp[(i*j)%n]=(temp[(i*j)%n]+(freq[i]*freq2[j])%mod)%mod;
        }
    }
    return temp;
}
string common(string& a, string b)
{
    ll k=min(a.size(), b.size());
    string p=a.substr(a.size()-k);
    string test=b+"$"+p;
    ll z[test.size()+10];
    reset(z);
    for(ll i=1,l=0,r=0;i<test.size();i++)
    {
        if(i<=r)
        z[i]=min(r-i+1, z[i-l]);
        while(i+z[i]<test.size() && test[i+z[i]]==test[z[i]])
            ++z[i];
        if(r-i+1<z[i])
        {
            l=i, r=i+z[i]-1;
        }
    }

    ll mx=0;
    for(ll i=test.size()-k;i<test.size();i++)
    {
        if(i+z[i]==test.size())
            mx=max(mx, z[i]);

    }
    a+=b.substr(mx);
    return b ;
}
signed main()
{


    auto start = high_resolution_clock::now();

    ios_base::sync_with_stdio(false);
    cin.tie(0);
    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
    cout.tie(0);
    //----------------  --------------------------------------------------------------------------------
    ll n;
    cin>>n;
    REDL(a,n);
    ll pref[n];
    pref[0]=a[0];
    for(int i=1;i<n;i++)
        pref[i]=pref[i-1]+a[i];
    ll best[n+1][n];
    reset(best);
    for(ll i=0;i<=n;i++)
        for(ll j=0;j<n;j++)
            best[i][j]=1e18;
    for(ll i=0;i<n;i++)
        best[1][i]=a[i];
    for(ll i=2;i<=n;i++)
    {
        for(ll j=0;j<n-i+1;j++)
        {
            for(ll k=1;k<i;k++)
            {
                best[i][j]=min(best[i][j], best[k][j]+best[i-k][j+k]+ pref[j+i-1]-(j!=0?pref[j-1]:0));
            }
        }
    }
    cout<<best[n][0]-pref[n-1];
    //-----------00;--------------------------
    //-----------------------------------------------------------


    auto stop = high_resolution_clock::now();
    auto duration = duration_cast<microseconds>(stop - start);

    cerr <<endl<<"Time in microseconds:" <<duration.count() <<endl<<"Time in seconds:"<<duration.count()/(1000000.0)<< endl;
}


Measured memory_bytes: 1536