#include <bits/stdc++.h>
using namespace std;
const long double eps = 1e-9;
const int MAXN = 1e4;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long n;
  cin >> n;
  long long t = 0;
  while ((1 << t) < n) ++t;
  vector<int> gg(n);
  for (long long i = 0; i < n; ++i) {
    cin >> gg[i];
  }
  reverse(gg.begin(), gg.end());
  long long ans = 0;
  multiset<int> ms;
  int k = n;
  for (int i = 0;;) {
    k /= 2;
    if (gg[i] == -1) {
      cout << ans << "\n";
      return 0;
    }
    ms.insert(gg[i++]);
    ans += *ms.begin();
    ms.erase(ms.begin());
    for (int j = 0; j < k - 1; ++j) {
      if (gg[i] == -1) {
        cout << ans << "\n";
        return 0;
      }
      ms.insert(gg[i++]);
    }
  }
  cout << ans << "\n";
  return 0;
}
