#pragma GCC optimize("O2,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
//#include <boost/multiprecision/cpp_int.hpp>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace boost::multiprecision;
using namespace __gnu_pbds;
template <typename T>
using os = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long ll;
typedef double ld;
#define ios ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define pii pair<ll,ll>
#define vi vector<ll>
#define vii vector<pii >
#define mi map<ll,ll>
#define umi unordered_map<ll,ll,custom_hash>
#define all(a) (a).begin(),(a).end()
#define pb push_back
#define make make_pair
#define ff first
#define ss second
#define osl os<ll>
#define ospii os<pair<ll,ll> >
#define n_l '\n'
#define mem(a,b) memset(a,b,sizeof(a))
#define el "\n"
#define dbg(...) cout << "[" << #__VA_ARGS__ << "]: "; cout << to_string(__VA_ARGS__) << endl
template <typename T, size_t N> int SIZE(const T (&t)[N]){ return N; } template<typename T> int SIZE(const T &t){ return t.size(); } string to_string(const string s, int x1=0, int x2=1e9){ return '"' + ((x1 < s.size()) ? s.substr(x1, x2-x1+1) : "") + '"'; } string to_string(const char* s) { return to_string((string) s); } string to_string(const bool b) { return (b ? "true" : "false"); } string to_string(const char c){ return string({c}); } template<size_t N> string to_string(const bitset<N> &b, int x1=0, int x2=1e9){ string t = ""; for(int __iii__ = min(x1,SIZE(b)), __jjj__ = min(x2, SIZE(b)-1); __iii__ <= __jjj__; ++__iii__){ t += b[__iii__] + '0'; } return '"' + t + '"'; } template <typename A, typename... C> string to_string(const A (&v), int x1=0, int x2=1e9, C... coords); int l_v_l_v_l = 0, t_a_b_s = 0; template <typename A, typename B> string to_string(const pair<A, B> &p) { l_v_l_v_l++; string res = "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; l_v_l_v_l--; return res; } template <typename A, typename... C> string to_string(const A (&v), int x1, int x2, C... coords) { int rnk = rank<A>::value; string tab(t_a_b_s, ' '); string res = ""; bool first = true; if(l_v_l_v_l == 0) res += n_l; res += tab + "["; x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v)); auto l = begin(v); advance(l, x1); auto r = l; advance(r, (x2-x1) + (x2 < SIZE(v))); for (auto e = l; e != r; e = next(e)) { if (!first) { res += ", "; } first = false; l_v_l_v_l++; if(e != l){ if(rnk > 1) { res += n_l; t_a_b_s = l_v_l_v_l; }; } else{ t_a_b_s = 0; } res += to_string(*e, coords...); l_v_l_v_l--; } res += "]"; if(l_v_l_v_l == 0) res += n_l; return res; } void dbgm(){;} template<typename Heads, typename... Tails> void dbgm(Heads H, Tails... T){ cout << to_string(H) << " | "; dbgm(T...); }
#define dbgm(...) cout << "[" << #__VA_ARGS__ << "]: "; dbgm(__VA_ARGS__); cout << endl
clock_t time_p=clock();
std::istream& in(std::cin);
std::ostream& ot(std::cout);
ll mod=1e9+7;
void Auriel()
{
    time_p=clock()-time_p;
    cerr<<"Time Taken : "<<(float)(time_p)/CLOCKS_PER_SEC<<"\n";
}
struct custom_hash {
    static uint64_t splittpix64(uint64_t x) {

        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splittpix64(x + FIXED_RANDOM);
    }
};
template <typename R>
void println(R r){
    for(auto i:r)
        cout<<i<<" ";
    cout<<endl;

}
template <typename R>
void print(R r){
    int e=0;
    for(auto i:r)
        cout<<i<<endl;
}
template <typename R>
void scnln(R &r){
    for(auto &i:r)
        cin>>i;
}
ll depth[400005],parent[400005];
void make_set(int v) {
    parent[v] = v;
    depth[v] = 0;
}
int find_set(int v) {
    if (v == parent[v])
        return v;
    return parent[v] = find_set(parent[v]);
}
void union_sets(int a, int b) {
    a = find_set(a);
    b = find_set(b);
    if (a != b) {
        if (depth[a] < depth[b])
            swap(a, b);
        parent[b] = a;
        if (depth[a] == depth[b])
            depth[a]++;
    }
}
int main() {
    ios
     ll n; in>>n; vi v(n); scnln(v);
     ll dp[401][401],cost[401][401]; rep(i,0,401){
         rep(j,0,401) {
             dp[i][j] = LLONG_MAX;
             cost[i][j]=LLONG_MAX;
         }
     }
     rep(i,0,n) {
         dp[i][i] = v[i];
         cost[i][i]=0;
     }

     rep(i,n,0){
         rep(j,i+1,n){
             rep(k,i,j){
//                 if(j-i+1==2){
//                     dp[i][j]=min(dp[i][j],dp[i][i]+dp[j][j]);
//                     continue;
//                 }
//                 else if(k==i){
//                     dp[i][j]=min(dp[i][j],dp[i][i]+2*dp[k+1][j]);
//                     continue;
//                 }
//                 else if(k==j){
//                     dp[i][j]=min(dp[i][j],2*dp[i][k]+dp[j][j]);
//                     continue;
//                 }
//                 if(k+1<n)
                    dp[i][j]=min(dp[i][j],(dp[i][k]+dp[k+1][j]));
                    cost[i][j]=min(cost[i][j],dp[i][j]+cost[i][k]+cost[k+1][j]);



             }
         }
     }
//     ll mn=INT_MAX;
     ot<<cost[0][n-1]<<el;
    // ot<<mn<<el;

}






Measured memory_bytes: 2816