#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e6 + 10;
const int MAX = 1000;
const int inf = 1e9;
const int mod = 1e9 + 7;
int n, m;
int a[maxn];
signed main() {
  std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n;
  int pos;
  for (int i = (1); i <= (n); ++i) {
    cin >> a[i];
    if (a[i] == -1) pos = i, a[i] = inf;
  }
  long long ans = 0;
  m = n;
  priority_queue<int, vector<int>, greater<int> > q;
  q.push(a[n]);
  while (1) {
    if (pos == m) {
      cout << (ans) << '\n';
      return 0;
    }
    ans += q.top();
    q.pop();
    if (m / 2 + 1 <= pos && pos <= m) {
      cout << (ans) << '\n';
      return 0;
    }
    for (int i = (m / 2); i <= (m - 1); ++i) q.push(a[i]);
    m /= 2;
  }
}
