Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

N = int(input())
S = input()
Q = int(input())
K = list(map(int, input().split()))

for k in K:
    res = 0
    D = deque()
    M = deque()
    dm = 0
    for i in range(N):
        if S[i] == 'D':
            D.append(i)
        elif S[i] == 'M':
            dm += len(D)
            if D:
                M.append(i)
        elif S[i] == 'C':
            res += dm
        if D and D[0] + k <= i + 1:
            D.popleft()
            dm -= len(M)
            if D:
                while M and D[0] > M[0]:
                    M.popleft()
            else:
                M = deque()
                dm = 0

    print(res)

Fixed submission:
from collections import deque

N = int(input())
S = input()
Q = int(input())
K = list(map(int, input().split()))

for k in K:
    res = 0
    d = 0
    m = 0
    dm = 0
    for i in range(N):
        if S[i] == 'D':
            d += 1
        elif S[i] == 'M':
            dm += d
            m += 1
        elif S[i] == 'C':
            res += dm
        if i + 1 - k >= 0:
            if S[i + 1 - k] == 'D':
                d -= 1
                dm -= m
            elif S[i + 1 - k] == 'M':
                m -= 1

    print(res)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

N = int(input())
S = input()
Q = int(input())
K = list(map(int, input().split()))

for k in K:
    res = 0
    D = deque()
    M = deque()
    dm = 0
    for i in range(N):
        if S[i] == 'D':
            D.append(i)
        if S[i] == 'M':
            dm += len(D)
            if D:
                M.append(i)
        if S[i] == 'C':
            res += dm
        if D and D[0] + k <= i + 1:
            d = D.popleft()
            dm -= len(M)
            if D:
                while M and D[0] > M[0]:
                    M.popleft()
            else:
                M = deque()
                dm = 0

    print(res)

Fixed submission:
from collections import deque

N = int(input())
S = input()
Q = int(input())
K = list(map(int, input().split()))

for k in K:
    res = 0
    d = 0
    m = 0
    dm = 0
    for i in range(N):
        if S[i] == 'D':
            d += 1
        elif S[i] == 'M':
            dm += d
            m += 1
        elif S[i] == 'C':
            res += dm
        if i + 1 - k >= 0:
            if S[i + 1 - k] == 'D':
                d -= 1
                dm -= m
            elif S[i + 1 - k] == 'M':
                m -= 1

    print(res)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

N = int(input())
S = input()
Q = int(input())
K = list(map(int, input().split()))

for k in K:
    res = 0
    D = deque()
    M = deque()
    dm = 0
    for i in range(N):
        if S[i] == 'D':
            D.append(i)
        if S[i] == 'M':
            dm += len(D)
            M.append(i)
        if S[i] == 'C':
            res += dm
        if D and D[0] + k <= i + 1:
            d = D.popleft()
            dm -= len(M)
            if D:
                while M and D[0] > M[0]:
                    M.popleft()
            else:
                M = deque()
                dm = 0

    print(res)

Fixed submission:
from collections import deque

N = int(input())
S = input()
Q = int(input())
K = list(map(int, input().split()))

for k in K:
    res = 0
    d = 0
    m = 0
    dm = 0
    for i in range(N):
        if S[i] == 'D':
            d += 1
        elif S[i] == 'M':
            dm += d
            m += 1
        elif S[i] == 'C':
            res += dm
        if i + 1 - k >= 0:
            if S[i + 1 - k] == 'D':
                d -= 1
                dm -= m
            elif S[i + 1 - k] == 'M':
                m -= 1

    print(res)