#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 7;
const long long INF = 1e18 + 7;
int n;
int a[maxn];
long long dp[maxn][20];
long long k[20];
long long C[20];
int main() {
  scanf("%d", &n);
  int res = n;
  int num = 0;
  while (res != 1) num++, res >>= 1;
  for (int i = 1; i <= num; i++) {
    k[i] = (1 << (i - 1)) - 1;
  }
  for (int i = 1; i <= num; i++) {
    C[i] = C[i - 1] + k[num + 1 - i];
  }
  for (int j = 0; j <= n; j++)
    for (int i = 0; i <= num; i++) dp[j][i] = INF;
  dp[n][0] = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  int flag = 0;
  for (long long i = n - 1; i >= 0; i--) {
    if (a[i] == -1) {
      flag = i;
      break;
    }
    for (long long j = 0; j <= num; j++) {
      if (j != 0) dp[i][j] = min(dp[i][j], dp[i + 1][j - 1] + a[i]);
      if (n - 1 - i + 1 - j > C[j]) continue;
      dp[i][j] = min(dp[i + 1][j], dp[i][j]);
    }
  }
  long long ans = INF;
  for (int i = num; i >= max(0, num - flag); i--) {
    ans = min(ans, dp[flag + 1][i]);
  }
  cout << ans << endl;
  return 0;
}
