#include<cstdio>
#include<iostream>
using namespace std;
const int MAXN = 410;
const long long INF = 0x7f7f7f7f7f7f7f; 
int n, a[MAXN];
long long sum[MAXN], f[MAXN][MAXN];
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			f[i][j] = INF;
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]),sum[i] = sum[i-1]+a[i], f[i][i]=0;
	for(int l=1;l<n;l++)
		for(int i=1,j=i+l;i<=n&&j<=n;i++,j=i+l)
			for(int k=i;k<j;k++)
				f[i][j] = min(f[i][j], f[i][k]+f[k+1][j]+sum[j]-sum[i-1]); 
	printf("%lld\n",f[1][n]);
	return 0;
}