#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  vector<int> boxers;
  int s, t, n, f, r;
  ll cost = 0;
  struct greaters {
    bool operator()(const int& a, const int& b) const { return a > b; }
  };
  cin >> n;
  boxers.resize(n + 2);
  for (int i = 1; i <= n; i++) {
    cin >> boxers[i];
    if (boxers[i] == -1) f = i;
  }
  s = n;
  t = n + 1;
  r = n - f;
  while (r > 0) {
    make_heap(begin(boxers) + s, begin(boxers) + t, greaters());
    cost += boxers[s];
    t--;
    boxers[s] = boxers[t];
    n /= 2;
    s -= n;
    r -= n;
  }
  cout << cost;
}
