#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
const long long MOD = 1e9 + 7;
const long long INF = 1e18;
signed main() {
  int n;
  cin >> n;
  vector<long long> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  multiset<long long> kek;
  long long ans = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (a[i] == -1) {
      break;
    }
    kek.insert(a[i]);
    if ((i & (i + 1)) == 0) {
      ans += *(kek.begin());
      kek.erase(kek.begin());
    }
  }
  cout << ans << "\n";
  return 0;
}
