#include <cstdio>
#include <algorithm>
using namespace std;
long long dp[410][410], s[410];
int a[410];
int main() {
	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		scanf("%d", &a[i]);
		s[i+1] = s[i] + a[i];
	}
	for (int i = 2; i <= n; i++) {
		for (int j = 0; j < n - i + 1; j++) {
			dp[j][i+j] = 1ll << 61;
			for (int k = 1; k < i; k++) {
				dp[j][i+j] = min(dp[j][i+j], dp[j][j+k] + dp[j+k][i+j]);
			}
			dp[j][i+j] += s[i+j] - s[j];
		}
	}
	printf("%lld\n", dp[0][n]);
	return 0;
}
