#include <bits/stdc++.h>
using namespace std;
const int MAXN = (1 << 20);
bool Need[MAXN];
int A[MAXN];
int n, id = 1;
long long ans;
int main(void) {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  priority_queue<int, vector<int>, greater<int>> Q;
  while (id <= n) Need[id] = 1, id <<= 1;
  for (int i = 1; i <= n; ++i) cin >> A[i];
  while (n--) {
    if (A[n + 1] == -1) break;
    Q.push(A[n + 1]);
    if (Need[n + 1]) ans += Q.top(), Q.pop();
  }
  cout << ans << '\n';
  return 0;
}
