#include <bits/stdc++.h>
using namespace std;
priority_queue<long long, vector<long long>, greater<long long> > que;
const long long MAXN = 1e6 + 10;
long long a[MAXN];
bool Judge(long long x) {
  long long cnt = 0;
  while (x) {
    cnt += (x % 2) == 1;
    x /= 2;
  }
  return cnt == 1;
}
signed main() {
  ios::sync_with_stdio(false);
  long long n, ans = 0;
  cin >> n;
  for (long long i = 1; i <= n; i++) cin >> a[i];
  for (long long i = n; i >= 1; i--) {
    que.push(a[i]);
    if (a[i] == -1) break;
    if (Judge(i)) ans += que.top(), que.pop();
  }
  cout << ans << endl;
  return 0;
}
