#include <bits/stdc++.h>
using namespace std ;
#define ll long long 
#define f(i, x, n) for (int i = x; i < (int)(n); ++i)

int const N = 1000000;
char s[N + 1];
int n, q;

ll go(int k){
	ll an = 0, sm = 0;
	int b = 0;
	deque<pair<int, int> > dq;
	f(i, 0, n){
		if (s[i] == 'D')dq.push_back(make_pair(i, b)), sm += b;
		else if (s[i] == 'M')++b;
		else if (s[i] == 'C'){
			while (!dq.empty() && i - dq.front().first >= k)sm -= dq.front().second, dq.pop_front();
			an += (ll)b * dq.size() - sm;
		}
	}
	return an;
}

int main(){
	scanf("%d%s%d", &n, s, &q);
	f(i, 0, q){
		int t;
		scanf("%d", &t);
		printf("%lld\n", go(t));
	}
}