#include <bits/stdc++.h>
using namespace std;
int n, k, z;
int a[1 << 18];
long long dp[19][1 << 18];
long long f(int k, int i, int s) {
  if (k < 0) return 0;
  long long& r = dp[k][i];
  if (r >= 0) return r;
  r = a[i] + f(k - 1, i + 1, s + (1 << k));
  if (s > i) r = min(r, f(k, i + 1, s));
  return r;
}
int main() {
  scanf("%d", &n);
  while ((1 << z) < n) z++;
  for (int i = 0, ThxDem = n; i < ThxDem; ++i) {
    scanf("%d", a + i);
    if (a[i] < 0) k = i;
  }
  for (int i = 0, ThxDem = k; i < ThxDem; ++i) a[i] = 0;
  for (int i = k, ThxDem = n - 1; i < ThxDem; ++i) a[i] = a[i + 1];
  reverse(a, a + n - 1);
  memset(dp, -1, sizeof(dp));
  printf("%lld\n", f(z - 1, 0, 0));
  return 0;
}
