#include <iostream>
using namespace std;

const long long INF = 1LL << 60;

int N;
long long a[409];
long long dp[409][409];

long long solve(int l, int r) {
    if(l == r) return 0;
    if(dp[l][r] != 0) return dp[l][r];

    long long res = INF;
    for(int i = l; i < r; i++)
        res = min(res, solve(l, i) + solve(i + 1, r));

    return dp[l][r] = res + a[r] - a[l - 1];
}

int main() {
    cin >> N;
    for(int i = 1; i <= N; i++) {
        cin >> a[i];
        a[i] += a[i - 1]; // 累積和を取る
    }

    cout << solve(1, N) << endl;
}