#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Pll = pair<ll, ll>;
using Pii = pair<int, int>;
constexpr ll MOD = 1000000007;
constexpr long double EPS = 1e-10;
constexpr int dyx[4][2] = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}};
constexpr ll INF = 1LL << 60;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  vector<ll> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  int n_need_win = 0;
  while (1 << n_need_win < n) ++n_need_win;
  int n_max_win = -1;
  for (int i = 0; i < n; ++i) {
    if (i + 1 == 1 << (n_max_win + 1)) {
      ++n_max_win;
    }
    if (a[i] == -1) break;
  }
  cerr << n_max_win << " " << n_need_win << endl;
  if (n_max_win == n_need_win) {
    cout << 0 << endl;
    return 0;
  }
  int n_bribe = n_need_win - n_max_win;
  int next_checkpoint = n - 1;
  priority_queue<ll, vector<ll>, greater<ll>> que;
  ll ans = 0LL;
  for (int i = n - 1; i >= 0 && n_bribe > 0; --i) {
    que.push(a[i]);
    if (i == next_checkpoint) {
      ans += que.top();
      que.pop();
      next_checkpoint = ((next_checkpoint + 1) >> 1) - 1;
      --n_bribe;
    }
  }
  cout << ans << endl;
}
