// Slower C++ solution:
/*input

3

1000000000 1000000000 1000000000

*/



#include <functional>

#include <algorithm>

#include <iostream>

#include <string.h>

#include <utility> 

#include <iomanip>

#include <string>

#include <vector>

#include <math.h>

#include <map>

#include <set>



using namespace std;



using ll = long long;

using ld = long double;



const int SINF = 1000000009;

const ll INF =  1000000000000000018;



#define sp          cout << " "

#define nl          cout << "\n"

#define all(c)      (c).begin(),(c).end()

#define prec(n)     cout << fixed << setprecision(n)

#define PI          3.14159265358979323846264338327951

#define ifalse      ios_base::sync_with_stdio(false), cin.tie(NULL)



template <typename T, typename U>

ostream &operator<<(ostream &os, const pair<T, U> &p) {

  os << p.first << " " << p.second;

  return os;

}



template <typename T, typename U>

istream &operator>>(istream &is, pair<T, U> &p) {

  is >> p.first >> p.second;

  return is;

}



template <typename T>

ostream &operator<<(ostream &os, const vector<T> &v) {

  for(auto x: v) {

    os << x << " ";

  }

  return os;

}



template <typename T>

istream &operator>>(istream &is, vector<T> &v) {

  for (auto &x : v) is >> x;

  return is;

}



/******************************** START *********************************/

const int N = 405;

vector < ll > v;

ll dp[N][N];





ll func(int l, int r)

{

  if(l == r) {

    return 0;

  }

  if(dp[l][r] > -1) {

    return dp[l][r];

  }



  ll res = INF;

  for(int i = l; i < r; i ++) {

    res = min(res, func(l, i) + func(i + 1, r) + v[r] - v[l - 1]);

  }

  return dp[l][r] = res;

}



int main()

{

  ifalse;

  int n;

  cin >> n;

  v.resize(n + 1);

  for(int i = 0; i < n; i ++) {

    cin >> v[i + 1];

  }

  for(int i = 1; i <= n; i ++) {

    v[i] += v[i - 1];

  }



  memset(dp, -1, sizeof(dp));

  cout << func(1, n), nl;

  return 0;

}

// Faster C++ solution for the same problem (measured speedup: 2.2985571102135176x):
/*input

5

10 10 10 10 10

*/



#include <functional>

#include <algorithm>

#include <iostream>

#include <string.h>

#include <utility> 

#include <iomanip>

#include <string>

#include <vector>

#include <math.h>

#include <map>

#include <set>



using namespace std;



using ll = long long;

using ld = long double;



const int SINF = 1000000009;

const ll INF =  1000000000000000018;



#define sp          cout << " "

#define nl          cout << "\n"

#define all(c)      (c).begin(),(c).end()

#define prec(n)     cout << fixed << setprecision(n)

#define PI          3.14159265358979323846264338327951

#define ifalse      ios_base::sync_with_stdio(false), cin.tie(NULL)



template <typename T, typename U>

ostream &operator<<(ostream &os, const pair<T, U> &p) {

  os << p.first << " " << p.second;

  return os;

}



template <typename T, typename U>

istream &operator>>(istream &is, pair<T, U> &p) {

  is >> p.first >> p.second;

  return is;

}



template <typename T>

ostream &operator<<(ostream &os, const vector<T> &v) {

  for(auto x: v) {

    os << x << " ";

  }

  return os;

}



template <typename T>

istream &operator>>(istream &is, vector<T> &v) {

  for (auto &x : v) is >> x;

  return is;

}



/******************************** START *********************************/

const int N = 405;

vector < ll > v;

ll dp[N][N];



int main()

{

  ifalse;

  int n;

  cin >> n;

  v.resize(n + 1);

  for(int i = 0; i < n; i ++) {

    cin >> v[i + 1];

  }

  for(int i = 1; i <= n; i ++) {

    v[i] += v[i - 1];

  }



  for(int i = 0; i <= n; i ++) {

    for(int j = 0; j <= n; j ++) {

      if(i == j) {

        dp[i][j] = 0;

      } else {

        dp[i][j] = INF;

      }

    }

  }



  ll ans = 0;

  for(int i = n - 1; i >= 1; i --) {

    for(int j = i + 1; j <= n; j ++) {

      for(int k = i; k < j; k ++) {

        dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j] + v[j] - v[i - 1]);

      }

    }

  }



  cout << dp[1][n], nl;

  return 0;

}