#include <bits/stdc++.h>
using namespace std;
const int MAXN = 512 * 512 + 3;
const int INF = 1e9 + 7;
int t[MAXN];
multiset<int> q;
int main() {
  int n, poz, pnt;
  int hf, licz = 0;
  long long wyn = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &t[i]);
    if (t[i] == -1) {
      poz = n - i;
      t[i] = INF;
    }
  }
  hf = n / 2;
  while (poz > 0) {
    poz -= hf;
    hf /= 2;
    licz++;
  }
  hf = n / 2;
  pnt = n - 1;
  if (licz > 0) {
    licz--;
    wyn += (long long)(t[n]);
  }
  while (licz) {
    for (int i = pnt; i > pnt - hf; i--) q.insert(t[i]);
    pnt -= hf;
    hf /= 2;
    wyn += (long long)(*q.begin());
    q.erase(q.begin());
    licz--;
  }
  printf("%lld", wyn);
  return 0;
}
