#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
const int MOD = 1000000007;
const long double EPS = 1e-9;
const long double PI = acos(-1);
int N;
long long br[1 << 19];
int str;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(10);
  cin >> N;
  for (int i = 0; i < N; i++) cin >> br[i];
  for (int i = 0; i < N; i++) {
    if (br[i] == -1) {
      str = i;
      break;
    }
    br[i] = 0;
  }
  vector<long long> res;
  for (int i = 0; i < N; i++)
    if (br[i] != -1) res.push_back(br[i]);
  int k = 0;
  while ((1 << k) < N) k++;
  priority_queue<long long, vector<long long>, greater<long long>> pq;
  pq.push(res.back());
  long long ans = 0;
  int pos = res.size() - 2;
  long long run = (1 << k);
  for (int i = 0; i < k; i++) {
    ans += pq.top();
    pq.pop();
    run >>= 1;
    for (int j = 0; j < run; j++) pq.push(res[pos--]);
  }
  cout << ans << "\n";
  return 0;
}
