#include <bits/stdc++.h>
using namespace std;
const int maxx = 1e6 + 10;
const int mod = 1e9 + 7;
int s[maxx];
int pos, num;
priority_queue<int, vector<int>, greater<int>> q;
int main() {
  int n;
  scanf("%d", &n);
  for (int i = n; i > 0; i--) {
    scanf("%d", &s[i]);
  }
  num = n / 2;
  long long ans = 0;
  for (int i = 1; s[i] != -1; i++) {
    q.push(s[i]);
    if (i > pos) {
      ans += q.top();
      q.pop();
      pos += num;
      num /= 2;
    }
  }
  printf("%lld\n", ans);
  return 0;
}
