#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 3e5 + 5;
priority_queue<long long, vector<long long>, greater<long long> > q;
long long a[maxn];
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  long long ans = 0;
  for (int i = 1; i <= n; i++) cin >> a[i];
  set<long long> s;
  long long d = 1;
  while (d <= n) {
    s.insert(d);
    d <<= 1;
  }
  for (int i = n; i > 0; i--) {
    if (a[i] == -1) break;
    q.push(a[i]);
    if (s.count(i)) {
      ans += q.top();
      q.pop();
    }
  }
  cout << ans << '\n';
  return 0;
}
