#include <bits/stdc++.h>
using namespace std;
long long temp[4000000];
long long v[4000000];
long long vr[4000000];
long long cur[4000000];
long long a[2000000];
long long ttemp[4000000];
void mrg(int l, int r, int numb, int qq) {
  if (r == l) {
    v[numb] = 0;
    vr[numb] = 0;
    return;
  }
  long long ans = 0;
  mrg(l, (l + r) / 2, 2 * numb, qq);
  mrg((l + r) / 2 + 1, r, 2 * numb + 1, qq);
  int uk1 = l;
  int uk2 = (l + r) / 2 + 1;
  int uk = l;
  while (uk1 <= (l + r) / 2 && uk2 <= r) {
    if (temp[uk1] <= temp[uk2]) {
      ttemp[uk] = temp[uk1];
      uk++;
      uk1++;
    } else {
      ttemp[uk] = temp[uk2];
      uk++;
      uk2++;
      ans += (l + r) / 2 - uk1 + 1;
    }
  }
  while (uk1 <= (l + r) / 2) {
    ttemp[uk] = temp[uk1];
    uk++;
    uk1++;
  }
  while (uk2 <= r) {
    ttemp[uk] = temp[uk2];
    uk++;
    uk2++;
  }
  for (int i = l; i <= r; i++) temp[i] = ttemp[i];
  if (qq)
    v[numb] += ans;
  else
    vr[numb] += ans;
  return;
}
int n;
void getans(int vv, int lvl, int need) {
  if (lvl < 0) return;
  temp[lvl] += v[vv];
  ttemp[lvl] += vr[vv];
  getans(2 * vv, lvl - 1, need);
  getans(2 * vv + 1, lvl - 1, need);
  return;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= (1 << n); i++) scanf("%d", &a[i]);
  for (int i = 1; i <= (1 << (n + 1)); i++) {
    cur[i] = 1;
  }
  for (int i = 1; i <= (1 << n); i++) temp[i] = a[i];
  mrg(1, (1 << n), 1, 1);
  for (int i = 1; i <= (1 << n); i++) temp[i] = a[(1 << n) - i + 1];
  mrg(1, (1 << n), 1, 0);
  for (int i = 1; i <= (1 << n); i++) {
    temp[i] = 0;
    ttemp[i] = 0;
  }
  getans(1, n, 0);
  int m;
  scanf("%d", &m);
  for (int i = 0; i < m; i++) {
    int z;
    long long sum = 0;
    scanf("%d", &z);
    for (int j = 0; j <= n; j++) {
      if (j <= z) cur[j] = 1 - cur[j];
      if (cur[j])
        sum += temp[j];
      else
        sum += ttemp[j];
    }
    printf("%I64d\n", sum);
  }
  return 0;
}
