#include <bits/stdc++.h>
using namespace std;
const int maxn = (1 << 18) + 5;
int a[maxn], vis[maxn];
struct Node {
  int x;
  bool operator<(const Node &rhs) const { return x > rhs.x; }
};
priority_queue<Node> q;
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  int x = 1;
  while (x <= n) {
    vis[x] = 1;
    x <<= 1;
  }
  multiset<int> s;
  long long ans = 0;
  for (int i = n; i >= 1; i--) {
    if (a[i] == -1) break;
    q.push(Node{a[i]});
    s.insert(a[i]);
    if (vis[i]) {
      ans += q.top().x;
      q.pop();
    }
  }
  printf("%lld\n", ans);
  return 0;
}
