#include <bits/stdc++.h>
using namespace std;
const long long int N = (1 << 18) + 5, mod = 31, bit = 60;
signed main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  long long int n, m, k, t, pro = 1, temp, i, j, l, r, x, y, z, mid, rem,
                            carry = 0, ind, ans = 0, mx = -LONG_LONG_MAX,
                            mn = LONG_LONG_MAX, cnt = 0, curr = 0, prev, next,
                            sum = 0, i1 = -1, i2 = -1;
  bool flag;
  cin >> n;
  long long int a[n + 1];
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] == -1) {
      for (j = 1; j <= i; j++) {
        a[j] = 0;
      }
    }
  }
  priority_queue<long long int, vector<long long int>, greater<long long int>>
      pq;
  for (i = n; i >= 1; i--) {
    pq.push(a[i]);
    if ((i & (i - 1)) == 0) {
      ans += pq.top();
      pq.pop();
    }
  }
  cout << ans << '\n';
  return 0;
}
