#include <bits/stdc++.h>
using namespace std;
const long long N = 1011110;
long long a[N];
priority_queue<long long, vector<long long>, greater<long long> > q;
long long n;
signed main() {
  cin >> n;
  long long u = 0;
  for (long long i = 1; i <= n; ++i) {
    cin >> a[i];
    if (a[i] == -1) u = i;
  }
  long long k = n, ans = 0, cur = n / 2;
  for (long long i = n; i > u; --i) {
    q.push(a[i]);
    if (i == k) {
      ans += q.top();
      q.pop();
      k -= cur;
      cur /= 2;
    }
  }
  cout << ans << endl;
}
