#include<cstdio>
#include<iostream>
using namespace std;
typedef long long int ll;
const int MAXN=1e6+5;
int n,q,k;
char s[MAXN];
int main(){
	scanf("%d\n%s%d",&n,s+1,&q);
	while(q--){
		scanf("%d",&k);
		ll ans=0,dnum=0,mnum=0,pairnum=0;
		for(int i=1;i<=k;++i){
			if(s[i]=='D') ++dnum;
			if(s[i]=='M') {++mnum;pairnum+=dnum;}
			if(s[i]=='C') ans+=pairnum;
		}
		for(int i=k+1;i<=n;++i){
			if(s[i-k]=='D') {--dnum;pairnum-=mnum;}
			if(s[i-k]=='M') --mnum;
			if(s[i]=='D') ++dnum;
			if(s[i]=='M') {++mnum;pairnum+=dnum;}
			if(s[i]=='C') ans+=pairnum;
		}
		printf("%lld\n",ans);
	}
	return 0;
}