#include <bits/stdc++.h>
using namespace std;
long long n, ans, a[2000005], ind;
multiset<long long> st;
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] == -1) ind = i;
  }
  for (int i = n; i > ind; i--) {
    st.insert(a[i]);
    if (__builtin_popcountll(i) == 1) {
      ans += (*st.begin());
      st.erase(st.begin());
    }
  }
  cout << ans << endl;
  return 0;
}
