// Slower C++ solution:


/**

    And in the end, the love you take is equal to the love you make.



        I LOVE YOU LAURA.

**/



#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>

#include <ext/rope>



#define _CRT_SECURE_NO_DEPRECATE

#define _CRT_DISABLE_PERFCRIT_LOCKS



#define findOrder find_by_order

#define orderKey order_of_key



#define isOn(mask, i) (mask & (1 << i))

#define turn(mask, i) (mask |= (1 << i))

#define switch(mask, i) (mask &= ~(1 << i))

#define change(mask, i) (mask ^= (1 << i))

#define lowbit(mask) (mask & (-mask))

#define turnAll(mask, n) (mask = (1 << n) - 1)

#define switchLastBit(mask) (mask & (mask - 1))

#define turnLastZero(mask) (mask | (mask + 1))

#define modulo(x, mod) ((x) & (mod - 1))

#define log2(x) (31 - __builtin_clz(x))



#define mkp make_pair

#define pb push_back

#define pf push_front

#define eb emplace_back

#define ef emplace_front

#define ins insert

#define rsz resize

#define f first

#define s second

#define lb lower_bound

#define ub upper_bound



#define sz(x) ((int)x.size())

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

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



#define debug(x) (cerr << x << " ")



using namespace std;

using namespace __gnu_pbds;

using namespace __gnu_cxx;



typedef double db;

typedef long long ll;

typedef long double ld;

typedef unsigned long long ull;



typedef pair<int, int> ii;

typedef pair<ll,ll> pll;

typedef pair<ld,ld> pdb;

typedef pair<int, ii> tri;

typedef complex<ld> cd;



typedef vector<int> vi;

typedef vector<ii> vii;

typedef vector<ld> vdb;

typedef vector<cd> vcd;

typedef vector<ll> vll;

typedef vector<string> vs;

typedef vector<vll> matrix;



typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_tree;



const ll mod = 1e9+7;

const int maxn = 405;

const int pi = 2 * acos(0.0);

const db eps = 1e-9;



ll dp[maxn][maxn], sum[maxn], arr[maxn];



ll solve_dp (int l, int r){

    if (l == r) return 0LL;



    ll &ans = dp[l][r];

    if (ans != -1) return ans;



    ans = (1LL << 60);

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

        ans = min(ans, solve_dp(l, i) + solve_dp(i + 1, r));

    }



    ans += (sum[r] - sum[l - 1]); // cost of combine element

    return ans;

}



int main()

{

    ios_base::sync_with_stdio(0);

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



    memset(dp, -1, sizeof dp);



    int n; cin >> n;



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

        cin >> arr[i];

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

    }



    cout << solve_dp(1, n) << "\n";



    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 2.291392091788918x):
//Bismillahir Rahmanir Rahim

 

//#pragma GCC optimize("Ofast,unroll-loops")

//#pragma GCC target("avx,avx2,fma")

        

#include<bits/stdc++.h>

#include<ext/pb_ds/assoc_container.hpp>

#include<ext/pb_ds/tree_policy.hpp>

 

#define ll long long

#define ull unsigned long long

#define pii pair<int,int>

#define pll pair<ll,ll>

#define xx first

#define yy second

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

#define scii(n,m) scanf("%d%d",&n,&m)

#define sciii(n,m,w) scanf("%d%d%d",&n,&m,&w)

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

#define scll(n,m) scanf("%lld%lld",&n,&m)

#define sclll(n,m,w) scanf("%lld%lld%lld",&n,&m,&w)

#define pf(a)   printf("%d\n",a)

#define CASE(a) printf("Case %d: ",a)

#define endl '\n'

#define READ  freopen("input.txt","r",stdin)

#define WRITE freopen("output.txt","w",stdout);

#define PI acos(-1)

#define mem(a,b) memset(a,b,sizeof(a))

#define SQR(a) (a)*(a)

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

#define pb push_back

#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

#define mod 1000000007

#define inf INT_MAX

#define eps 1e-9



using namespace __gnu_pbds;

using namespace std;



#define Gene template< class

#define Rics printer& operator,

Gene c> struct rge{c b, e;};

Gene c> rge<c> range(c i, c j){ return {i, j};}

struct printer{

    ~printer(){cerr<<endl;}

    Gene c >Rics(c x){ cerr<<boolalpha<<x; return *this;}

