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

def search(k):
    d = 0
    m = 0
    count = 0
    ans = 0
    for i in range(n):
        if s[i] == "D":
            d += 1
        elif s[i] == "M":
            m += 1
            count += d
        elif s[i] == "C":
            ans += count
        if i>= k-1:
            if s[i-k+1] == "D":
                d -= 1
                count -= m
            elif s[i-k+1] == "M":
                m -= 1                
    return ans
for i in k:
    print(search(i))

Fixed submission:
from collections import deque
n = int(input())
s = input()
q = int(input())
k = list(map(int,input().split()))

def search(k):
    d = 0
    m = 0
    count = 0
    ans = 0
    q = deque([])
    for i in range(n):
        if s[i] == "D":
            d += 1
        elif s[i] == "M":
            m += 1
            count += d
            q.append(d)
        elif s[i] == "C":
            ans += count
        if i>= k-1:
            if s[i-k+1] == "D":
                d -= 1
                count -= m
            elif s[i-k+1] == "M":
                m -= 1
                
        
    return ans
for i in k:
    print(search(i))

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()))
    k = [[x,i] for i,x in enumerate(k)]
    k.sort()

    ans = [[0,-1] for i in range(q)]
    for i in range(q):
        ans[i][1] = k[i][1]

    def search(x):
        ind = x
        m = 0
        c = 0
        count = 0
        for num,i in enumerate(k):
            while ind+count < n and count < i[0]:
                if s[ind+count] == "M":
                    m += 1
                elif s[ind+count] == "C":
                    c += 1
                    ans[num][0]+=m
                count += 1
        
    for i in range(n):
        if s[i] == "D":
            search(i)

    for i in range(1,q):
        ans[i][0] += ans[i-1][0]
    ans.sort(key=lambda x: x[1])
    for i,j in ans:
        print(i)

if __name__ == "__main__":
    main()

Fixed submission:
from collections import deque
n = int(input())
s = input()
q = int(input())
k = list(map(int,input().split()))

def search(k):
    d = 0
    m = 0
    count = 0
    ans = 0
    q = deque([])
    for i in range(n):
        if s[i] == "D":
            d += 1
        elif s[i] == "M":
            m += 1
            count += d
            q.append(d)
        elif s[i] == "C":
            ans += count
        if i>= k-1:
            if s[i-k+1] == "D":
                d -= 1
                count -= m
            elif s[i-k+1] == "M":
                m -= 1
                
        
    return ans
for i in k:
    print(search(i))

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()))
k = [[x,i] for i,x in enumerate(k)]
k.sort()

ans = [[0,-1] for i in range(q)]
for i in range(q):
    ans[i][1] = k[i][1]

def search(x):
    ind = x
    m = 0
    c = 0
    count = 0
    for num,i in enumerate(k):
        while ind+count < n and count < i[0]:
            if s[ind+count] == "M":
                m += 1
            elif s[ind+count] == "C":
                c += 1
                ans[num][0]+=m
            count += 1
    
for i in range(n):
    if s[i] == "D":
        search(i)

for i in range(1,q):
    ans[i][0] += ans[i-1][0]
ans.sort(key=lambda x: x[1])
for i,j in ans:
    print(i)

Fixed submission:
from collections import deque
n = int(input())
s = input()
q = int(input())
k = list(map(int,input().split()))

def search(k):
    d = 0
    m = 0
    count = 0
    ans = 0
    q = deque([])
    for i in range(n):
        if s[i] == "D":
            d += 1
        elif s[i] == "M":
            m += 1
            count += d
            q.append(d)
        elif s[i] == "C":
            ans += count
        if i>= k-1:
            if s[i-k+1] == "D":
                d -= 1
                count -= m
            elif s[i-k+1] == "M":
                m -= 1
                
        
    return ans
for i in k:
    print(search(i))

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
s = input()
q = int(input())
k = list(map(int,input().split()))
k = [[x,i] for i,x in enumerate(k)]
k.sort()
MAX = k[-1][0]
ans = [[0,-1] for i in range(q)]
for i in range(q):
    ans[i][1] = k[i][1]

def search(x):
    ind = x
    m = 0
    c = 0
    count = 0
    for num,i in enumerate(k):
        while ind+count < n and count < i[0]:
            if s[ind+count] == "M":
                m += 1
            elif s[ind+count] == "C":
                c += 1
                ans[num][0]+=m
            count += 1
    
for i in range(n):
    if s[i] == "D":
        search(i)

for i in range(1,q):
    ans[i][0] += ans[i-1][0]
ans.sort(key=lambda x: x[1])
for i,j in ans:
    print(i)

Fixed submission:
from collections import deque
n = int(input())
s = input()
q = int(input())
k = list(map(int,input().split()))

def search(k):
    d = 0
    m = 0
    count = 0
    ans = 0
    q = deque([])
    for i in range(n):
        if s[i] == "D":
            d += 1
        elif s[i] == "M":
            m += 1
            count += d
            q.append(d)
        elif s[i] == "C":
            ans += count
        if i>= k-1:
            if s[i-k+1] == "D":
                d -= 1
                count -= m
            elif s[i-k+1] == "M":
                m -= 1
                
        
    return ans
for i in k:
    print(search(i))

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
s = input()
q = int(input())
k = list(map(int,input().split()))
k = [[x,i] for i,x in enumerate(k)]
k.sort()
MAX = k[-1][0]
ans = [[0,-1] for i in range(q)]
for i in range(q):
    ans[i][1] = k[i][1]

def search(x):
    ind = x
    m = 0
    c = 0
    count = 0
    for num,i in enumerate(k):
        while ind+count < n and count < i[0]:
            if s[ind+count] == "M":
                m += 1
            elif s[ind+count] == "C":
                c += 1
                ans[num][0]+=m
            count += 1
    
for i in range(n):
    if s[i] == "D":
        search(i)

for i in range(1,q):
    ans[i][0] += ans[i-1][0]
ans.sort(key=lambda x: x[1])
for i,j in ans:
    print(i)

Fixed submission:
from collections import deque
n = int(input())
s = input()
q = int(input())
k = list(map(int,input().split()))

def search(k):
    d = 0
    m = 0
    count = 0
    ans = 0
    q = deque([])
    for i in range(n):
        if s[i] == "D":
            d += 1
        elif s[i] == "M":
            m += 1
            count += d
            q.append(d)
        elif s[i] == "C":
            ans += count
        if i>= k-1:
            if s[i-k+1] == "D":
                d -= 1
                count -= m
            elif s[i-k+1] == "M":
                m -= 1
                
        
    return ans
for i in k:
    print(search(i))