Buggy python submission (verdict: Time Limit Exceeded):
def num(s,n,k):
  d,m,dm,dmc = 0,0,0,0
  for i in range(n,n*2):
    x,y = s[i],s[i-k]
    if y == 68:
      d -= 1
      dm -= m
    elif y == 77:
      m -= 1
    if x == 68:
      d += 1
    elif x == 77:
      m += 1
      dm += d
    elif x == 67:
      dmc += dm
  return dmc

def main():
  n = int(input())
  s = [0]*n + list(map(ord, list(input())))
  _ = input()
  l = list(map(int, input().split()))
  for k in l:
    print(num(s,n,k))

main()

Fixed submission:
def num(s,n,k):
  d,m,dm,dmc = 0,0,0,0
  for i in range(n,n*2):
    x,y = s[i],s[i-k]
    if y == 68:
      d -= 1
      dm -= m
    elif y == 77:
      m -= 1
    if x == 68:
      d += 1
    elif x == 77:
      m += 1
      dm += d
    elif x == 67:
      dmc += dm
  return dmc

def main():
  n = int(input())
  s = [0]*n + list(map(ord, list(input())))
  _ = input()
  l = list(map(int, input().split()))
  for k in l:
    print(num(s,n,k))

main()

Buggy python submission (verdict: Time Limit Exceeded):
def num(s,n,k):
  d,m,dm,dmc = 0,0,0,0
  for i,j in zip(s[n:],s[n-k:]):
    if j == 1:
      d -= 1
      dm -= m
    elif j == 2:
      m -= 1
    if i == 1:
      d += 1
    elif i == 2:
      m += 1
      dm += d
    elif i == 3:
      dmc += dm
  return dmc

def main():
  n = int(input())
  t = input()
  _ = input()
  s = [0]*n + [(x=='D') + (x=='M')*2 + (x=='C')*3 for x in t]
  l = list(map(int, input().split()))
  for k in l:
    print(num(s,n,k))

main()

Fixed submission:
def num(s,n,k):
  d,m,dm,dmc = 0,0,0,0
  for i in range(n,n*2):
    x,y = s[i],s[i-k]
    if y == 68:
      d -= 1
      dm -= m
    elif y == 77:
      m -= 1
    if x == 68:
      d += 1
    elif x == 77:
      m += 1
      dm += d
    elif x == 67:
      dmc += dm
  return dmc

def main():
  n = int(input())
  s = [0]*n + list(map(ord, list(input())))
  _ = input()
  l = list(map(int, input().split()))
  for k in l:
    print(num(s,n,k))

main()

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
t = input()
_ = input()
mk = str.maketrans('DMCYQWERTUIOPASFGHJKLZXVBN','123' + '0'*23)
s = [0]*n + list(map(int, list(t.translate(mk))))

l = list(map(int, input().split()))
for k in l:
  d,m,dm,dmc = 0,0,0,0
  for i,j in zip(s[n:],s[n-k:]):
    if j == 1:
      d -= 1
      dm -= m
    elif j == 2:
      m -= 1
    if i == 1:
      d += 1
    elif i == 2:
      m += 1
      dm += d
    elif i == 3:
      dmc += dm
  print(dmc)

Fixed submission:
def num(s,n,k):
  d,m,dm,dmc = 0,0,0,0
  for i in range(n,n*2):
    x,y = s[i],s[i-k]
    if y == 68:
      d -= 1
      dm -= m
    elif y == 77:
      m -= 1
    if x == 68:
      d += 1
    elif x == 77:
      m += 1
      dm += d
    elif x == 67:
      dmc += dm
  return dmc

def main():
  n = int(input())
  s = [0]*n + list(map(ord, list(input())))
  _ = input()
  l = list(map(int, input().split()))
  for k in l:
    print(num(s,n,k))

main()

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
t = input()
_ = input()
mk = str.maketrans('DMCYQWERTUIOPASFGHJKLZXVBN','123' + '0'*23)
s = [0]*n + list(map(int, list(t.translate(mk))))

l = list(map(int, input().split()))
for k in l:
  d,m,dm,dmc = 0,0,0,0
  for i,j in zip(s[n:],s[n-k:]):
    if j == 1:
      d -= 1
      dm -= m
    elif j == 2:
      m -= 1
    if i == 1:
      d += 1
    elif i == 2:
      m += 1
      dm += d
    elif i == 3:
      dmc += dm
  print(dmc)

Fixed submission:
def num(s,n,k):
  d,m,dm,dmc = 0,0,0,0
  for i in range(n,n*2):
    x,y = s[i],s[i-k]
    if y == 68:
      d -= 1
      dm -= m
    elif y == 77:
      m -= 1
    if x == 68:
      d += 1
    elif x == 77:
      m += 1
      dm += d
    elif x == 67:
      dmc += dm
  return dmc

def main():
  n = int(input())
  s = [0]*n + list(map(ord, list(input())))
  _ = input()
  l = list(map(int, input().split()))
  for k in l:
    print(num(s,n,k))

