#include <bits/stdc++.h>
using namespace std;
int n, a[300010];
signed main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
  multiset<int> st;
  int64_t ans = 0;
  for (int i = n; i; i--) {
    if (a[i] == -1) break;
    st.insert(a[i]);
    if ((i & -i) == i) {
      ans += *st.begin();
      st.erase(st.begin());
    }
  }
  cout << ans << '\n';
  return 0;
}
