#include <bits/stdc++.h>
using namespace std;
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}
long long int v[290000], n, ans = 0;
int main() {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> v[i];
  multiset<long long int> s;
  for (int i = n; i > 0; i--) {
    if (v[i] == -1)
      break;
    else {
      s.insert(v[i]);
      if ((i & i - 1) == 0) {
        ans += *s.begin();
        s.erase(s.begin());
      }
    }
  }
  cout << ans;
  return 0;
}
