#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
int T, n;
int a[1000010];
int main() {
  T = 1;
  while (T--) {
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
      scanf("%d", &a[i]);
    }
    int tail = n;
    priority_queue<int, vector<int>, greater<int>> que;
    que.push(a[tail]);
    int now = n / 2;
    long long ans = 0;
    while (!que.empty()) {
      int tp = que.top();
      if (tp == -1) break;
      que.pop();
      ans += tp;
      for (int i = 1; i <= now; ++i) que.push(a[--tail]);
      now /= 2;
    }
    printf("%lld\n", ans);
  }
  return 0;
}
