#include <bits/stdc++.h>
using namespace std;
int n, A[1 << 19];
long long ans;
multiset<int> bribes;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  ans = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (A[i] == -1) break;
    bribes.insert(A[i]);
    if ((!((i + 1) & ((i + 1) - 1)))) {
      ans += *bribes.begin();
      bribes.erase(bribes.begin());
    }
  }
  cout << ans << '\n';
  return 0;
}
