#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#define lol(i,n) for(int i=0;i<n;i++)
#define mod 1000000007
typedef long long ll;

using namespace std;
#define N 1000010
string s;
ll n,q,cs[N];
vector<ll> c;
void Solve(ll k){
    ll ans=0,l=0,r=0,cur=0,cnt=0;
    lol(i,n){
	for(;l<c.size()&&c[l]<i;l++){
	    cnt--;
	}
	for(;r<c.size()&&c[r]<i+k;r++){
	    cur+=max((ll)0,cs[c[r]+1]-cs[i]);
	    cnt++;
	}
	if(s[i]=='M')cur-=cnt;
	if(s[i]=='D')ans+=cur;
    }
    cout<<ans<<endl;
}
int main(){
    cin>>n>>s>>q;
    cs[0]=0;
    lol(i,n)cs[i+1]=cs[i]+(s[i]=='M');
    lol(i,n)if(s[i]=='C')c.push_back(i);
    
    lol(i,q){
	ll k;cin>>k;
	Solve(k);
    }
    return 0;
}
