#include <bits/stdc++.h>   using namespace std;   const long long INF = (long long)1e15 + 696969;   long long n, k;   long long a[1LL << 20], b[1LL << 20];   long long opt[20][1LL << 20];   signed main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     memset(opt, -1, sizeof opt);     cin >> n;     for (long long i = 1; i < n; i <<= 1) k++;     long long ok = false;     for (long long i = 0; i < n; i++) {       long long x;       cin >> x;       if (x == -1)         ok = true;       else if (!ok)         a[i] = 0;       else         a[i - 1] = x;     }     n--;     reverse(a, a + n);     for (long long i = k - 1, j = 0; i > 0; i--) {       j += 1 << i;       b[i] = j;     }     for (long long i = 0; i < n; i++) {       opt[0][i] = a[i];     }     for (long long i = 1; i < k; i++) {       long long mn = INF;       for (long long j = n - 1; j >= 0; j--) {         if (j > b[i])           opt[i][j] = INF;         else {           opt[i][j] = mn + a[j];           mn = min(mn, opt[i - 1][j]);         }       }     }     cout << opt[k - 1][0] << endl;     return 0;   }   