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



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

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



#define vi vector<int>

#define vll vector<long long>

#define ll long long

#define ull unsigned long long

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

#define nl cout<<'\n'

#define pb push_back

#define mp make_pair

#define pi pair<int,int>

#define pll pair<long long,long long>

#define vpii vector<pair<int,int> >

#define vpll vector<pair<ll,ll> >

#define si set<int>

#define sll set<long long>

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

#define Unique(x)           (x).erase(unique(all(x)), x.end())

#define v_present(vec, x)  (find(all(vec),x)!=(vec).end())

#define present(myset, x)    ((myset).find(x)!=(myset).end())

#define Cprint(c)  for(auto i:(c)) { cout<<i<<" "; } nl

#define trace2(x, y) cout<<x<<" "<<y<<endl;

#define trace3(x, y, z) cout<<x<<" "<<y<<" "<<z<<endl;

#define trace4(x, y, z, a) cout<<x<<" "<<y<<" "<<z<<" "<<a<<endl;

ll mod = 1e9 + 7;

const int MAX = 1e5 + 5;



using namespace std;



ll dp[405][405];  // dp[i][j] represents ways to divide j candies among 1st i students

ll pre[405];



ll solve(int i, int j){

    if(i==j)

        return 0;



    ll &ans=dp[i][j];

    if(ans!=-1)

        return ans;

    ans=1e15;

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

        ans=min(ans,solve(i,k)+solve(k+1,j)+pre[j]-pre[i-1]);

    }

    return ans;

}





int main() {

    FAST_IO;

    int n;

    cin >> n ;

    vll a(n+1);

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

        cin >> a[i];

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

    }

    memset(dp,-1,sizeof(dp));

    ll ans=solve(1,n);

    cout<<ans;





    return 0;



}

// Faster C++ solution for the same problem (measured speedup: 2.289892948161425x):
#include<bits/stdc++.h>



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

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



#define vi vector<int>

#define vll vector<long long>

#define ll long long

#define ull unsigned long long

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

#define nl cout<<'\n'

#define pb push_back

#define mp make_pair

#define pi pair<int,int>

#define pll pair<long long,long long>

#define vpii vector<pair<int,int> >

#define vpll vector<pair<ll,ll> >

#define si set<int>

#define sll set<long long>

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

#define Unique(x)           (x).erase(unique(all(x)), x.end())

#define v_present(vec, x)  (find(all(vec),x)!=(vec).end())

#define present(myset, x)    ((myset).find(x)!=(myset).end())

#define Cprint(c)  for(auto i:(c)) { cout<<i<<" "; } nl

#define trace2(x, y) cout<<x<<" "<<y<<endl;

#define trace3(x, y, z) cout<<x<<" "<<y<<" "<<z<<endl;

#define trace4(x, y, z, a) cout<<x<<" "<<y<<" "<<z<<" "<<a<<endl;

ll mod = 1e9 + 7;

const int MAX = 1e5 + 5;



using namespace std;



ll dp[405][405];  // dp[i][j] represents ways to divide j candies among 1st i students

ll pre[405];

//

//ll solve(int i, int j){

//    if(i==j)

//        return 0;

//

//    ll &ans=dp[i][j];

//    if(ans!=-1)

//        return ans;

//    ans=1e15;

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

//        ans=min(ans,solve(i,k)+solve(k+1,j)+pre[j]-pre[i-1]);

//    }

//    return ans;

//}





int main() {

    FAST_IO;

    int n;

    cin >> n ;

    vll a(n+1);

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

        cin >> a[i];

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

    }

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

        dp[i][i]=0;



    for(int siz=2;siz<=n;siz++){

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

            int l=i; int r=i+siz-1;

            //cout<<l<<" "<<r;nl;

            dp[l][r]=1e15;

            for(int k=l; k<=r-1; k++){

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

            }

        }

    }

    cout<<dp[1][n];

    return 0;



}