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

void solve(long long N, std::string S, long long Q, std::vector<long long> k)
{

    for (ll i = 0; i < Q; i++) {
        ll D = 0;
        ll M = 0;
        ll DM = 0;
        ll DMC = 0;
        ll ans = 0;
        for (ll j = 0; j < N; j++) {
            if (j - k[i] >= 0) {
                if (S[j - k[i]] == 'D') {
                    D--;
                    DM -= M;
                }
                if (S[j - k[i]] == 'M') {
                    M--;
                }
            }
            if (S[j] == 'D') {
                D++;
            }
            if (S[j] == 'M') {
                M++;
                DM += D;
            }
            if (S[j] == 'C') {
                DMC += DM;
            }
            ans = max(ans, DMC);
        }
        cout << ans << "\n";
    }
}

// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
int main()
{
    long long N;
    scanf("%lld", &N);
    std::string S;
    std::cin >> S;
    long long Q;
    scanf("%lld", &Q);
    std::vector<long long> k(Q - 1 - 0 + 1);
    for (int i = 0; i < Q - 1 - 0 + 1; i++) {
        scanf("%lld", &k[i]);
    }
    solve(N, S, Q, std::move(k));
    return 0;
}

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

ll sum[3][1000005];
ll combiDM[1000005];

int main() {
    int N;
    cin >> N;
    string S;
    cin >> S;
    int Q;
    cin >> Q;
    vector<int> k(Q);
    rep(i, Q) cin >> k[i];

    rep(j, 3) rep(k, N) sum[j][k] = 0;
    combiDM[0] = 0;

    string T = "DMC";
    rep(i, N) {
        rep(j, 3) {
            if (S[i] == T[j]) ++sum[j][i];
        }
    }

    rep(i, 2) {
        rep(j, N - 1) {
            if (i == 1) {
                combiDM[j + 1] = combiDM[j] + sum[0][j + 1] * sum[1][j + 1];
            }
            sum[i][j + 1] += sum[i][j];
        }
    }

    rep(i, Q) {
        vector<ll> ans(N);
        rep(j, N) {
            if (j < 2) continue;
            ans[j] += ans[j - 1];

            if (sum[2][j] != 1) continue;
            ans[j] += combiDM[j];
            if (j - k[i] < 0) continue;
            ans[j] -= (combiDM[j - k[i]] +
                       sum[0][j - k[i]] * (sum[1][j - 1] - sum[1][j - k[i]]));
        }

        cout << ans[N - 1] << endl;
    }
}