#include <bits/stdc++.h>
using namespace std;
#define rep2(i, m, n) for(int i=int(m); i<int(n); ++i)
#define rep(i, n) rep2(i, 0, n)
using ll = long long;
using V = vector<int>;

char s[1000000];
ll t[3][1000001];

int main() {
  int n, q;
  scanf("%d%s%d", &n, s, &q);

  V D, C;
  rep(i, n) {
    rep(j, 3) t[j][i+1] = t[j][i];
    switch(s[i]){
      case 'D':
        ++t[0][i+1];
        D.push_back(i+1);
        break;
      case 'M':
        ++t[1][i+1];
        break;
      case 'C':
        ++t[2][i+1];
        C.push_back(i);
        break;
    }
  }

  ll sa1 = 0, sc1 = 0;
  for (auto &a : D) sa1 += t[1][a] * t[2][a];
  for (auto &c : C) sc1 += t[1][c] * t[0][c];

  rep(_, q) {
    int k; 
    scanf("%d", &k);
    ll ans = sa1 + sc1;
    ll sa2 = 0, sc2 = 0;
    for (auto &a : D) {
      if (a+k-1 < n) ans -= t[1][a] * t[2][a+k-1];
      else sa2 += t[1][a];
    }
    for (auto &c : C) {
      if (c-k+1 > 0) ans -= t[1][c] * t[0][c-k+1];
      else sc2 += t[1][c];
    }
    ans -= sa2 * t[2][n] + sc2 * t[0][0];
    printf("%lld\n", ans);
  }
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
/* typedef */
typedef long long ll;
typedef pair<int, int> pii;
/* constant */
const int INF = 1 << 30;
const ll LINF = 1LL << 50;
const int NIL = -1;
const int MAX = 10000;
const int mod = 1000000007;
const double pi = 3.141592653589;
/* global variables */
/* function */
/* main */
int main(){
    int N, Q;
    string S;
    cin >> N >> S >> Q;
    vector<int> K(Q);
    for (int i = 0; i < Q; i++) cin >> K[i];

    vector<int> sum(N+1);
    for (int k : K) {
        ll D = 0, M = 0, ans = 0;
        ll cntDM = 0;
        for (int i = 0; i < S.size(); i++) {
            if (S[i] == 'D') D++;
            else if (S[i] == 'M') {
                M++;
                cntDM += D;
            }
            else if (S[i] == 'C') {
                int l = max(i - k, 0), r = i + 1;
                ans += cntDM;
            }
            if (i >= k - 1) {
                if (S[i - (k - 1)] == 'D') {
                    D--;
                    cntDM -= M;
                }
                else if (S[i - (k - 1)] == 'M') M--;
            }
        }
        cout << ans << '\n';
    }
}
