#include <bits/stdc++.h>
using namespace std;
int n, a;
long long sum[404];
long long dp[404][404];
long long minCost(int left, int right){
	if(left == right) return 0;
	long long& ret = dp[left][right];
	if(ret != -1)	return ret;
	ret = 1e18;
	for(int i = left+1;i<=right;i++){
		ret = min(ret,minCost(left,i-1)+minCost(i,right)+sum[right]-sum[left-1]);
	}
	return ret;
}
int main() {
	cin>>n;
	sum[0] = 0;
	for(int i = 1;i<=n;i++){
		cin>>a;
		sum[i] = sum[i-1]+a*1LL;
	}
	memset(dp,-1,sizeof(dp));
	cout<<minCost(1,n);
	return 0;
}