#include <iostream>
using namespace std;
long long b[401][401];

int main() 
{
	int n;
	cin >> n;
	long long a[n + 1], prefixsum[n + 1];
	prefixsum[0] = 0;
	
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i];
		prefixsum[i] = prefixsum[i - 1] + a[i];
	}
	
	for (int i = 1; i <= n; i++)
	{
		b[i][i] = 0;
		for (int j = i - 1; j >= 1; j--)
			{
				long long amin = 1e18;
				for (int k = j; k <= i - 1; k++) 
					if (amin > b[j][k] + b[k + 1][i]) amin = b[j][k] + b[k + 1][i];
				b[j][i] = amin + prefixsum[i] - prefixsum[j - 1];
			}
	}
	cout << b[1][n];
}