#include <bits/stdc++.h>
using namespace std;
string char_to_str(char c) {
  string tem(1, c);
  return tem;
}
struct node {
  int m = 0;
  int c = 0;
  long long to = 0;
};
node createseg(node seg[], string s, int l, int r, int i) {
  if (l == r) {
    seg[i].m = 0;
    seg[i].c = 0;
    seg[i].to = 0;
    if (s[l] == 'M') {
      seg[i].m++;
    }
    if (s[l] == 'C') {
      ++seg[i].c;
    }
    return seg[i];
  } else {
    int m = (l + r) / 2;
    node fi = createseg(seg, s, l, m, 2 * i);
    node se = createseg(seg, s, m + 1, r, 2 * i + 1);
    seg[i].m = fi.m + se.m;
    seg[i].c = fi.c + se.c;
    seg[i].to = fi.to + se.to + fi.m * ((long long)se.c);
    return seg[i];
  }
}
node ansseg(node seg[], string s, int l, int r, int i, int lq, int rq) {
  if (l == lq && r == rq) {
    return seg[i];
  } else {
    int m = (l + r) / 2;
    if (rq <= m) {
      node fi = ansseg(seg, s, l, m, 2 * i, lq, rq);
      return fi;
    }
    if (lq > m) {
      node se = ansseg(seg, s, m + 1, r, 2 * i + 1, lq, rq);
      return se;
    }
    node fi = ansseg(seg, s, l, m, 2 * i, lq, m);
    node se = ansseg(seg, s, m + 1, r, 2 * i + 1, m + 1, rq);
    node k;
    k.m = fi.m + se.m;
    k.c = fi.c + se.c;
    k.to = fi.to + se.to + fi.m * ((long long)se.c);
    return k;
  }
}
node seg[4000000];
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  int n;
  cin >> n;
  string s;
  cin >> s;
  int q;
  cin >> q;
  while (q) {
    --q;
    int k;
    cin >> k;
    long long ans = 0;
    node te;
    for (int i = 0; i < n - 1; ++i) {
      if (s[i] == 'D') {
        ans += te.to;
      }
      if (s[i + 1] == 'M') {
        te.to -= te.c;
        te.m--;
      }
      if (s[i + 1] == 'C') {
        te.c--;
      }
      if (i + k < n) {
        if (s[i + k] == 'M') {
          te.m++;
        }
        if (s[i + k] == 'C') {
          te.to += te.m;
          te.c++;
        }
      }
    }
    cout << ans << "\n";
  }
}
