Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
from collections import defaultdict
N = int(input())
S = input().strip()
Q = int(input())
K = list(map(int,input().split()))

m = [0] * 3
for k in K:
    ans = 0
    m = defaultdict(int)
    for i,s in enumerate(S):
        if i >= k:
            pre = S[i-k]
            if pre == 'D':
                m[0] -= 1 # m['D'] -= 1
                m[2] -= m[1] # m['DM'] -= m['M']
            elif pre == 'M':
                m[1] -= 1 # m['M'] -= 1
        if s == 'D':
            m[0] += 1 # m['D'] += 1
        if s == 'M': 
            m[2] += m[0] # m['DM'] += m['D']
            m[1] += 1# m['M'] += 1
        if s == 'C':
            ans += m[2] # ans += m['DM']
        #print(s,m)
    print(ans)



Fixed submission:
import sys
input = sys.stdin.readline
from collections import defaultdict
N = int(input())
S = input().strip()
Q = int(input())
K = list(map(int,input().split()))

'''
import random
import string
S = "".join(random.choices(string.ascii_uppercase, k=10**6))
Q =75 
K = [random.randint(1,10**6) for i in range(Q)]
'''

for k in K:
    ans = 0
    m = [0] * 3
    for i,s in enumerate(S):
        if i >= k:
            pre = S[i-k]
            if pre == 'D':
                m[0] -= 1 # m['D'] -= 1
                m[2] -= m[1] # m['DM'] -= m['M']
            elif pre == 'M':
                m[1] -= 1 # m['M'] -= 1
        if s == 'D':
            m[0] += 1 # m['D'] += 1
        if s == 'M': 
            m[2] += m[0] # m['DM'] += m['D']
            m[1] += 1# m['M'] += 1
        if s == 'C':
            ans += m[2] # ans += m['DM']
        #print(s,m)
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
from collections import defaultdict
N = int(input())
S = input().strip()
Q = int(input())
K = list(map(int,input().split()))

for k in K:
    ans = 0
    m = defaultdict(int)
    for i,s in enumerate(S):
        if i >= k:
            pre = S[i-k]
            if pre == 'D':
                m['D'] -= 1
                m['DM'] -= m['M']
            elif pre == 'M':
                m['M'] -= 1
        if s == 'D':
            m['D'] += 1
        if s == 'M': 
            m['DM'] += m['D']
            m['M'] += 1
        if s == 'C':
            ans += m['DM']
        #print(s,m)
    print(ans)



Fixed submission:
import sys
input = sys.stdin.readline
from collections import defaultdict
N = int(input())
S = input().strip()
Q = int(input())
K = list(map(int,input().split()))

'''
import random
import string
S = "".join(random.choices(string.ascii_uppercase, k=10**6))
Q =75 
K = [random.randint(1,10**6) for i in range(Q)]
'''

for k in K:
    ans = 0
    m = [0] * 3
    for i,s in enumerate(S):
        if i >= k:
            pre = S[i-k]
            if pre == 'D':
                m[0] -= 1 # m['D'] -= 1
                m[2] -= m[1] # m['DM'] -= m['M']
            elif pre == 'M':
                m[1] -= 1 # m['M'] -= 1
        if s == 'D':
            m[0] += 1 # m['D'] += 1
        if s == 'M': 
            m[2] += m[0] # m['DM'] += m['D']
            m[1] += 1# m['M'] += 1
        if s == 'C':
            ans += m[2] # ans += m['DM']
        #print(s,m)
    print(ans)