#include <bits/stdc++.h>
using namespace std;
long long a[410], dp[410][410], sum[410] = {}, x = 0, mx = 1000000000000000;
long long solve(int l, int r) {
  if (dp[l][r] != mx) return dp[l][r];
  if (r - l == 1) {
    dp[l][r] = 0;
    return 0;
  }
  for (int i = l + 1; i < r; i++) {
    dp[l][r] =
        min(dp[l][r], solve(l, i) + solve(i, r) + sum[r - 1] - sum[l - 1]);
  }
  return dp[l][r];
}
int main() {
  int i, j, n;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    x += a[i];
    sum[i] = x;
  }
  for (i = 0; i <= n + 1; i++) {
    for (j = 0; j <= n + 1; j++) {
      dp[i][j] = mx;
    }
  }
  cout << solve(1, n + 1) << endl;
}
