#include <bits/stdc++.h>
using namespace std;
int a[(1 << 18) + 5];
priority_queue<int, vector<int>, greater<int> > pq;
long long solve(int pos, int n) {
  if (pos >= n)
    return 0;
  else {
    long long res = 0;
    if (pq.empty() || a[n] < pq.top()) {
      res += a[n];
      for (int i = n / 2 + 1; i < n; ++i) pq.push(a[i]);
    } else {
      res += pq.top();
      pq.pop();
      for (int i = n / 2 + 1; i <= n; ++i) pq.push(a[i]);
    }
    res += solve(pos, n / 2);
    return res;
  }
}
void test_case() {
  int n;
  scanf("%d", &n);
  int p = -1;
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    if (a[i] == -1) p = i;
  }
  printf("%lld\n", solve(p, n));
}
int main() {
  int t = 1;
  for (int ti = 1; ti <= t; ++ti) {
    test_case();
  }
}
