#include<bits/stdc++.h>


using namespace std;

const int N =1000;

typedef long long ll;

ll f[N][N];
ll a[N],s[N]; 

int main()
{
	int n;
	cin>>n;
	s[0]=0;
	for(int i=1;i<=n;i++)	{
		int x;
		cin>>x;
		s[i]+=s[i-1]+x;	
	}
	memset(f,0,sizeof(f));
	for(int len=2;len<=n;len++)
	{
		for(int i=1;i+len-1<=n;i++)
		{
			int j=i+len-1;
			f[i][j]=1e18;
			for(int k=i;k<j;k++)
			f[i][j]=min(f[i][j],f[i][k]+f[k+1][j]+s[j]-s[i-1]);
		}
	}
	cout<<f[1][n]<<endl;
}