main()

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
t = input()
q = int(input())
s = [0]*(n*2)
for i in range(n):
  if t[i] == 'D':
    s[i+n] = 1
  elif t[i] == 'M':
    s[i+n] = 2
  elif t[i] == 'C':
    s[i+n] = 3

l = list(map(int, input().split()))
for k in l:
  d,m,dm,dmc = 0,0,0,0
  for i in range(n,n*2):
    j = i-k
    if s[j] == 1:
      d -= 1
      dm -= m
    elif s[j] == 2:
      m -= 1
    if s[i] == 1:
      d += 1
    elif s[i] == 2:
      m += 1
      dm += d
    elif s[i] == 3:
      dmc += dm
  print(dmc)

Fixed submission:
def num(s,n,k):
  d,m,dm,dmc = 0,0,0,0
  for i in range(n,n*2):
    x,y = s[i],s[i-k]
    if y == 68:
      d -= 1
      dm -= m
    elif y == 77:
      m -= 1
    if x == 68:
      d += 1
    elif x == 77:
      m += 1
      dm += d
    elif x == 67:
      dmc += dm
  return dmc

def main():
  n = int(input())
  s = [0]*n + list(map(ord, list(input())))
  _ = input()
  l = list(map(int, input().split()))
  for k in l:
    print(num(s,n,k))

main()

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
t = input()
q = int(input())
s = [0]*(n*2)
for i in range(n):
  if t[i] == 'D':
    s[i+n] = 1
  elif t[i] == 'M':
    s[i+n] = 2
  elif t[i] == 'C':
    s[i+n] = 3

l = list(map(int, input().split()))
for k in l:
  d,m,dm,dmc = 0,0,0,0
  for i in range(n,n*2):
    j = i-k
    if s[j] == 1:
      d -= 1
      dm -= m
    elif s[j] == 2:
      m -= 1
    if s[i] == 1:
      d += 1
    elif s[i] == 2:
      m += 1
      dm += d
    elif s[i] == 3:
      dmc += dm
  print(dmc)

Fixed submission:
def num(s,n,k):
  d,m,dm,dmc = 0,0,0,0
  for i in range(n,n*2):
    x,y = s[i],s[i-k]
    if y == 68:
      d -= 1
      dm -= m
    elif y == 77:
      m -= 1
    if x == 68:
      d += 1
    elif x == 77:
      m += 1
      dm += d
    elif x == 67:
      dmc += dm
  return dmc

def main():
  n = int(input())
  s = [0]*n + list(map(ord, list(input())))
  _ = input()
  l = list(map(int, input().split()))
  for k in l:
    print(num(s,n,k))

main()

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
t = input()
s = [0]*n
for i in range(n):
  if t[i] == 'D':
    s[i] = 1
  elif t[i] == 'M':
    s[i] = 2
  elif t[i] == 'C':
    s[i] = 3
_ = input()
l = list(map(int, input().split()))
for k in l:
  d,m,dm,dmc = 0,0,0,0
  for i in range(n):
    if i > k-1:
      j = i-k
      if s[j] == 1:
        d -= 1
        dm -= m
      elif s[j] == 2:
        m -= 1
    if s[i] == 1:
      d += 1
    elif s[i] == 2:
      m += 1
      dm += d
    elif s[i] == 3:
      dmc += dm
  print(dmc)

Fixed submission:
def num(s,n,k):
  d,m,dm,dmc = 0,0,0,0
  for i in range(n,n*2):
    x,y = s[i],s[i-k]
    if y == 68:
      d -= 1
      dm -= m
    elif y == 77:
      m -= 1
    if x == 68:
      d += 1
    elif x == 77:
      m += 1
      dm += d
    elif x == 67:
      dmc += dm
  return dmc

def main():
  n = int(input())
  s = [0]*n + list(map(ord, list(input())))
  _ = input()
  l = list(map(int, input().split()))
  for k in l:
    print(num(s,n,k))

main()

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
s = input()
_ = input()
l = list(map(int, input().split()))
for k in l:
  d,m,dm,dmc = 0,0,0,0
  for i in range(n):
    if i > k-1:
      j = i-k
      if s[j] == 'D':
        d -= 1
        dm -= m
      elif s[j] == 'M':
        m -= 1
    if s[i] == 'D':
      d += 1
    elif s[i] == 'M':
      m += 1
      dm += d
    elif s[i] == 'C':
      dmc += dm
  print(dmc)

Fixed submission:
def num(s,n,k):
  d,m,dm,dmc = 0,0,0,0
  for i in range(n,n*2):
    x,y = s[i],s[i-k]
    if y == 68:
      d -= 1
      dm -= m
    elif y == 77:
      m -= 1
    if x == 68:
      d += 1
    elif x == 77:
      m += 1
      dm += d
    elif x == 67:
      dmc += dm
  return dmc

def main():
  n = int(input())
  s = [0]*n + list(map(ord, list(input())))
  _ = input()
  l = list(map(int, input().split()))
  for k in l:
    print(num(s,n,k))

main()