    Rics(string x){cerr<<x;return *this;}

    Gene c, class d >Rics(pair<c, d> x){ return *this,"(",x.first,", ",x.second,")";}

    Gene ... d, Gene ...> class c >Rics(c<d...> x){ return *this, range(begin(x), end(x));}

    Gene c >Rics(rge<c> x){

        *this,"["; for(auto it = x.b; it != x.e; ++it)

            *this,(it==x.b?"":", "),*it; return *this,"]";}

};

#define debug() cerr<<"LINE "<<__LINE__<<" >> ", printer()

#define dbg(x) "[",#x,": ",(x),"] "

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int my_rand(int l, int r) {

    return uniform_int_distribution<int>(l, r) (rng);

}

 

//int dx[] = {0,1,0,-1,1,1,-1,-1};

//int dy[] = {1,0,-1,0,1,-1,-1,1};

 

//ll ncr(ll n,ll r){if(n==r)return 1;if(r==1)return n;if(dp[n][r]!=-1)return dp[n][r];return dp[n][r]=ncr(n-1,r)+ncr(n-1,r-1);}

 

// bit manipulations

 

//bool checkbit(int mask,int bit){return mask & (1<<bit);}

//int setbit(int mask,int bit){ return mask | (1<<bit) ; }

//int clearbit(int mask,int bit){return mask & ~(1<<bit);}

//int togglebit(int mask,int bit){return mask ^ (1<<bit);}

//int bitno(int mask) {return (int)__builtin_popcount(mask);}

 

template <typename T> T BigMod (T b,T p,T m){if (p == 0) return 1;if (p%2 == 0){T s = BigMod(b,p/2,m);return ((s%m)*(s%m))%m;}return ((b%m)*(BigMod(b,p-1,m)%m))%m;}

template <typename T> T ModInv (T b,T m){return BigMod(b,m-2,m);}

template <typename T> T POW(T B,T P){ if(P==0) return 1; if(P&1) return B*POW(B,P-1);  else return SQR(POW(B,P/2));}

template <typename T> T Dis(T x1,T y1,T x2, T y2){return sqrt( SQR(x1-x2) + SQR(y1-y2) );}

template <typename T> T Angle(T x1,T y1,T x2, T y2){ return atan( double(y1-y2) / double(x1-x2));}

template <typename T> T gcd(T a,T b){if(a<0)return gcd(-a,b);if(b<0)return gcd(a,-b);return (b==0)?a:gcd(b,a%b);}

template <typename T> T lcm(T a,T b) {if(a<0)return lcm(-a,b);if(b<0)return lcm(a,-b);return a*(b/gcd(a,b));}

template <typename T> T euclide(T a,T b,T &x,T &y) {if(a<0){T d=euclide(-a,b,x,y);x=-x;return d;}   if(b<0){T d=euclide(a,-b,x,y);y=-y;return d;}   if(b==0){x=1;y=0;return a;}else{T d=euclide(b,a%b,x,y);T t=x;x=y;y=t-(a/b)*y;return d;}}

template <typename T> void ia (T a[],int n){for (int i=0; i<n; i++) cin >> a[i];}

template <typename T> void pa (T a[],int n){for (int i=0; i<n-1; i++) cout << a[i] << " ";cout << a[n-1] << endl;}

template <typename T> ll isLeft(T a,T b,T c) { return (a.x-b.x)*(b.y-c.y)-(b.x-c.x)*(a.y-b.y);}

 

template<typename T>

using ordered_set=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;

 

///********************************MY CODE STARTS HERE********************************///

 

 

#define mxn 405



ll arr[mxn];

ll dp[mxn][mxn];

ll cum[mxn];

 

void solve(){

   

   int n;

   cin >> n;



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

   	  cin >> arr[i];

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

   }



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

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

   		dp[i][j] = 1e17;

   	}

   }





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

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

   	 	int j = i + sz - 1;

   	 	if(sz == 1){

   	 		dp[i][j] = 0;

   	 		continue;

   	 	}

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

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

   	 		//cout << dp[i][j] << endl;

   	 	}

   	 }

   }



   cout << dp[1][n] << endl;





   



}

 

 

int main(){

       IOS;

    

    int T = 1;

    

    // cin >> T;



    while(T--){



    	solve();

    }



return 0;

}