#include <bits/stdc++.h>
using namespace std;
class Solution {
 public:
  long long solve(vector<int>& A) {
    int n = A.size();
    int F = 0;
    for (int i = 0; i < n; ++i) {
      if (A[i] == -1) {
        F = i;
        break;
      }
    }
    if (F == n - 1) {
      return 0;
    }
    int m = 0;
    while (1 << m < n) {
      ++m;
    }
    vector<int> sums(m + 1, 0);
    for (int i = 0; i < m; ++i) {
      sums[i + 1] = sums[i] + (1 << (m - 1 - i));
    }
    const long long INF = 1LL << 60;
    vector<long long> dp(m + 1, INF);
    vector<long long> ndp(dp);
    dp[0] = 0;
    for (int i = n - 1, cnt = 1; i > F; --i, ++cnt) {
      fill(ndp.begin(), ndp.end(), INF);
      for (int j = m; j >= 0; --j) {
        if (j + 1 <= m) {
          ndp[j + 1] = min(ndp[j + 1], dp[j] + A[i]);
        }
        if (sums[j] >= cnt) {
          ndp[j] = min(ndp[j], dp[j]);
        }
      }
      swap(ndp, dp);
    }
    auto res = *min_element(dp.begin(), dp.end());
    return res;
  }
};
int main(int argc, char** argv) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  Solution sol;
  int n;
  cin >> n;
  vector<int> A(n);
  for (int i = 0; i < n; ++i) {
    cin >> A[i];
  }
  cout << sol.solve(A) << "\n";
  return 0;
}
