Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
s=list(input())
q=int(input())
k=list(map(int,input().split()))
di=s.count("D")
ma=max(k)
h=[]
for r in range(di):
    f=s.index("D")
    p=s[:]
    p=p[f+1:]
    s=s[f+1:]
    while p.count("C")!=0:
        j=p.index("C")
        if j+1<ma:
            h.append([j+1,p[:j].count("M")])
            p[j]="0"
        else:
            break
h.sort()
for i in range(q):
    c=0
    for t in range(len(h)):
        if h[t][0]<k[i]:
            c+=h[t][1]
        else:
            print(c)
            break
print(c)        


Fixed submission:
n=int(input())
s=input()
q=int(input())
for i in list(map(int,input().split())):
    a=0
    v=[0,0,0]
    for j in range(n):
        if s[j]=="D":
            v[0]+=1
        elif s[j]=="M":
            v[1]+=1
            v[2]+=v[0]
        if j>=i:
            if s[j-i]=="D":
                v[0]-=1
                v[2]-=v[1]
            elif s[j-i]=="M":
                v[1]-=1
        if s[j]=="C":
            a+=v[2]
    print(a)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
s=list(input())
q=int(input())
k=list(map(int,input().split()))
h=[0]*q
di=s.count("D")
for r in range(di):
    f=s.index("D")
    s=s[f+1:]
    a=0
    l=0
    sp=min(len(s),max(k))
    for c in range(sp):
        if s[c]=="M":
            a+=1
        elif s[c]=="C":
            l+=a
        if c+2 in k:
            h[k.index(c+2)]+=l
    else:
        for i in k:
            if i>len(s)+1:
                h[k.index(i)]+=l 
for i in range(q):
    print(h[i])

Fixed submission:
n=int(input())
s=input()
q=int(input())
for i in list(map(int,input().split())):
    a=0
    v=[0,0,0]
    for j in range(n):
        if s[j]=="D":
            v[0]+=1
        elif s[j]=="M":
            v[1]+=1
            v[2]+=v[0]
        if j>=i:
            if s[j-i]=="D":
                v[0]-=1
                v[2]-=v[1]
            elif s[j-i]=="M":
                v[1]-=1
        if s[j]=="C":
            a+=v[2]
    print(a)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
s=list(input())
q=int(input())
k=list(map(int,input().split()))
for i in range(q):
    p=s[:]
    h=0
    while p.count("D")!=0:
        f=p.index("D")
        p=p[f+1:]
        a=0
        m=(k[i]-1 if len(p)>=k[i]-1 else len(p))
        for c in range(m):
            if p[c]=="M":
                a+=1
            elif p[c]=="C":
                h+=a
    else:
        print(h)            

Fixed submission:
n=int(input())
s=input()
q=int(input())
for i in list(map(int,input().split())):
    a=0
    v=[0,0,0]
    for j in range(n):
        if s[j]=="D":
            v[0]+=1
        elif s[j]=="M":
            v[1]+=1
            v[2]+=v[0]
        if j>=i:
            if s[j-i]=="D":
                v[0]-=1
                v[2]-=v[1]
            elif s[j-i]=="M":
                v[1]-=1
        if s[j]=="C":
            a+=v[2]
    print(a)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
s=list(input())
q=int(input())
k=list(map(int,input().split()))
for i in range(q):
    p=s[:]
    h=0
    while p.count("D")!=0:
        f=p.index("D")
        p=p[f+1:]
        w=p[:]
        w=w[:k[i]-1]
        l=w.count("C")
        kl=w.count("M")
        if l!=0and kl!=0:
            for qp in range(kl):
                j=w.index("M")
                w=w[j+1:]
                h+=w.count("C")
    else:
        print(h)            

Fixed submission:
n=int(input())
s=input()
q=int(input())
for i in list(map(int,input().split())):
    a=0
    v=[0,0,0]
    for j in range(n):
        if s[j]=="D":
            v[0]+=1
        elif s[j]=="M":
            v[1]+=1
            v[2]+=v[0]
        if j>=i:
            if s[j-i]=="D":
                v[0]-=1
                v[2]-=v[1]
            elif s[j-i]=="M":
                v[1]-=1
        if s[j]=="C":
            a+=v[2]
    print(a)