#include <bits/stdc++.h>
using namespace std;
const int MAXN = (1 << 18);
long long MAXLL = 1234567898765432123;
int n;
int m;
long long f[MAXN];
long long g[MAXN];
int a[MAXN];
int ComputeH(int nn) {
  int h = 0;
  while (nn > 1) {
    nn /= 2;
    h++;
  }
  return h;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int c;
    scanf("%d", &c);
    if (c == -1) {
      m = i;
    }
    a[i] = c;
  }
  int h = ComputeH(n);
  for (int i = 0; i <= m; i++) {
    a[i] = 0;
  }
  for (int i = 0; i < n; i++) {
    g[i] = 0;
  }
  int minj_base = 2;
  for (int i = 1; i <= h; i++) {
    int minj = minj_base - 1;
    for (int j = 0; j < minj; j++) {
      f[j] = MAXLL;
    }
    for (int j = minj; j < n; j++) {
      f[j] = g[j - 1] + a[j];
    }
    g[0] = f[0];
    for (int j = 1; j < n; j++) {
      g[j] = min(f[j], g[j - 1]);
    }
    minj_base *= 2;
  }
  cout << f[n - 1] << endl;
}
