#include <bits/stdc++.h>
using namespace std;
const int maxn = 500005;
int res, nu;
long long fac[30], pre[30];
long long dp[maxn][30];
long long a[maxn], n;
void go(int i, int j) {
  long long Min = 1e18;
  if (j > 0) {
    if (dp[i + 1][j - 1] + a[i] < dp[i][j]) {
      dp[i][j] = dp[i + 1][j - 1] + a[i];
      Min = dp[i][j];
    }
  }
  int tmp = n - i - j;
  if (tmp <= pre[j]) {
    if (dp[i + 1][j] < dp[i][j]) dp[i][j] = dp[i + 1][j];
  }
}
void solve() {
  res = n;
  while (res != 1) {
    nu++;
    res /= 2;
  }
  for (int i = 1; i <= 25; i++) {
    fac[i] = (1 << (i - 1)) - 1;
  }
  for (int i = 1; i <= 25; i++) {
    pre[i] = pre[i - 1] + fac[nu + 1 - i];
  }
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= nu; j++) {
      if (i == n && j == 0) continue;
      dp[i][j] = 1e18;
    }
  int flag = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (a[i] == -1) {
      flag = i;
      break;
    }
    for (int j = 0; j <= nu; j++) {
      go(i, j);
    }
  }
  long long ans = 1e18;
  for (int i = nu; i >= max(0, nu - flag); i--) {
    ans = min(ans, dp[flag + 1][i]);
  }
  cout << ans << endl;
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) scanf("%I64d", &a[i]);
  solve();
  return 0;
}
