Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N = int(input())
    S = list(input())
    Q = int(input())
    K = list(map(int, input().split()))
    for k in K:
        d_cnt = 0
        m_cnt = 0
        dm_cnt = 0
        answer = 0
        for i in range(N):
            c = S[i]
            prevc = S[i-k]
            if i-k >= 0 and prevc == "D":
                d_cnt -= 1
                dm_cnt -= m_cnt
            elif i-k >= 0 and prevc == "M":
                m_cnt -= 1
            if c == "D":
                d_cnt += 1
            elif c == "M":
                m_cnt += 1
                dm_cnt += d_cnt
            elif c == "C":
                answer += dm_cnt
        print(answer)


if __name__ == "__main__":
    main()


Fixed submission:
def main():
    N = int(input())
    S = input()
    Q = int(input())
    K = list(map(int, input().split()))
    for k in K:
        d_cnt = 0
        m_cnt = 0
        dm_cnt = 0
        answer = 0
        for i in range(N):
            c = S[i]
            if i-k >= 0:
                prevc = S[i-k]
                if prevc == "D":
                    d_cnt -= 1
                    dm_cnt -= m_cnt
                elif prevc == "M":
                    m_cnt -= 1
            if c == "D":
                d_cnt += 1
            elif c == "M":
                m_cnt += 1
                dm_cnt += d_cnt
            elif c == "C":
                answer += dm_cnt
        print(answer)


if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N = int(input())
    S = list(input())
    Q = int(input())
    K = list(map(int, input().split()))
    for k in K:
        d_cnt = 0
        m_cnt = 0
        dm_cnt = 0
        answer = 0
        for i in range(N):
            c = S[i]
            prevc = S[i-k]
            if i-k >= 0 and prevc == "D":
                d_cnt -= 1
                dm_cnt -= m_cnt
            elif i-k >= 0 and prevc == "M":
                m_cnt -= 1
            if c == "D":
                d_cnt += 1
            elif c == "M":
                m_cnt += 1
                dm_cnt += d_cnt
            elif c == "C":
                answer += dm_cnt
        print(answer)


if __name__ == "__main__":
    main()


Fixed submission:
def main():
    N = int(input())
    S = input()
    Q = int(input())
    K = list(map(int, input().split()))
    for k in K:
        d_cnt = 0
        m_cnt = 0
        dm_cnt = 0
        answer = 0
        for i in range(N):
            c = S[i]
            if i-k >= 0:
                prevc = S[i-k]
                if prevc == "D":
                    d_cnt -= 1
                    dm_cnt -= m_cnt
                elif prevc == "M":
                    m_cnt -= 1
            if c == "D":
                d_cnt += 1
            elif c == "M":
                m_cnt += 1
                dm_cnt += d_cnt
            elif c == "C":
                answer += dm_cnt
        print(answer)


if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N = int(input())
    S = input()
    Q = int(input())
    K = list(map(int, input().split()))
    for k in K:
        d_cnt = 0
        m_cnt = 0
        dm_cnt = 0
        answer = 0
        for i in range(N):
            if i-k >= 0 and S[i-k] == "D":
                d_cnt -= 1
                dm_cnt -= m_cnt
            elif i-k >= 0 and S[i-k] == "M":
                m_cnt -= 1
            if S[i] == "D":
                d_cnt += 1
            elif S[i] == "M":
                m_cnt += 1
                dm_cnt += d_cnt
            elif S[i] == "C":
                answer += dm_cnt
        print(answer)


if __name__ == "__main__":
    main()


Fixed submission:
def main():
    N = int(input())
    S = input()
    Q = int(input())
    K = list(map(int, input().split()))
    for k in K:
        d_cnt = 0
        m_cnt = 0
        dm_cnt = 0
        answer = 0
        for i in range(N):
            c = S[i]
            if i-k >= 0:
                prevc = S[i-k]
                if prevc == "D":
                    d_cnt -= 1
                    dm_cnt -= m_cnt
                elif prevc == "M":
                    m_cnt -= 1
            if c == "D":
                d_cnt += 1
            elif c == "M":
                m_cnt += 1
                dm_cnt += d_cnt
            elif c == "C":
                answer += dm_cnt
        print(answer)


if __name__ == "__main__":
    main()