#include <iostream>
#define ll long long
using namespace std;
ll dp[401][401]={},ar[401]={},n;
int main()
{
	cin>>n;
	for (int i=1;i<=n;i++)
	{
		cin>>ar[i];ar[i]+=ar[i-1];
	}
	for (int i=0;i<401;i++)
	{
		for (int j=0;j<401;j++)
		{
			dp[i][j]=10000000000000000;
			if (i==j) {dp[i][i]=0;}
		}
	}
	for (int i=2;i<=n;i++)
	{
		for (int j=1;j<=n-i+1;j++)
		{
			for (int k=j;k<j+i-1;k++)
			{
				dp[j][j+i-1] = min(dp[j][j+i-1],dp[j][k]+dp[k+1][j+i-1]);
			}
			dp[j][j+i-1]+=ar[j+i-1]-ar[j-1];
		}
	}
	cout<<dp[1][n]<<"\n";
	return 0;
}