#include <bits/stdc++.h>   using namespace std;   const int maxn = 21;   int a[1 << maxn], n;   long long ans[maxn][2];   void mergesort(int l, int r, int dep) {     if (r - l <= 1) return;     int m = (l + r) / 2;     mergesort(l, m, dep - 1);     mergesort(m, r, dep - 1);     for (int i = l; i < m; i++) {       ans[dep][0] += lower_bound(a + m, a + r, a[i]) - a - m;     }     for (int i = m; i < r; i++) {       ans[dep][1] += lower_bound(a + l, a + m, a[i]) - a - l;     }     inplace_merge(a + l, a + m, a + r);   }   long long solve(int q) {     long long res = 0;     while (q) swap(ans[q][0], ans[q][1]), q--;     for (int i = 1; i <= n; i++) {       res += ans[i][0];     }     return res;   }   int main() {     int m, q;     cin >> n;     for (int i = 0; i < 1 << n; i++) scanf( %d , &a[i]);     cin >> m;     mergesort(0, 1 << n, n);     for (int i = 0; i < m; i++) {       scanf( %d , &q);       cout << solve(q) << endl;     }     return 0;   }   