#include <bits/stdc++.h>
using namespace std;
const int N = (1 << 18) + 10;
multiset<int> st;
map<int, int> vis;
int a[N];
int n;
long long solve() {
  long long ans = 0;
  return ans;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", a + i);
  }
  int k = 1;
  while (k <= n) {
    vis[k] = 1;
    k = k * 2;
  }
  long long ans = 0;
  for (int i = n; i >= 1; i--) {
    if (a[i] == -1) break;
    st.insert(a[i]);
    if (vis[i]) {
      ans += *st.begin();
      st.erase(st.begin());
    }
  }
  printf("%lld\n", ans);
  return 0;
}
