#include <bits/stdc++.h>
using namespace std;
long long int a[405];
void add_self(int &a, int b) {
  a += b;
  if (a >= (long long)1e18) {
    a -= (long long)1e18;
  }
}
void sub_self(int &a, int b) {
  a -= b;
  if (a < 0) {
    a += (long long)1e18;
  }
}
long long int sum(long long int l, long long int r) {
  long long int sum1 = 0;
  for (long long int i = l; i <= r; i++) {
    sum1 += a[i];
  }
  return sum1;
}
long long int min(long long int a, long long int b) { return a <= b ? a : b; }
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  long long int dp[n][n];
  for (int l = n - 1; l >= 0; l--) {
    for (int r = l; r < n; r++) {
      if (l == r) {
        dp[l][r] = 0;
      } else {
        dp[l][r] = (long long)1e18;
        long long int s = sum(l, r);
        for (int i = l; i <= r - 1; i++) {
          dp[l][r] = min(dp[l][r], dp[l][i] + dp[i + 1][r] + s);
        }
      }
    }
  }
  cout << dp[0][n - 1] << endl;
}
