#include <bits/stdc++.h>
using namespace std;
int a[1010000];
int main() {
  int n;
  cin >> n;
  int index;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] == -1) index = i;
  }
  int pow1 = 0;
  for (int m = n; m; m >>= 1) pow1++;
  int pow2 = 0;
  for (int m = index; m; m >>= 1) pow2++;
  int num = pow1 - pow2;
  int poww = n;
  int now = n;
  long long ans = 0;
  priority_queue<int, vector<int>, greater<int> > q;
  for (; num > 0;) {
    for (; now >= poww && now >= 1 && now > index; now--) q.push(a[now]);
    ans += q.top();
    q.pop();
    poww >>= 1;
    num--;
  }
  cout << ans << endl;
}
