#include <bits/stdc++.h>
using namespace std;
long long n, S, sum[100], a[1000007], f[22][1000007];
int main() {
  scanf("%lld", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%lld", &a[i]);
    if (a[i] == -1) S = i + 1;
  }
  for (int i = 1, j = n / 2; i <= 20; ++i, j /= 2) sum[i] = sum[i - 1] + j;
  for (int j = S; j <= n; ++j) {
    for (int i = log2(n); i >= 0; --i) {
      f[i][j] = f[i + 1][j - 1] + a[j];
      if (sum[i] >= n - j + 1) f[i][j] = min(f[i][j], f[i][j - 1]);
    }
  }
  printf("%lld\n", f[0][n]);
}
