#include <bits/stdc++.h>
using namespace std;
long long n,vals[401],dp[401][401];

int main(){
	cin >> n;
	for(int i = 0 ; i < 401 ; i += 1){
		for(int j = 0 ; j < 401 ; j += 1){
			dp[i][j] = 1000000000000000000;
		}
	}
	for(int r = 1 ; r <= n ; r += 1){
		cin >> vals[r];
		dp[r][r] = 0;
		long long sum = 0;
		for(int l = r ; l > 0 ; l -= 1){
			sum += vals[l];
			for(int k = r ; k >= l  ; k -= 1){
				dp[l][r] = min(dp[l][r] , sum+dp[l][k-1]+dp[k][r]);
			}
		}
	}
	cout << dp[1][n] << endl;
}