// Slower C++ solution:
//#pragma GCC target ("avx2,sse2")

//#pragma GCC optimization ("Ofast")

//#pragma GCC optimization ("unroll-loops")

#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;



#define ordered_set tree <pair <ll, int>, null_type, less <pair <ll, int> >, rb_tree_tag, tree_order_statistics_node_update>

#define ll long long

#define ull unsigned long long

#define db double

#define pb push_back

#define pf push_front

#define ppb pop_back

#define ppf pop_front

#define fi first

#define se second

#define mp make_pair

#define up_b upper_bound

#define low_b lower_bound

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

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

#define endl "\n"



#define left fsdsdfoisf

#define sum dpsdfioppsf

#define assign xcvjlkdjfio

#define trie fksdfkjkfnjuiv

#define next sidlfjsfkl

#define merge sdfksdkfsldf



#define int long long



using namespace std;



void dout() {

    cerr << endl;

}

template <typename Head, typename... Tail>

void dout(Head H, Tail... T) {

    cerr << H << ' ';

    dout(T...);

}



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

typedef pair <int, int> pii;



const int N = 407, inf = 1e15;

int n, a[N], p[N], dp[N][N];



int calc(int l, int r) {

    if (l >= r) {

        return 0;

    }

    int & res = dp[l][r];

    if (res != -1) {

        return res;

    }

    res = inf;

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

        res = min(res, calc(l, i) + calc(i + 1, r) + (p[i] - p[l - 1]) + (p[r] - p[i]));

    }

    return res;

}



void solve(int tc) {

    // check for (int i = 0; i < n; j++)

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

    cin >> n;

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

        cin >> a[i];

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

    }

    cout << calc(1, n);

}



signed main() {

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);

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

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

    int tc = 1;

//    cin >> tc;

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

        solve(i);

//        cleanup();

    }

}


// Faster C++ solution for the same problem (measured speedup: 2.3440639894251736x):
#include <iostream>

#include <bits/stdc++.h>

using namespace std;



typedef vector<int> vi;

#define ll long long

#define PI 3.14159265359

#define x first

#define y second

#define point pair<int,int>

#define clr(x, val)	memset((x), (val), sizeof(x))



//you don't know math, unless you can do math!

ll dp[400][400];

ll OO = 1e15 + 10;



vector<ll> a, a_sum;

int main() {

    int n; cin>>n;

    a= vector<ll> (n);

    a_sum = vector<ll> (n);



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

        cin>>a[i];

        a_sum[i] = a[i];

        if(i) a_sum[i]+= a_sum[i - 1];

    }





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

        dp[i][i + 1] = a[i] + a[i + 1];

    }



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

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

         int j = i + sz;

         dp[i][j] = OO;



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

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



         dp[i][j] += a_sum[j];

         if(i - 1 >= 0) dp[i][j] -= a_sum[i - 1];

        }

    }



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

}