#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
using vii = vector<ii>;
const ll INF = 1e18 + 7;
const ll MOD = 1e9 + 7;
const int MAXN = (1 << 18) + 2;
int n, a[MAXN], X;
ll mem[MAXN][20];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    if (a[i] == -1) X = i;
  }
  int comb = 0, won = 0;
  for (int i = n; i > 1; i >>= 1) comb++;
  for (int i = X + 1; i > 1; i >>= 1) {
    comb--;
    won++;
  }
  won = 1 << won;
  cerr << comb << ' ' << won << endl;
  mem[X][0] = 0;
  for (int j = 1; j <= comb; ++j) {
    mem[X][j] = INF;
  }
  for (int i = X + 1; i < n; ++i) {
    mem[i][0] = 0;
    for (int j = 1, w = 2; j <= comb; ++j, w <<= 1) {
      if (i - won + 1 >= (w - 1) * won) {
        mem[i][j] = min(mem[i - 1][j], mem[i - 1][j - 1] + a[i]);
      } else {
        mem[i][j] = mem[i - 1][j];
      }
    }
  }
  cout << mem[n - 1][comb] << endl;
}
