#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int N;
ll ans = LLONG_MAX;
map<vector<ll>, int> mp;
void dfs(vector<ll>& a) {
  if (mp.count(a) == true)
    return;
  else
    mp[a] = 1;
  if (a.size() == 2) {
    ans = min(ans, a[0] + a[1]);
    return;
  } else if (a.size() < 16) {
    for (int i = 1; i < a.size() - 1; i++) {
      vector<ll> tmp;
      for (int j = 0; j < a.size(); j++) tmp.push_back(a[j]);
      tmp[i - 1] += tmp[i];
      tmp[i + 1] += tmp[i];
      tmp.erase(tmp.begin() + i);
      dfs(tmp);
    }
  } else {
    pair<ll, int> mitr;
    mitr.first = 1;
    mitr.second = 1;
    for (int i = 2; i < a.size() - 1; i++) {
      if (a[mitr.first] > a[i]) {
        mitr.first = i;
        mitr.second = 1;
      } else if (a[mitr.first] == a[i]) {
        mitr.second++;
      }
    }
    if (mitr.second == 1) {
      int itr = mitr.first;
      a[itr - 1] += a[itr];
      a[itr + 1] += a[itr];
      a.erase(a.begin() + itr);
      dfs(a);
    } else {
      for (int i = 0; i < mitr.second; i++) {
        vector<ll> tmp;
        for (int j = 0; j < a.size(); j++) tmp.push_back(a[j]);
        int itr = mitr.first;
        tmp[itr - 1] += tmp[itr];
        tmp[itr + 1] += tmp[itr];
        tmp.erase(tmp.begin() + itr);
        dfs(tmp);
      }
    }
  }
}
int main() {
  cin >> N;
  vector<ll> a(N);
  for (int i = 0; i < N; i++) cin >> a[i];
  dfs(a);
  cout << ans << endl;
}
