#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 9;
const int inf = 0x3f3f3f3f;
int p, n, bi;
int a[maxn];
int cnt0(int val) {
  int ans(-1);
  while (val) {
    val >>= 1;
    ++ans;
  }
  return ans;
}
long long dp[maxn][30];
vector<int> ve;
int main() {
  ios::sync_with_stdio(false);
  int T(1), cas(0);
  while (T--) {
    cin >> n;
    bi = cnt0(n);
    for (int i = (1); i <= int(n); ++i) cin >> a[i];
    for (int i = (1); i <= int(n); ++i) {
      if (!p) {
        if (a[i] != -1)
          a[i] = 0;
        else
          p = i, a[i] = -1;
      } else
        a[i - 1] = a[i];
    }
    reverse(a + 1, a + 1 + n - 1);
    a[n] = -inf;
    long long ans = 0;
    int las = 0;
    for (int i = bi - 1; i >= 0; --i) {
      int ri;
      if (i == bi - 1)
        ve.push_back(a[1]), ri = 1, ans += a[1];
      else {
        ri = las + (1 << (i + 1));
        for (int j = (las + 1); j <= int(ri); ++j) ve.push_back(a[j]);
        auto from = ve.begin() + (bi - 1) - i;
        auto mi = min_element(from, ve.end());
        swap(*from, *mi);
        ans += *from;
      }
      las = ri;
    }
    cout << ans << '\n';
  }
  return 0;
}
