#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 70;
const int INF = 0x3f3f3f3f;
const long long inf = 0x3f3f3f3f3f3f3f3f;
const int MOD = 1e9 + 7;
const double eps = 1e-7;
const double PI = acos(-1.0);
int n, m;
int a[maxn];
long long solve() {
  long long ans = 0;
  priority_queue<int, vector<int>, greater<int>> Q;
  for (int i = m; i >= 0; i--) {
    Q.push(a[1 << i]);
    if (Q.top() == -1) return ans;
    ans += Q.top();
    Q.pop();
    for (int j = (1 << i) - 1; j > (1 << (i - 1)); j--) Q.push(a[j]);
  }
}
int main() {
  while (cin >> n) {
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    m = 1;
    while ((1 << m) != n) m++;
    long long ans = solve();
    printf("%lld\n", ans);
  }
  return 0;
}
