#include <bits/stdc++.h>
using namespace std;
int n, k;
long long ans, v[(1 << 18) + 10], dp[(1 << 18) + 10][20];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  int ok = 0;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    if (x == -1) ok = 1, k = n - i;
    if (ok && x > -1) v[n - i + 1] = x;
  }
  if (!k) {
    cout << 0 << '\n';
    return 0;
  }
  int curr_ant = 0, curr = 0, p = n / 2, nr = 0;
  while (p) {
    curr = min(curr + p, k);
    p /= 2;
    nr++;
    dp[nr][nr] = dp[nr - 1][nr - 1] + v[nr];
    for (int i = 1; i < nr; i++) dp[i][nr] = dp[i][nr - 1];
    for (int i = nr + 1; i <= curr; i++) {
      dp[i][nr] = dp[i - 1][nr];
      if (i - 1 <= curr_ant)
        dp[i][nr] = min(dp[i - 1][nr - 1] + v[i], dp[i][nr]);
    }
    curr_ant = curr;
  }
  ans = 2000000000000000000;
  for (int i = 1; i <= nr; i++)
    if (dp[k][i]) ans = min(ans, dp[k][i]);
  cout << ans << '\n';
  return 0;
}
