#include <bits/stdc++.h>
using namespace std;
int a[1 << 19];
bool used[1 << 19];
int f;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] == -1) f = i;
  }
  long long int res = 0;
  for (int k = 64 - __builtin_clzll(n) - 1; k >= 1; k--) {
    if (f >= (1 << k)) {
      cout << res << endl;
      return 0;
    }
    int minCost = 1000000000 + 1;
    int ind = -1;
    for (int i = (1 << k); i <= n; i++) {
      if (!used[i] && minCost > a[i]) {
        minCost = a[i];
        ind = i;
      }
    }
    used[ind] = 1;
    res += minCost;
  }
  cout << res << endl;
  return 0;
}
