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

int main() {
	ios_base::sync_with_stdio(false);
	int n; string s; cin >> n >> s;
	vector<int> pre(n, 0);
	for (int i = 0; i < n; ++i) {
		pre[i] = (s[i] == 'M');
		if (i) pre[i] += pre[i - 1];
	}
	auto get = [&](int l,int r) {
		return pre[r] - (l ? pre[l - 1] : 0);
	};
	int q; cin >> q;
	while (q--) {
		int k; cin >> k;
		long long d = 0;
		long long dm = 0;
		long long dmc = 0;
		for (int i = 0; i < n; ++i) {
			if (i - k >= 0 && s[i - k] == 'D') {
				dm -= get(i - k + 1, i - 1);
				d--;
			}
			if (s[i] == 'D') d++;
			else if (s[i] == 'M') dm += d;
			else if (s[i] == 'C') dmc += dm;
		}
		cout << dmc << '\n';
	}
}