#include "bits/stdc++.h"
using namespace std;

long long N, A[400], SUM[410] = {};
static long long DP[410][410] = {};
bool used[410][410] = {};

long long DFS(long long L, long long R) {
	if (L + 1 == R) return 0;
	if (used[L][R]) return DP[L][R];
	long long RET = 1000000000000000000;
	for (int i = L + 1; i < R; i++) RET = min(RET, DFS(L, i) + DFS(i, R));
	used[L][R] = true;
	return DP[L][R] = RET + SUM[R] - SUM[L];
}

int main() {
	cin >> N;
	for (int i = 0; i < N; i++) cin >> A[i], SUM[i + 1] = SUM[i] + A[i];
	cout << DFS(0, N) << endl;
}