#include <bits/stdc++.h>

#define ll long long

using namespace std;

const int N = 405;

int n;

ll a[N], dp[N][N], p[N];

int main () {
    cin >> n;
    for (int i = 1;i <= n;i ++) {
        cin >> a[i];
        p[i] = p[i - 1] + a[i];
    }
    for (int i = 2;i <= n;i ++) {
        for (int l = 1, r = i;r <= n;l ++, r ++) {
            dp[l][r] = 1e18 + 7;
            for (int mid = l;mid < r;mid ++) {
                dp[l][r] = min (dp[l][r], dp[l][mid] + dp[mid + 1][r] + p[r] - p[l - 1]);
            }
        }
    }
    cout << dp[1][n];
}
