#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
priority_queue<pair<int, int> > p;
int n;
const int maxn = 1 << 19;
int vis[maxn];
long long dp[maxn][19];
int a[maxn], pos, L, sum;
long long ans;
int main() {
  scanf("%d", &n);
  memset(dp, -1, sizeof(dp));
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    if (a[i] == -1) pos = i;
    if (!pos) a[i] = 0;
  }
  a[pos] = 0;
  int tmp = n;
  for (L = 0; 1 << L <= n; L++)
    ;
  for (int j = 1; j <= L; j++) {
    long long Mi = inf;
    if (j == 1) Mi = 0;
    for (int i = n; i > n - j + 1; i--)
      if (dp[i][j - 1] != -1) Mi = min(Mi, dp[i][j - 1]);
    for (int i = n - j + 1; i >= n - sum; i--) {
      dp[i][j] = Mi + a[i];
      if (dp[i][j - 1] != -1) Mi = min(Mi, dp[i][j - 1]);
    }
    sum += tmp / 2;
    tmp /= 2;
  }
  long long ans = inf;
  for (int i = 1; i <= n; i++)
    if (dp[i][L] != -1) ans = min(ans, dp[i][L]);
  cout << ans << '\n';
}
