import sys

rl = sys.stdin.readline

N = int(rl())

S = rl()

Q = int(rl())

ks = list(map(int,rl().split()))



ans = []

for k in ks:

    d = m = 0

    dm = dmc = 0

    for i in range(N):

        if i >= k:

            if S[i-k] == 'D':

                d -= 1

                dm -= m

            elif S[i-k] == 'M':

                m -= 1

        if S[i] == 'D':

            d += 1

        elif S[i] == 'M':

            m += 1

            dm += d

        elif S[i] == 'C':

            dmc += dm

    ans.append(dmc)



print(*ans,sep='\n')

import sys

input = sys.stdin.readline

N = int(input())

S = input()

Q = int(input())

K = list(map(int,input().split()))



ans = []

for k in K:

    d = m = dm = dmc = 0

    for i in range(N):

        if i >= k:

            if S[i-k] == 'D':

                d -= 1

                dm -= m

            elif S[i-k] == 'M':

                m -= 1

        if S[i] == 'D':

            d += 1

        elif S[i] == 'M':

            m += 1

            dm += d

        elif S[i] == 'C':

            dmc += dm

    ans.append(dmc)

print(*ans, sep='\n')

import sys

rl = sys.stdin.readline
N = int(rl())
S = rl()
Q = int(rl())
ks = list(map(int, rl().split()))
ans = []
for k in ks:
    d = m = 0
    dm = dmc = 0
    for i in range(N):
        if i >= k:
            if S[i - k] == "D":
                d -= 1
                dm -= m
            elif S[i - k] == "M":
                m -= 1
        if S[i] == "D":
            d += 1
        elif S[i] == "M":
            m += 1
            dm += d
        elif S[i] == "C":
            dmc += dm
    ans.append(dmc)
print(*ans, sep="\n")

import sys

input = sys.stdin.readline
N = int(input())
S = input()
Q = int(input())
K = list(map(int, input().split()))
ans = []
for k in K:
    d = m = dm = dmc = 0
    for i in range(N):
        if i >= k:
            if S[i - k] == "D":
                d -= 1
                dm -= m
            elif S[i - k] == "M":
                m -= 1
        if S[i] == "D":
            d += 1
        elif S[i] == "M":
            m += 1
            dm += d
        elif S[i] == "C":
            dmc += dm
    ans.append(dmc)
print(*ans, sep="\n")

-rl = sys.stdin.readline -N = int(rl()) -S = rl() -Q = int(rl()) -ks = list(map(int, rl().split())) +input = sys.stdin.readline +N = int(input()) +S = input() +Q = int(input()) +K = list(map(int, input().split())) -for k in ks: -    d = m = 0 -    dm = dmc = 0 +for k in K: +    d = m = dm = dmc = 0