#include <bits/stdc++.h>

using namespace std;

const int N = 410;

typedef long long ll;

ll dp[N][N];
int n;
ll a[N];

ll go(int l, int r) {
  if(l >= r) return 0;
  if(dp[l][r] != -1) return dp[l][r];
  ll ans = 1e18;
  ll sum = 0ll;
  for(int i = l; i <= r; i++) sum += a[i];
  for(int i = l; i < r; i++) {
    ans = min(ans, go(l, i) + go(i + 1, r) + sum);
  }
  return dp[l][r] = ans;
}

int main() {
  memset(dp, -1, sizeof dp);
  cin >> n;
  for(int i = 0; i < n; i++) cin >> a[i];
  cout << go(0, n - 1) << '\n';
}