#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
queue<long long> q;
long long n, Q;
string str;
long long cnt, res, ans;
long long k, x[N];
int main() {
  cin >> n >> str >> Q;
  for (long long op = 0; op < Q; op++) cin >> x[op];
  for (long long op = 0; op < Q; op++) {
    k = x[op] - 1;
    cnt = ans = res = 0;
    while (!q.empty()) q.pop();
    for (long long i = 0; i < n; i++) {
      if (str[i] == 'D') q.push(cnt);
      if (str[i] == 'M') cnt++, res += q.size();
      if (str[i] == 'C') ans += res;
      if (i >= k) {
        int l = i - k;
        if (str[l] == 'D') {
          res -= (cnt - q.front());
          q.pop();
        }
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}
