import numpy as np

I=input

N = int(I())

S = np.fromiter('-'+I(),dtype='U1')

Q = int(I())

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



isD = S == 'D'

isM = S == 'M'

isC = S == 'C'



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

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



for K in query:

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

    x *= isM

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

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

import numpy as np

I=input

N = int(I())

S = np.array(list('-'+I()),dtype='U1')

Q = int(I())

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



isD = S == 'D'

isM = S == 'M'

isC = S == 'C'



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

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



for K in query:

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

    x *= isM

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

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

import numpy as np

I = input
N = int(I())
S = np.fromiter("-" + I(), dtype="U1")
Q = int(I())
query = list(map(int, I().split()))
isD = S == "D"
isM = S == "M"
isC = S == "C"
cumD = isD.cumsum(dtype=np.int64)
cumM = isM.cumsum(dtype=np.int64)
for K in query:
    x = cumD.copy()
    x[K:] -= cumD[:-K]
    x *= isM
    x[K + 1 :] -= isD[1:-K] * (cumM[K:-1] - cumM[: -K - 1])
    print(((x.cumsum() * isC).sum()))

import numpy as np

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

-S = np.fromiter("-" + I(), dtype="U1") +S = np.array(list("-" + I()), dtype="U1")