import sys

read = sys.stdin.buffer.read

readline = sys.stdin.buffer.readline

readlines = sys.stdin.buffer.readlines



import numpy as np



N = int(readline())

S = np.frombuffer(b'-'+readline().rstrip(),'S1')

Q = int(readline())

query = list(map(int,read().split()))



isD = S == b'D'

isM = S == b'M'

isC = S == b'C'



cumD = isD.cumsum(dtype=np.int64)

cumM = isM.cumsum(dtype=np.int64)



def F(K):

    x = cumD.copy(); x[K:] -= cumD[:-K]

    x *= isM

    x[K+1:] -= isD[1:-K] * (cumM[K:-1] - cumM[:-K-1])

    return (x.cumsum() * isC).sum()



print(('\n'.join(str(F(K)) for K in query)))
from numpy import *

I=input

N=int(I())

S=array(list('-'+I()))

Q=int(I())

query = list(map(int,I().split()))

D=S=='D';M=S=='M';C=S=='C'

d=D.cumsum(dtype=int64);m=M.cumsum(dtype=int64)

for K in query:

  x=d.copy();x[K:]-=d[:-K];x*=M

  x[K+1:]-=D[1:-K]*(m[K:-1]-m[:-K-1])

  print((sum(cumsum(x)*C)))
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
import numpy as np

N = int(readline())
S = np.frombuffer(b"-" + readline().rstrip(), "S1")
Q = int(readline())
query = list(map(int, read().split()))
isD = S == b"D"
isM = S == b"M"
isC = S == b"C"
cumD = isD.cumsum(dtype=np.int64)
cumM = isM.cumsum(dtype=np.int64)


def F(K):
    x = cumD.copy()
    x[K:] -= cumD[:-K]
    x *= isM
    x[K + 1 :] -= isD[1:-K] * (cumM[K:-1] - cumM[: -K - 1])
    return (x.cumsum() * isC).sum()


print(("\n".join(str(F(K)) for K in query)))

from numpy import *

I = input
N = int(I())
S = array(list("-" + I()))
Q = int(I())
query = list(map(int, I().split()))
D = S == "D"
M = S == "M"
C = S == "C"
d = D.cumsum(dtype=int64)
m = M.cumsum(dtype=int64)
for K in query:
    x = d.copy()
    x[K:] -= d[:-K]
    x *= M
    x[K + 1 :] -= D[1:-K] * (m[K:-1] - m[: -K - 1])
    print((sum(cumsum(x) * C)))

-import sys +from numpy import * -read = sys.stdin.buffer.read -readline = sys.stdin.buffer.readline -readlines = sys.stdin.buffer.readlines -import numpy as np - -N = int(readline()) -S = np.frombuffer(b"-" + readline().rstrip(), "S1") -Q = int(readline()) -query = list(map(int, read().split())) -isD = S == b"D" -isM = S == b"M" -isC = S == b"C" -cumD = isD.cumsum(dtype=np.int64) -cumM = isM.cumsum(dtype=np.int64) - - -def F(K): -    x = cumD.copy() -    x[K:] -= cumD[:-K] -    x *= isM -    x[K + 1 :] -= isD[1:-K] * (cumM[K:-1] - cumM[: -K - 1]) -    return (x.cumsum() * isC).sum() - - -print(("\n".join(str(F(K)) for K in query))) +I = input +N = int(I()) +S = array(list("-" + I())) +Q = int(I()) +query = list(map(int, I().split())) +D = S == "D" +M = S == "M" +C = S == "C" +d = D.cumsum(dtype=int64) +m = M.cumsum(dtype=int64) +for K in query: +    x = d.copy() +    x[K:] -= d[:-K] +    x *= M +    x[K + 1 :] -= D[1:-K] * (m[K:-1] - m[: -K - 1]) +    print((sum(cumsum(x) * C)))