import sys

def input():

	return sys.stdin.readline().strip()



N = int(eval(input()))

S = eval(input())

Q = int(eval(input()))

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



DM_total = [(0,0,0)]

C_list = []



for i in range(N):

	if S[i] == "D":

		DM_total.append((DM_total[-1][0] + 1, DM_total[-1][1], DM_total[-1][2]))

	elif S[i] == "M":

		DM_total.append((DM_total[-1][0], DM_total[-1][1] + 1, DM_total[-1][2] + DM_total[-1][0]))

	else:

		DM_total.append(DM_total[-1])

	if S[i] == "C":

		C_list.append(i)





for q in range(Q):

	ans = 0

	for c in C_list:

		if k[q] > c:

			ans += DM_total[c + 1][2]

		else:

			ans += DM_total[c + 1][2] - DM_total[c + 1 - k[q]][2]

			ans -= DM_total[c + 1 - k[q]][0] * (DM_total[c + 1][1] - DM_total[c + 1 - k[q]][1])



	print(ans)
import sys

def input():

	return sys.stdin.readline().strip()



N = int(eval(input()))

S = eval(input())

Q = int(eval(input()))

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



DM_total = [(0,0,0)]

C_list = []



for i in range(N):

	if S[i] == "D":

		DM_total.append((DM_total[-1][0] + 1, DM_total[-1][1], DM_total[-1][2]))

	elif S[i] == "M":

		DM_total.append((DM_total[-1][0], DM_total[-1][1] + 1, DM_total[-1][2] + DM_total[-1][0]))

	else:

		DM_total.append(DM_total[-1])

	if S[i] == "C":

		C_list.append(i)



ans_list = [0]*Q

for q in range(Q):

	for c in C_list:

		if k[q] > c:

			ans_list[q] += DM_total[c + 1][2]

		else:

			ans_list[q] += DM_total[c + 1][2] - DM_total[c + 1 - k[q]][2]

			ans_list[q] -= DM_total[c + 1 - k[q]][0] * (DM_total[c + 1][1] - DM_total[c + 1 - k[q]][1])



for q in range(Q):

	print((ans_list[q]))

import sys


def input():
    return sys.stdin.readline().strip()


N = int(eval(input()))
S = eval(input())
Q = int(eval(input()))
k = list(map(int, input().split()))
DM_total = [(0, 0, 0)]
C_list = []
for i in range(N):
    if S[i] == "D":
        DM_total.append((DM_total[-1][0] + 1, DM_total[-1][1], DM_total[-1][2]))
    elif S[i] == "M":
        DM_total.append(
            (DM_total[-1][0], DM_total[-1][1] + 1, DM_total[-1][2] + DM_total[-1][0])
        )
    else:
        DM_total.append(DM_total[-1])
    if S[i] == "C":
        C_list.append(i)
for q in range(Q):
    ans = 0
    for c in C_list:
        if k[q] > c:
            ans += DM_total[c + 1][2]
        else:
            ans += DM_total[c + 1][2] - DM_total[c + 1 - k[q]][2]
            ans -= DM_total[c + 1 - k[q]][0] * (
                DM_total[c + 1][1] - DM_total[c + 1 - k[q]][1]
            )
    print(ans)

import sys


def input():
    return sys.stdin.readline().strip()


N = int(eval(input()))
S = eval(input())
Q = int(eval(input()))
k = list(map(int, input().split()))
DM_total = [(0, 0, 0)]
C_list = []
for i in range(N):
    if S[i] == "D":
        DM_total.append((DM_total[-1][0] + 1, DM_total[-1][1], DM_total[-1][2]))
    elif S[i] == "M":
        DM_total.append(
            (DM_total[-1][0], DM_total[-1][1] + 1, DM_total[-1][2] + DM_total[-1][0])
        )
    else:
        DM_total.append(DM_total[-1])
    if S[i] == "C":
        C_list.append(i)
ans_list = [0] * Q
for q in range(Q):
    for c in C_list:
        if k[q] > c:
            ans_list[q] += DM_total[c + 1][2]
        else:
            ans_list[q] += DM_total[c + 1][2] - DM_total[c + 1 - k[q]][2]
            ans_list[q] -= DM_total[c + 1 - k[q]][0] * (
                DM_total[c + 1][1] - DM_total[c + 1 - k[q]][1]
            )
for q in range(Q):
    print((ans_list[q]))

+ans_list = [0] * Q -    ans = 0 -            ans += DM_total[c + 1][2] +            ans_list[q] += DM_total[c + 1][2] -            ans += DM_total[c + 1][2] - DM_total[c + 1 - k[q]][2] -            ans -= DM_total[c + 1 - k[q]][0] * ( +            ans_list[q] += DM_total[c + 1][2] - DM_total[c + 1 - k[q]][2] +            ans_list[q] -= DM_total[c + 1 - k[q]][0] * ( -    print(ans) +for q in range(Q): +    print((ans_list[q]))