Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
s = input()
q = int(input())
K = list(map(int,input().split()))

for ii in range(q):
    D = [[0] * 3 for i in range(n)]
    ans = 0
    k = K[ii]
    for i in range(n):
        for j in range(3):
            D[i][j] = D[i-1][j]
        
        if i-k >= 0:
            if s[i-k] == "D":
                D[i][0] -= 1
                D[i][2] -= D[i][1]
            if s[i-k] == "M":
                D[i][1] -= 1
        
        if s[i] == "D":
            D[i][0] += 1
        if s[i] == "M":
            D[i][1] += 1
            D[i][2] += D[i][0]
        if s[i] == "C":
            ans += D[i][2]
    print(ans)
    # print(D)

Fixed submission:
import sys
input = sys.stdin.readline

def main():
    n = int(input())
    s = input()
    q = int(input())
    K = list(map(int,input().split()))
    # print(s)

    for i in range(q):
        ans = 0
        d = 0
        m = 0
        dm = 0
        k = K[i]
        for j in range(n):

            if j-k >= 0:
                if s[j-k] == "D":
                    d -= 1
                    dm -= m
                elif s[j-k] == "M":
                    m -= 1

            if s[j] == "D":
                d += 1
            elif s[j] == "M":
                m += 1
                dm += d
            elif s[j] == "C":
                ans += dm
        print(ans)

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

def main():
    n = int(input())
    s = input()
    q = int(input())
    K = list(map(int,input().split()))

    # print(s)

    for i in range(q):
        ans = 0
        k = K[i]
        D = [[0] * 3 for j in range(n)]
        for j in range(n):
            D[j][:] = D[j-1][:]

            if j-k >= 0:
                if s[j-k] == "D":
                    D[j][0] -= 1
                    D[j][2] -= D[j][1]
                elif s[j-k] == "M":
                    D[j][1] -= 1

            if s[j] == "D":
                D[j][0] += 1
            elif s[j] == "M":
                D[j][1] += 1
                D[j][2] += D[j][0]
            elif s[j] == "C":
                ans += D[j][2]
        print(ans)

if __name__ == "__main__":
    main()

Fixed submission:
import sys
input = sys.stdin.readline

def main():
    n = int(input())
    s = input()
    q = int(input())
    K = list(map(int,input().split()))
    # print(s)

    for i in range(q):
        ans = 0
        d = 0
        m = 0
        dm = 0
        k = K[i]
        for j in range(n):

            if j-k >= 0:
                if s[j-k] == "D":
                    d -= 1
                    dm -= m
                elif s[j-k] == "M":
                    m -= 1

            if s[j] == "D":
                d += 1
            elif s[j] == "M":
                m += 1
                dm += d
            elif s[j] == "C":
                ans += dm
        print(ans)

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
n = int(input())
s = input()
q = int(input())
K = list(map(int,input().split()))

# print(s)

for i in range(q):
    ans = 0
    k = K[i]
    D = [[0] * 3 for j in range(n)]
    for j in range(n):
        D[j][:] = D[j-1][:]

        if j-k >= 0:
            if s[j-k] == "D":
                D[j][0] -= 1
                D[j][2] -= D[j][1]
            elif s[j-k] == "M":
                D[j][1] -= 1

        if s[j] == "D":
            D[j][0] += 1
        elif s[j] == "M":
            D[j][1] += 1
            D[j][2] += D[j][0]
        elif s[j] == "C":
            ans += D[j][2]
    print(ans)

Fixed submission:
import sys
input = sys.stdin.readline

def main():
    n = int(input())
    s = input()
    q = int(input())
    K = list(map(int,input().split()))
    # print(s)

    for i in range(q):
        ans = 0
        d = 0
        m = 0
        dm = 0
        k = K[i]
        for j in range(n):

            if j-k >= 0:
                if s[j-k] == "D":
                    d -= 1
                    dm -= m
                elif s[j-k] == "M":
                    m -= 1

            if s[j] == "D":
                d += 1
            elif s[j] == "M":
                m += 1
                dm += d
            elif s[j] == "C":
                ans += dm
        print(ans)

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
s = input()
q = int(input())
K = list(map(int,input().split()))

for i in range(q):
    ans = 0
    k = K[i]
    D = [[0] * 3 for j in range(n)]
    for j in range(n):
        D[j][:] = D[j-1][:]

        if j-k >= 0:
            if s[j-k] == "D":
                D[j][0] -= 1
                D[j][2] -= D[j][1]
            elif s[j-k] == "M":
                D[j][1] -= 1

        if s[j] == "D":
            D[j][0] += 1
        elif s[j] == "M":
            D[j][1] += 1
            D[j][2] += D[j][0]
        elif s[j] == "C":
            ans += D[j][2]
    print(ans)

Fixed submission:
import sys
input = sys.stdin.readline

def main():
    n = int(input())
    s = input()
    q = int(input())
    K = list(map(int,input().split()))
    # print(s)

    for i in range(q):
        ans = 0
        d = 0
        m = 0
        dm = 0
        k = K[i]
        for j in range(n):

            if j-k >= 0:
                if s[j-k] == "D":
                    d -= 1
                    dm -= m
                elif s[j-k] == "M":
                    m -= 1

            if s[j] == "D":
                d += 1
            elif s[j] == "M":
                m += 1
                dm += d
            elif s[j] == "C":
                ans += dm
        print(ans)

if __name__ == "__main__":
    main()