#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int i, n, ans = 0, capacity;
  cin >> n;
  capacity = n / 2;
  vector<long long int> a(n);
  for (i = 0; i < n; i++) cin >> a[i];
  priority_queue<long long int, vector<long long int>, greater<long long int>>
      pq;
  if (a[n - 1] == -1) {
    cout << ans;
    return 0;
  }
  ans = a[n - 1];
  i = n - 2;
  while (a[i] != -1) {
    pq.push(a[i]);
    if (pq.size() >= capacity) {
      n /= 2;
      capacity += n / 2 - 1;
      ans += pq.top();
      pq.pop();
    }
    i--;
  }
  cout << ans;
}
