#include <cstdio>
#include <cstring>
const int N = 400 + 10;
typedef long long ll;
int n, a[N];
ll f[N][N], sum[N];
ll mi(ll a, ll b)
{
	return a > b ? b : a;
}
ll dp(int l, int r)
{
	if (l == r) return 0;
	if (~f[l][r]) return f[l][r];
	f[l][r] = 1e18;
	for (int i = l; i < r; i++)
		f[l][r] = mi(f[l][r], dp(l, i) + dp(i + 1, r) + sum[r] - sum[l - 1]);
	return f[l][r];
}
int main()
{
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
	{
		scanf("%d", &a[i]);
		sum[i] = sum[i - 1] + a[i];
	}
	memset(f, -1, sizeof(f));
	printf("%lld", dp(1, n));
}