#include<iostream>
#include<cstring>
#define ll long long
#define pi acos(-1.0)
#define inf 0x3f3f3f3f
using namespace std;
const int maxn = 2e5+5;
ll dp[405][405], a[405], pre[405];
int n;

ll dfs(int l, int r){
	if(l >= r) return a[l];
	if(dp[l][r]) return dp[l][r];
	ll res = 1e17;
	for(int i = l; i < r; i++){
		res = min(res, dfs(l, i) + dfs(i+1, r));
	}
	return dp[l][r] = res + pre[r] - pre[l-1];
}

int main(){
	cin >> n;
	for(int i = 1; i <= n; i++){
		cin >> a[i];
		pre[i] = pre[i-1] + a[i];
	}

	cout << dfs(1, n) - pre[n] << endl;
	return 0;
}