N = int(eval(input()))

S = eval(input())

Q = int(eval(input()))

*k, = list(map(int, input().split()))



acd = [0]*(N+1)

acm = [0]*(N+1)

acc = [0]*(N+1)

acmc = [0]*(N+2)



for i in range(1, N+1):

    acd[i] = acd[i-1] + (S[i-1]=="D")

    acm[i] = acm[i-1] + (S[i-1]=="M")

    acc[i] = acc[i-1] + (S[i-1]=="C")



ans = 0

for i in range(1, N+1):

    if S[i-1]=="M":

        ans += acd[i-1]*(acc[N]-acc[i])



for i in range(1, N)[::-1]:

    acmc[i] += acmc[i+1]

    if S[i-1]=="M":

        acmc[i] += acc[N]-acc[i]



for i in k:

    v = 0

    for j in range(1, N+1):

        if S[j-1]=="D":

            v += (acm[min(N, i+j-1)]-acm[j]) * (acc[N]-acc[min(N, i+j-1)]) + acmc[min(i+j, N)]

    print((ans-v))
import sys

input=sys.stdin.readline





def solve():

    N = int(eval(input()))

    S = eval(input())

    Q = int(eval(input()))

    *k, = list(map(int, input().split()))



    acd = [0]*(N+1)

    acm = [0]*(N+1)

    acc = [0]*(N+1)

    acmc = [0]*(N+2)



    for i in range(1, N+1):

        acd[i] = acd[i-1] + (S[i-1]=="D")

        acm[i] = acm[i-1] + (S[i-1]=="M")

        acc[i] = acc[i-1] + (S[i-1]=="C")



    ans = 0

    for i in range(1, N+1):

        if S[i-1]=="M":

            ans += acd[i-1]*(acc[N]-acc[i])



    for i in range(1, N)[::-1]:

        acmc[i] += acmc[i+1]

        if S[i-1]=="M":

            acmc[i] += acc[N]-acc[i]



    for i in k:

        v = 0

        for j in range(1, N+1):

            if S[j-1]=="D":

                v += (acm[min(N, i+j-1)]-acm[j]) * (acc[N]-acc[min(N, i+j-1)]) + acmc[min(i+j, N)]

        print((ans-v))





if __name__ == "__main__":

    solve()

N = int(eval(input()))
S = eval(input())
Q = int(eval(input()))
(*k,) = list(map(int, input().split()))
acd = [0] * (N + 1)
acm = [0] * (N + 1)
acc = [0] * (N + 1)
acmc = [0] * (N + 2)
for i in range(1, N + 1):
    acd[i] = acd[i - 1] + (S[i - 1] == "D")
    acm[i] = acm[i - 1] + (S[i - 1] == "M")
    acc[i] = acc[i - 1] + (S[i - 1] == "C")
ans = 0
for i in range(1, N + 1):
    if S[i - 1] == "M":
        ans += acd[i - 1] * (acc[N] - acc[i])
for i in range(1, N)[::-1]:
    acmc[i] += acmc[i + 1]
    if S[i - 1] == "M":
        acmc[i] += acc[N] - acc[i]
for i in k:
    v = 0
    for j in range(1, N + 1):
        if S[j - 1] == "D":
            v += (acm[min(N, i + j - 1)] - acm[j]) * (
                acc[N] - acc[min(N, i + j - 1)]
            ) + acmc[min(i + j, N)]
    print((ans - v))

import sys

input = sys.stdin.readline


def solve():
    N = int(eval(input()))
    S = eval(input())
    Q = int(eval(input()))
    (*k,) = list(map(int, input().split()))
    acd = [0] * (N + 1)
    acm = [0] * (N + 1)
    acc = [0] * (N + 1)
    acmc = [0] * (N + 2)
    for i in range(1, N + 1):
        acd[i] = acd[i - 1] + (S[i - 1] == "D")
        acm[i] = acm[i - 1] + (S[i - 1] == "M")
        acc[i] = acc[i - 1] + (S[i - 1] == "C")
    ans = 0
    for i in range(1, N + 1):
        if S[i - 1] == "M":
            ans += acd[i - 1] * (acc[N] - acc[i])
    for i in range(1, N)[::-1]:
        acmc[i] += acmc[i + 1]
        if S[i - 1] == "M":
            acmc[i] += acc[N] - acc[i]
    for i in k:
        v = 0
        for j in range(1, N + 1):
            if S[j - 1] == "D":
                v += (acm[min(N, i + j - 1)] - acm[j]) * (
                    acc[N] - acc[min(N, i + j - 1)]
                ) + acmc[min(i + j, N)]
        print((ans - v))


if __name__ == "__main__":
    solve()

-N = int(eval(input())) -S = eval(input()) -Q = int(eval(input())) -(*k,) = list(map(int, input().split())) -acd = [0] * (N + 1) -acm = [0] * (N + 1) -acc = [0] * (N + 1) -acmc = [0] * (N + 2) -for i in range(1, N + 1): -    acd[i] = acd[i - 1] + (S[i - 1] == "D") -    acm[i] = acm[i - 1] + (S[i - 1] == "M") -    acc[i] = acc[i - 1] + (S[i - 1] == "C") -ans = 0 -for i in range(1, N + 1): -    if S[i - 1] == "M": -        ans += acd[i - 1] * (acc[N] - acc[i]) -for i in range(1, N)[::-1]: -    acmc[i] += acmc[i + 1] -    if S[i - 1] == "M": -        acmc[i] += acc[N] - acc[i] -for i in k: -    v = 0 -    for j in range(1, N + 1): -        if S[j - 1] == "D": -            v += (acm[min(N, i + j - 1)] - acm[j]) * ( -                acc[N] - acc[min(N, i + j - 1)] -            ) + acmc[min(i + j, N)] -    print((ans - v)) +import sys + +input = sys.stdin.readline + + +def solve(): +    N = int(eval(input())) +    S = eval(input()) +    Q = int(eval(input())) +    (*k,) = list(map(int, input().split())) +    acd = [0] * (N + 1) +    acm = [0] * (N + 1) +    acc = [0] * (N + 1) +    acmc = [0] * (N + 2) +    for i in range(1, N + 1): +        acd[i] = acd[i - 1] + (S[i - 1] == "D") +        acm[i] = acm[i - 1] + (S[i - 1] == "M") +        acc[i] = acc[i - 1] + (S[i - 1] == "C") +    ans = 0 +    for i in range(1, N + 1): +        if S[i - 1] == "M": +            ans += acd[i - 1] * (acc[N] - acc[i]) +    for i in range(1, N)[::-1]: +        acmc[i] += acmc[i + 1] +        if S[i - 1] == "M": +            acmc[i] += acc[N] - acc[i] +    for i in k: +        v = 0 +        for j in range(1, N + 1): +            if S[j - 1] == "D": +                v += (acm[min(N, i + j - 1)] - acm[j]) * ( +                    acc[N] - acc[min(N, i + j - 1)] +                ) + acmc[min(i + j, N)] +        print((ans - v)) + + +if __name__ == "__main__": +    solve()