#include <bits/stdc++.h>
using namespace std;
int a[1000010];
priority_queue<int, vector<int>, greater<int> > pq;
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  if (a[n] == -1) {
    printf("0\n");
    return 0;
  }
  long long ans = a[n];
  int pos = n - 1;
  int cnt = n / 2;
  while (pos > 0) {
    bool ok = false;
    for (int i = pos; i >= pos - cnt + 1; i--) {
      if (a[i] == -1) {
        ok = true;
        break;
      }
      pq.push(a[i]);
    }
    if (ok) break;
    ans += pq.top();
    pq.pop();
    pos -= cnt;
    cnt >>= 1;
  }
  printf("%lld\n", ans);
  return 0;
}
