#include <bits/stdc++.h>
using namespace std;
long long n;
string s;
long long q;
long long pr[1500000];
vector<pair<long long, long long> > keks;
long long prr[1500000];
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> s;
  for (long long i = 0; i < s.size(); i++) {
    if (i) pr[i] = pr[i - 1];
    pr[i] += s[i] == 'M';
    if (s[i] == 'C') keks.push_back({i, pr[i]});
  }
  for (long long i = 0; i < keks.size(); i++) {
    if (i) prr[i] = prr[i - 1];
    prr[i] += keks[i].second;
  }
  cin >> q;
  for (long long i = 0; i < q; i++) {
    long long k = 0;
    cin >> k;
    long long ans = 0;
    long long first = -1, l2 = -1;
    for (long long j = 0; j < s.size(); j++) {
      if (s[j] == 'D') {
        while (first + 1 < keks.size() && keks[first + 1].first < j + k)
          first++;
        while (l2 + 1 < keks.size() && keks[l2 + 1].first < j) l2++;
        ans += prr[first] - (l2 >= 0 ? prr[l2] : 0) - (first - l2) * pr[j];
      }
    }
    cout << ans << "\n";
  }
}
