#include <bits/stdc++.h>
using namespace std;
const int maxn = (1 << 18) | 1;
int n, a[maxn];
priority_queue<int, vector<int>, greater<int> > pq;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  int logn;
  for (logn = 0; (1 << logn) < n; logn++)
    ;
  long long ans = 0;
  for (int i = n, j = 0; i; i--) {
    if (a[i] == -1) break;
    j++, pq.push(a[i]);
    if (i == n || j == (1 << logn)) ans += pq.top(), pq.pop(), j = 0, logn--;
  }
  printf("%lld\n", ans);
  return 0;
}
