#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int Maxn = 1000005;

int n;
char S[Maxn];
int q;
int k;

int main()
{
    scanf("%d", &n);
    scanf("%s", S);
    scanf("%d", &q);
    while (q--) {
        scanf("%d", &k);
        ll res = 0, cur = 0;
        int D = 0, M = 0;
        int l = 0;
        for (int r = 0; r < n; r++) {
            if (r - l + 1 > k) {
                if (S[l] == 'D') { cur -= M; D--; }
                else if (S[l] == 'M') M--;
                l++;
            }
            if (S[r] == 'D') D++;
            else if (S[r] == 'M') { cur += D; M++; }
            else if (S[r] == 'C') res += cur;
        }
        printf("%lld\n", res);
    }
    return 0;
}
