#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
const int N = 2097155;
const int mo = 998244353;
const double pi = acos(-1);
inline int read() {
  int x = 0, f = 1;
  char ch = 0;
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') f = -1;
  for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
  return x * f;
}
inline int ksm(int p, int q, int mo) {
  int res = 1;
  for (; q; q >>= 1, p = 1LL * p * p % mo)
    if (q & 1) res = 1LL * res * p % mo;
  return res;
}
int n, fr;
int a[(1 << 18) + 4];
long long ans;
int main() {
  n = read();
  for (int i = 1; i <= n; ++i) a[i] = read();
  for (int i = 1; i <= n && !fr; ++i)
    if (a[i] == -1) fr = i;
  int k = 0, t = 1;
  while (t != n) t <<= 1, k++;
  if (a[n] == -1) return puts("0"), 0;
  ans = a[n];
  int dq = n >> 1;
  priority_queue<int> q;
  for (int i = n - 1; i >= 0; i -= dq, dq >>= 1) {
    for (int j = 0; j < dq; ++j) {
      if (a[i - j] == -1)
        return printf("%lld\n", ans), 0;
      else
        q.push(-a[i - j]);
    }
    ans += -q.top();
    q.pop();
  }
}
