#include<iostream>
#include<string>

using namespace std;

typedef long long ll;

int main() {
	int n;
	string s;
	cin >> n >> s;

	int q;
	cin >> q;
	while (q--) {
		int k;
		cin >> k;

		ll ans = 0;
		ll cm = 0, cc = 0;
		int left = 0, right = 0;
		ll plus = 0;
		for (; left < n; left++) {
			while (right < n && right - left < k) {
				if (s[right] == 'M')cm++;
				if (s[right] == 'C')plus += cm, cc++;
				right++;
			}

			if (s[left] == 'D')ans += plus;

			if (s[left] == 'M')plus -= cc, cm--;
			if (s[left] == 'C')cc--;
		}
		cout << ans << endl;
	}

	return 0;
}
#include<iostream>
#include<algorithm>
using namespace std;
/*
void swap(int &a,int &b){
	int tmp=a;
	a=b;
	b=tmp;
}*/


int partition(int A[],int p,int r){
	
	int x=A[r-1];
	int i=p-1;


	for(int j=p;j<r;j++){
		if(A[j]<=x){
			i++;
		if(i>=0&&j>=0)
		swap(A[i],A[j]);	
		}
		//swap(A[i],A[r-1]);
	}

	//swap(A[i],A[r-1]);
	return i;

}



int main(){
	int tmp[1000000];
	int n;
	cin>>n;

	for(int i=0;i<n;i++){
		cin>>tmp[i];
	}

	int q;

	q=partition(tmp,0,n);
	
	for(int i=0;i<n;i++){
		
		if(i==q){
			cout<<"["<<tmp[i]<<"]";
		}
		
		else cout<<tmp[i];

		if(i==n-1)cout<<endl;
		else cout<<" ";
	}

	return 0;
}