def main():

    n = int(eval(input()))

    s = eval(input())

    q = int(eval(input()))

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

    for ki in k:

        ans = 0

        DMC = [0]*3

        for i in range(n):

            c = s[i]

            if c == "D":

                DMC[0] += 1

            elif c == "M":

                DMC[1] += 1

                DMC[2] += DMC[0]

            elif c == "C":

                ans += DMC[2]

            if i >= ki-1:

                j = i-ki+1

                if s[j] == "D":

                    DMC[0] -= 1

                    DMC[2] -= DMC[1]

                elif s[j] == "M":

                    DMC[1] -= 1

        print(ans)

if __name__ == "__main__":

    main()
def main():

    n = int(eval(input()))

    s = eval(input())

    q = int(eval(input()))

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

    for ki in k:

        ans = 0

        DMC = [0]*3

        for i in range(n):

            if s[i] == "D":

                DMC[0] += 1

            elif s[i] == "M":

                DMC[1] += 1

                DMC[2] += DMC[0]

            if i >= ki:

                j = i-ki

                if s[j] == "D":

                    DMC[0] -= 1

                    DMC[2] -= DMC[1]

                elif s[j] == "M":

                    DMC[1] -= 1

            if s[i] == "C":

                ans += DMC[2]

        print(ans)



if __name__ == "__main__":

    main()
def main():
    n = int(eval(input()))
    s = eval(input())
    q = int(eval(input()))
    k = list(map(int, input().split()))
    for ki in k:
        ans = 0
        DMC = [0] * 3
        for i in range(n):
            c = s[i]
            if c == "D":
                DMC[0] += 1
            elif c == "M":
                DMC[1] += 1
                DMC[2] += DMC[0]
            elif c == "C":
                ans += DMC[2]
            if i >= ki - 1:
                j = i - ki + 1
                if s[j] == "D":
                    DMC[0] -= 1
                    DMC[2] -= DMC[1]
                elif s[j] == "M":
                    DMC[1] -= 1
        print(ans)


if __name__ == "__main__":
    main()

def main():
    n = int(eval(input()))
    s = eval(input())
    q = int(eval(input()))
    k = list(map(int, input().split()))
    for ki in k:
        ans = 0
        DMC = [0] * 3
        for i in range(n):
            if s[i] == "D":
                DMC[0] += 1
            elif s[i] == "M":
                DMC[1] += 1
                DMC[2] += DMC[0]
            if i >= ki:
                j = i - ki
                if s[j] == "D":
                    DMC[0] -= 1
                    DMC[2] -= DMC[1]
                elif s[j] == "M":
                    DMC[1] -= 1
            if s[i] == "C":
                ans += DMC[2]
        print(ans)


if __name__ == "__main__":
    main()

-            c = s[i] -            if c == "D": +            if s[i] == "D": -            elif c == "M": +            elif s[i] == "M": -            elif c == "C": -                ans += DMC[2] -            if i >= ki - 1: -                j = i - ki + 1 +            if i >= ki: +                j = i - ki +            if s[i] == "C": +                ans += DMC[2]