#include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
using namespace std;
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  vector<long long> arr(n);
  long long me = 0;
  for (long long i = 0; i < n; ++i) {
    cin >> arr[i];
    if (arr[i] == -1) {
      me = i;
    }
  }
  for (long long i = 0; i <= me; ++i) {
    arr[i] = 0;
  }
  long long i = n - 1;
  long long pw = n;
  priority_queue<long long> q;
  long long res = arr[n - 1];
  while (pw / 2 - 1 > me) {
    for (long long i = pw / 2 - 1; i + 1 < pw; ++i) {
      q.push(-arr[i]);
    }
    res += -q.top();
    q.pop();
    pw /= 2;
  }
  cout << res;
}
