Buggy python submission (verdict: Time Limit Exceeded):
import sys
n = int(input())
s = input()
q = int(input())
k = list(map(int, sys.stdin.readline().split()))
x = []
for i in s:
  if i == "D":
    x.append(1)
  elif i == "M":
    x.append(2)
  elif i == "C":
    x.append(3)
  else:
    x.append(0)
mrui = [0]* (n+1)
for i in range(1,n+1):
  mrui[i] = mrui[i-1]
  if x[i-1] == 2:
    mrui[i] += 1
dp = [[0]*4 for _ in range(n)]
for i in k:
  dp[-1] = [1,0,0,0]
  for j in range(n):
    dp[j] = dp[j-1]
    kon = x[j]
    if kon:
      dp[j][kon] += dp[j-1][kon-1]
    if j >= i-1:
      if x[j-i+1] == 1:
        dp[j][1] -= 1
        dp[j][2] -= (mrui[j+1]-mrui[j-i+2])
  
  print(dp[n-1][3])

Fixed submission:
import sys
n = int(input())
s = input()
q = int(input())
k = list(map(int, sys.stdin.readline().split()))
x = []
for i in s:
  if i == "D":
    x.append(1)
  elif i == "M":
    x.append(2)
  elif i == "C":
    x.append(3)
  else:
    x.append(0)
mrui = [0]* (n+1)
for i in range(1,n+1):
  mrui[i] = mrui[i-1]
  if x[i-1] == 2:
    mrui[i] += 1
dp = [0,0,0,0]
dp1 = [0,0,0,0]
for i in k:
  dp = [1,0,0,0]
  for j in range(n):
    dp1 = dp
    kon = x[j]
    if kon:
      dp1[kon] += dp[kon-1]
    dp = dp1
    if j >= i-1:
      if x[j-i+1] == 1:
        dp[1] -= 1
        dp[2] -= (mrui[j+1]-mrui[j-i+2])
  
  print(dp1[3])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
n = int(input())
s = input()
q = int(input())
k = list(map(int, sys.stdin.readline().split()))
x = []
for i in s:
  if i == "D":
    x.append(1)
  elif i == "M":
    x.append(2)
  elif i == "C":
    x.append(3)
  else:
    x.append(0)
mrui = [0]* (n+1)
for i in range(1,n+1):
  mrui[i] = mrui[i-1]
  if x[i-1] == 2:
    mrui[i] += 1
for i in k:
  dp = [[0]*4 for _ in range(n)]
  dp[-1][0] = 1
  for j in range(n):
    dp[j] = dp[j-1]
    kon = x[j]
    if kon:
      dp[j][kon] += dp[j-1][kon-1]
    if j >= i-1:
      if x[j-i+1] == 1:
        dp[j][1] -= 1
        dp[j][2] -= (mrui[j+1]-mrui[j-i+2])
  
  print(dp[n-1][3])

Fixed submission:
import sys
n = int(input())
s = input()
q = int(input())
k = list(map(int, sys.stdin.readline().split()))
x = []
for i in s:
  if i == "D":
    x.append(1)
  elif i == "M":
    x.append(2)
  elif i == "C":
    x.append(3)
  else:
    x.append(0)
mrui = [0]* (n+1)
for i in range(1,n+1):
  mrui[i] = mrui[i-1]
  if x[i-1] == 2:
    mrui[i] += 1
dp = [0,0,0,0]
dp1 = [0,0,0,0]
for i in k:
  dp = [1,0,0,0]
  for j in range(n):
    dp1 = dp
    kon = x[j]
    if kon:
      dp1[kon] += dp[kon-1]
    dp = dp1
    if j >= i-1:
      if x[j-i+1] == 1:
        dp[1] -= 1
        dp[2] -= (mrui[j+1]-mrui[j-i+2])
  
  print(dp1[3])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
n = int(input())
s = input()
q = int(input())
k = list(map(int, sys.stdin.readline().split()))
x = []
for i in s:
  if i == "D":
    x.append(1)
  elif i == "M":
    x.append(2)
  elif i == "C":
    x.append(3)
  else:
    x.append(0)
mrui = [0]* (n+1)
for i in range(1,n+1):
  mrui[i] = mrui[i-1]
  if x[i-1] == 2:
    mrui[i] += 1
for i in k:
  dp = [[0]*4 for _ in range(n)]
  dp[-1][0] = 1
  for j in range(n):
    dp[j][0] += dp[j-1][0]
    dp[j][1] += dp[j-1][1]
    dp[j][2] += dp[j-1][2]
    dp[j][3] += dp[j-1][3]
    if x[j] == 1:
      dp[j][1] += dp[j-1][0]
    elif x[j] == 2:
      dp[j][2] += dp[j-1][1]
    elif x[j] == 3:
      dp[j][3] += dp[j-1][2]
    if j >= i-1:
      if x[j-i+1] == 1:
        dp[j][1] -= 1
        dp[j][2] -= (mrui[j+1]-mrui[j-i+2])
  
  print(dp[n-1][3])

Fixed submission:
import sys
n = int(input())
s = input()
q = int(input())
k = list(map(int, sys.stdin.readline().split()))
x = []
for i in s:
  if i == "D":
    x.append(1)
  elif i == "M":
    x.append(2)
  elif i == "C":
    x.append(3)
  else:
    x.append(0)
mrui = [0]* (n+1)
for i in range(1,n+1):
  mrui[i] = mrui[i-1]
  if x[i-1] == 2:
    mrui[i] += 1
dp = [0,0,0,0]
dp1 = [0,0,0,0]
for i in k:
  dp = [1,0,0,0]
  for j in range(n):
    dp1 = dp
    kon = x[j]
    if kon:
      dp1[kon] += dp[kon-1]
    dp = dp1
    if j >= i-1:
      if x[j-i+1] == 1:
        dp[1] -= 1
        dp[2] -= (mrui[j+1]-mrui[j-i+2])
  
  print(dp1[3])