Harryxun/GPT-100-sft-dataset huggingface dataset:
Problem p03078 (python)

Fixed submission:
x, y, z, k = map(int, input().split())
a =list(map(int, input().split()))
b =list(map(int, input().split()))
c = list(map(int, input().split()))
a.sort(reverse = True)
b.sort(reverse = True)
c.sort(reverse = True)
abc = []
for i in range(x):
    if (i+1) > k:
        break
    for j in range(y):
        if (i+1)*(j+1) > k:
            break
        for h in range(z):
            if (i+1)*(j+1)*(h+1) >k:
                break
            abc.append(a[i] +b[j] +c[h])
abc.sort(reverse = True)
for i in range(k):
    print(abc[i])

Buggy submission (verdict: Runtime Error):
x, y, z, k = map(int, input().split())
a =list(map(int, input().split()))
b =list(map(int, input().split()))
c = list(map(int, input().split()))
a.sort(reverse = True)
b.sort(reverse = True)
c.sort(reverse = True)
ab =[]
abc = []
for i in a:
    for j in b:
        ab.append(i+j)
ab.sort(reverse = True)
m = min(K, X*Y)
ab = ab[:m]
for i in ab:
    for j in c:
        abc.append(i +j)
abc.sort(reverse = True)
for i in range(k):
    print(abc[i])

Buggy submission (verdict: Runtime Error):
x, y, z, k = map(int, input().split())
a =list(map(int, input().split()))
b =list(map(int, input().split()))
c = list(map(int, input().split()))
a.sort(reverse = True)
b.sort(reverse = True)
c.sort(reverse = True)
ab =[]
abc = []
for i in a:
    for j in b:
        ab.append(i+j)
ab.sort(reverse = True)
m = min(K, X*Y)
ab = ab[:m]
for i in ab:
    for j in c:
        abc.append(i +j)
abc.sort(reverse = True)
for i in range(k):
    print(abc[i])

Buggy submission (verdict: Runtime Error):
x, y, z, k = map(int, input().split())
a =list(map(int, input().split()))
b =list(map(int, input().split()))
c = list(map(int, input().split()))
a.sort(reverse = True)
b.sort(reverse = True)
c.sort(reverse = True)
ab =[]
abc = []
for i in a:
    for j in b:
        ab.append(i+j)
ab.sort(reverse = True)
m = min(K, X*Y)
ab = ab[:m]
for i in ab:
    for j in c:
        abc.append(i +j)
abc.sort(reverse = True)
for i in range(c):
    print(abc[i])

Buggy submission (verdict: Runtime Error):
x, y, z, k = map(int, input().split())
a =list(map(int, input().split()))
b =list(map(int, input().split()))
c = list(map(int, input().split()))
a.sort(reverse = True)
b.sort(reverse = True)
c.sort(reverse = True)
ab =[]
abc = []
for i in a:
    for j in b:
        ab.append(i+j)
ab.sort(reverse = True)
m = min(K, X*Y)
ab = ab[:m+1]
for i in ab:
    for j in c:
        abc.append(i +j)
abc.sort(reverse = True)
for i in abc:
    print(i)
Problem p03086 (python)

Fixed submission:
s = input()
acgt = []
cnt = 0
for a in s:
    if a == 'A' or a == 'C' or a == 'T' or a == 'G':
        cnt += 1
    else:
        acgt.append(cnt)
        cnt = 0
acgt.append(cnt)
print(max(acgt))

Buggy submission (verdict: Runtime Error):
s = input()
acgt = []
cnt = 0
for a in s:
    if a == 'A' or a == 'C' or a == 'T' or a == 'G':
        cnt += 1
    else:
        acgt.append(cnt)
        cnt = 0
acgt.appen(cnt)
print(max(acgt))

Buggy submission (verdict: Runtime Error):
s = input()
acgt = []
cnt = 0
for a in s:
    if a == 'A' or a == 'C' or a == 'T' or a == 'G':
        cnt += 1
    else:
        acgt.append(cnt)
        cnt = 0
print(max(acgt))
Problem p03111 (python)

Fixed submission:
N, A, B, C = map(int, input().split())
l = [int(input()) for i in range(N)]
inf = 10 ** 9

def dfs(cur, a, b, c):
    if cur == N:
        return abs(a-A)+abs(b-B)+abs(c-C) - 30 if min(a, b, c) >0 else inf
    ret0 = dfs(cur+1, a,b,c)
    ret1 = dfs(cur+1, a+l[cur], b, c) + 10
    ret2 = dfs(cur+1, a, b+l[cur], c) +10
    ret3 = dfs(cur+1, a,b,c+l[cur]) +10
    return min(ret0, ret1, ret2, ret3)

print(dfs(0, 0, 0, 0))

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(100000)
N, A, B, C = map(int, input().split())
l = [int(input()) for i in range(n)]
inf = 10 ** 9

def dfs(cur, a, b, c):
    if cur == N:
        return abs(a-A)+abs(b-B)+abs(c-C) - 30 if min(a, b, c) >0 else inf
    ret0 = dfs(cur+1, a,b,c)
    ret1 = dfs(cur+1, a+l[cur], b, c) + 10
    ret2 = dfs(cur+1, a, b+l[cur], c) +10
    ret3 = dfs(cur+1, a,b,c+l[cur]) +10
    return min(ret0, ret1, ret2, ret3)

print(dfs(0, 0, 0, 0))

Buggy submission (verdict: Runtime Error):
N, A, B, C = map(int, input().split())
l = [int(input()) for i in range(n)]
inf = 10 ** 9

def dfs(cur, a, b, c):
    if cur == N:
        return abs(a-A)+abs(b-B)+abs(c-C) - 30 if min(a, b, c) >0 else inf
    ret0 = dfs(cur+1, a,b,c)
    ret1 = dfs(cur+1, a+l[cur], b, c) + 10
    ret2 = dfs(cur+1, a, b+l[cur], c) +10
    ret3 = dfs(cur+1, a,b,c+l[cur]) +10
    return min(ret0, ret1, ret2, ret3)

print(dfs(0, 0, 0, 0))
Problem p03251 (python)

Fixed submission:
N, M , X , Y = map(int, input().split())
xs = list(map(int, input().split()))
ys = list(map(int, input().split()))
xs.append(X)
ys.append(Y)
if min(ys) <= max(xs):
    print('War')
else:
    print('No War')

Buggy submission (verdict: Runtime Error):
N, M , X , Y = map(int, input().split())
xs = list(map(int, input().split()))
ys = list(map(int, input().split()))
xs.append(X)
ys.append(Y)
if min(ys) <= max(xs) 
    print('War')
else:
    print('No War')
Problem p03262 (python)

Fixed submission:
import fractions
N, X = map(int, input().split())
xs = list(map(int, input().split()))
xs.append(X)
xs.sort()
dif = []
for i in range(N):
    dif.append(xs[i+1] -xs[i])
if len(dif) > 1:
    ans = fractions.gcd(dif[0], dif[1])
    for i in range(N):
        ans = fractions.gcd(ans, dif[i])
    print(ans)
else:
    print(dif[0])

Buggy submission (verdict: Runtime Error):
import math
N, X = map(int, input().split())
xs = list(map(int, input().split()))
xs.append(X)
xs.sort()
dif = []
for i in range(N):
    dif.append(xs[i+1] -xs[i])
if len(dif) > 1:
    ans = math.gcd(dif[0], dif[1])
    for i in range(N):
        ans = math.gcd(ans, dif[i])
    print(ans)
else:
    print(dif[0])
Problem p03289 (python)

Fixed submission:
S = input()
flag = True
if S[0] != 'A':
    flag = False
elif 'C' not in S[2:len(S)-1]:
    flag = False
s2 = S.replace('C', '', 1)
if 'C' in s2:
    flag = False
    
if flag:
    s2 = s2.replace('A', '', 1)
    if s2.islower():
        flag = True
    else:
        flag = False
if flag:
    print('AC')
else:
    print('WA')

Buggy submission (verdict: Runtime Error):
S = input()
flag = True
if S[0] != 'A':
    flag = False
elif 'C' in S[2:len(S)-1]:
    s2 = S.replace('C', '', 1)
    if 'C' in s2:
        flag = False
if flag:
    s2 = s2.replace('A', '', 1)
    if s2.islower():
        flag = True
    else:
        flag = False
if flag:
    print('AC')
else:
    print('WA')
Problem p03355 (python)

Fixed submission:
s = input()
K = int(input())
head = [h for h in s]

head = set(head)
head = list(head)
head.sort()

cnt = 0
ans = []
idx = 0
while cnt < K:
    for i in range(len(s)):
        if s[i] == head[idx]:
            for k in range(i+1, min(len(s)+1, i+6)):
                ans.append(s[i:k])
        ans = set(ans)
        ans = list(ans)
        cnt = len(ans)
    idx += 1
ans = set(ans)
ans = list(ans)
ans.sort()
print(ans[K-1])

Buggy submission (verdict: Runtime Error):
s = input()
K = int(input())
head = [h for h in s]

head = set(head)
head = list(head)
head.sort()

cnt = 0
ans = []
idx = 0
while cnt < K:
    for i in range(len(s)):
        if s[i] == head[idx]:
            for k in range(i+1, len(s)+1):
                ans.append(s[i:k])
                cnt += 1
    idx += 1
ans = set(ans)
ans = list(ans)
ans.sort()
print(ans[K-1])

Buggy submission (verdict: Runtime Error):
s = input()
K = int(input())
head = [h for h in s]

head = set(head)
head = list(head)
head.sort()

cnt = 0
ans = []
idx = 0
while cnt <= K:
    for i in range(len(s)):
        if s[i] == head[idx]:
            for k in range(i+1, len(s)+1):
                ans.append(s[i:k])
                cnt += 1
    idx += 1
ans = set(ans)
ans = list(ans)
ans.sort()
print(ans[K-1])

Buggy submission (verdict: Runtime Error):
s = input()
K = int(input())
ans = []
for i in range(len(s)):
    for k in range(i+1, len(s)):
        ans.append(s[i:k])
ans = set(ans)
ans = list(ans)
ans.sort()
print(ans[K-1])
Problem p03361 (python)

Fixed submission:
H, W = map(int, input().split())
S =['.'*(W+2)]+ ['.'+input()+'.' for i in range(H)]+['.'*(W+2)]
flag = True
for i in range(1, H+1):
    for k in range(1, W+1):
        if S[i][k] == '#':
            if S[i-1][k] == '.' and S[i+1][k] == '.' and S[i][k+1] == '.' and S[i][k-1] == '.':
                flag = False
                break
    if not flag:
        break
if flag:
    print('Yes')
else:
    print('No')

Buggy submission (verdict: Runtime Error):
H, W = map(int, input().split())
S =['.']*(W+2)+ ['.'+input()+'.' for i in range(H)]+['.']*(W+2)
flag = True
for i in range(1, H+1):
    for k in range(1, W+1):
        if S[i][k] == '#':
            if S[i-1][k] == '.' and S[i+1][k] == '.' and S[i][k+1] == '.' and S[i][k-1] == '.':
                flag = False
                break
    if not flag:
        break
if flag:
    print('Yes')
else:
    print('No')
Problem p03393 (python)

Fixed submission:
import collections
S = input()
alp = 'abcdefghijklmnopqrstuvwxyz'
dic = collections.defaultdict(int)
ans = -1
if len(S) <= 25:
    for i in S:
        dic[i] += 1
    for i in alp:
        if dic[i] == 0:
            ans = S + i
            break
else:
    dic2 = {}
    dic3 = {}
    lst = []
    tmp = 0
    flag = False
    for i, j in zip(alp, range(1, 27)):
        dic2[i] = j
        dic3[j] = i
    for i in range(1, 27):
        a2 = S[-i]
        idx = dic2[a2]
        if idx < tmp:
            lst.sort()
            for j in lst:
                if j > idx:
                    ans = S[:-i] + dic3[j]
                    flag = True
                    break
        
        else:
            tmp = idx
            lst.append(idx)
        if flag:
            break

print(ans)

Buggy submission (verdict: Runtime Error):
import collections
S = input()
alp = 'abcdefghijklmnopqrstuvwxyz'
dic = collections.defaultdict(int)
ans = -1
if len(S) <= 25:
    for i in S:
        dic[i] += 1
    for i in alp:
        if dic[i] == 0:
            ans = S + i
            break
else:
    dic2 = {}
    dic3 = {}
    lst = []
    tmp = 0
    for i, j in zip(alp, range(1, 27)):
        dic2[i] = j
        dic3[j] = i
    for i in range(1, 27):
        a = S[-i]
        idx = dic2[a]
        if idx < tmp:
            lst.sort()
            for j in lst:
                if j > idx:
                    ans = S[:-i] + dic3[j]
                    flag = True
                    break
        
        else:
            tmp = idx
            lst.append(idx)
        if flag:
            break


print(ans)
Problem p03436 (python)

Fixed submission:
H, W = map(int, input().split())
g = [input() for _ in range(H)]
white = 0
black = 0
for i in range(H):
    for j in range(W):
        if g[i][j] == '#':
            black += 1
        else:
            white += 1
start = (0, 0)
goal = (H-1, W-1)
S = [start]
T = {start: 1}
while S and goal not in T:
    S2 = []
    for x, y in S:
        for pos in [(x+1, y), (x-1, y), (x, y+1), (x, y-1)]:
            x2, y2 = pos
            if 0<= x2 <= H-1 and 0<= y2 <= W-1:
                if g[x2][y2] != '#' and pos not in T:
                    T[pos] = T[(x, y)] + 1
                    S2.append(pos)
    S = S2
if goal not in T:
    print(-1)
else:
    print(white - T[goal])

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**7)
H, W = map(int, input().split())
g = [input() for _ in range(H)]
white = 0
black = 0
for i in range(H):
    for j in range(W):
        if g[i][j] == '#':
            black += 1
        else:
            white += 1
[[0]*W for _ in range(H)]
dist = 10**9
def dfs(x, y,visit, n):
    if x < 0 or x >= H or y < 0 or y >= W or g[x][y] == '#':
        return 0
    elif x == H-1and y == W-1:
        global dist
        if n < dist:
            dist = n
        return 0
    elif visit[x][y]:
        return 0
    else:
        visit[x][y] = 1
        dfs(x+1, y, n+1)
        dfs(x, y+1, n+1)
        dfs(x-1, y, n+1)
        dfs(x, y-1, n+1)
dfs(0, 0,[[0]*W for _ in range(H)] ,1)
if dist == 10**9:
    print(-1)
else:
    print(white - dist)

Buggy submission (verdict: Runtime Error):
H, W = map(int, input().split())
g = [input() for _ in range(H)]
white = 0
black = 0
for i in range(H):
    for j in range(W):
        if g[i][j] == '#':
            black += 1
        else:
            white += 1
visit =[[0]*W for _ in range(H)]
dist = 10**9
def dfs(x, y, n):
    if x < 0 or x >= H or y < 0 or y >= W or g[x][y] == '#':
        return 0
    elif x == H-1and y == W-1:
        visit[x][y] = 1
        global dist
        if n < dist:
            dist = n
    elif visit[x][y]:
        return 0
    else:
        visit[x][y] = 1
        dfs(x+1, y, n+1)
        dfs(x-1, y, n+1)
        dfs(x, y+1, n+1)
        dfs(x, y-1, n+1)
dfs(0, 0, 1)
if not visit[H-1][W-1]:
    print(-1)
else:
    print(white - dist)
Problem p03469 (python)

Fixed submission:
S = input()
print('2018'+S[4:])

Buggy submission (verdict: Runtime Error):
n, y = map(int, input().split())
ans = []
for i in range(n+1):
    for j in range(n+1-i):
        k = n-i-j
        if 10000*i + 5000*j + 1000*k == y and k >=0:
            ans.append([i,j,k])
            break
                
if len(ans) >=1:
    str_ans = map(str, ans[0])
    print(' '.join(str_ans))
if len(ans) == 0:
    print('-1 -1 -1')
Problem p03476 (python)

Fixed submission:
import math
def primes(n):
    a = list(range(2, n+1))
    primes = [2]
    for i in a:
        flag = True
        m = math.sqrt(i)
        for j in primes:
            if j > m:
                break
            else:
                if i % j == 0:
                    flag = False
                    break
        if flag:
            primes.append(i)
    return primes
primes = primes(10**5)
anss = []
for p in primes:
    if (p+1)//2 in primes:
        anss.append(p)
dp = [0]*(10**5+1)
cnt = 0
for i in range(1, 10**5+1, 2):
    if i in anss:
        cnt += 1
    dp[i] = cnt

    
Q = int(input())
for _ in range(Q):
    l, r = map(int, input().split())
    print(dp[r]-dp[l-2])

Buggy submission (verdict: Runtime Error):
import math
def primes(n):
    a = list(range(2, n+1))
    primes = [2]
    for i in a:
        flag = True
        m = math.sqrt(i)
        for j in primes:
            if j > m:
                break
            else:
                if i % j == 0:
                    flag = False
                    break
        if flag:
            primes.append(i)
    return primes
primes = primes(10**5)
dp = [0]*(10**5+1)
cnt = 0
for i in range(1, 10**5+1, 2):
    if i in primes:
        if (i+1)//2 in primes:
            cnt += 1
    dp[i] = cnt
 
    
Q = int(input())
for _ in range(Q):
    l, r = map(int, input().split())
    print(dp[r]-dp[l-2])
import math
def primes(n):
    a = list(range(2, n+1))
    primes = [2]
    for i in a:
        flag = True
        m = math.sqrt(i)
        for j in primes:
            if j > m:
                break
            else:
                if i % j == 0:
                    flag = False
                    break
        if flag:
            primes.append(i)
    return primes
primes = primes(10**5)
dp = [0]*(10**5+1)
cnt = 0
for i in range(1, 10**5+1, 2):
    if i in primes:
        if (i+1)//2 in primes:
            cnt += 1
    dp[i] = cnt

    
Q = int(input())
for _ in range(Q):
    l, r = map(int, input().split())
    print(dp[r]-dp[l-2])
提出情報
Problem p03565 (python)

Fixed submission:
S = input()
T = input()
n = len(S)
t = len(T)
ans = ''
if t > n:
    flag = False
for i in range(t, n+1):
    flag = True
    for j in range(t):
        if S[-i+j] != '?' and S[-i+j] != T[j]:
            flag = False
            break
    if flag:
        break
if flag:
    for s in S[:-i]:
        if s == '?':
            ans += 'a'
        else:
            ans += s
    ans += T
    if i != t:
        for s in S[-i+t:]:
            if s == '?':
                ans += 'a'
            else:
                ans += s
    print(ans)
else:
    print('UNRESTORABLE')

Buggy submission (verdict: Runtime Error):
S = input()
T = input()
n = len(S)
t = len(T)
ans = ''
for i in range(t, n+1):
    flag = True
    for j in range(t):
        if S[-i+j] != '?' and S[-i+j] != T[j]:
            flag = False
            break
    if flag:
        break
if flag:
    for s in S[:-i]:
        if s == '?':
            ans += 'a'
        else:
            ans += s
    ans += T
    if i != t:
        for s in S[-i+t:]:
            if s == '?':
                ans += 'a'
            else:
                ans += s
    print(ans)
else:
    print('UNRESTORABLE')

Buggy submission (verdict: Runtime Error):
S = input()
T = input()
n = len(S)
t = len(T)
ans = ''
for i in range(t, n+1):
    flag = True
    for j in range(t):
        if S[-i+j] != '?' and S[-i+j] != T[j]:
            flag = False
            break
    if flag:
        break
if flag:
    for s in S[:-i]:
        if s == '?':
            ans += 'a'
        else:
            ans += s
    ans += T
    print(ans)
else:
    print('UNRESTORABLE')
Problem p03612 (python)

Fixed submission:
N = int(input())
ps = list(map(int, input().split()))
state = 0
ans = 0
for i in range(N):
    if state:
        state = 0
        continue
    if ps[i] == i+1:
        ans += 1
        state = 1
print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())
ps = list(map(int, input().split()))
ans = 0
for i in range(N):
    if ps[i] == i+1:
        ps[i] = ps[i+1]
        ps[i+1] = i+1
        ans += 1
print(ans)
Problem p03048 (python)

Fixed submission:
r,g,b,N=map(int,input().split())
num=0
for i in range(N+1):
  for j in range (N+1):
    s=(N-i*r-j*g)
    if s>=0 and s%b==0:
      num+=1
print(num)

Buggy submission (verdict: Runtime Error):
r,g,b,n=map(int,input().split())
num=0
fot i in range(g+1):
  for j in range (b+1):
    if i+j>N:
      break
    if 0<=N-i-j<=r:
      num+=1
print(num)
Problem p03132 (python)

Fixed submission:
N=int(input())
A=[int(input()) for i in range(N)]
B=[0]*N
for i in range(N):
    if A[i]>0:
        if A[i]%2==0:
            B[i]=2
        else:
            B[i]=1
L=[0,2,1,2,0]
dp=[[10**9]*5 for i in range(N+1)]
for k in range(5):
    dp[0][k]=0
for i in range(1,N+1):
    for k in range(5):
        for k1 in range(k+1):
            if 1<=k<=3:
                dp[i][k]=min(dp[i][k],dp[i-1][k1]+abs(L[k]-B[i-1]))
            else:
                dp[i][k]=min(dp[i][k],dp[i-1][k1]+A[i-1])
ans=10**9
#print(B)
#print(dp)
for a in range(5):
    ans=min(ans,dp[N][a])
print(ans)

Buggy submission (verdict: Runtime Error):
N=int(input())
A=[int(input()) for i in range(N)]
B=[0]*N
for i in range(N):
    if A[i]>0:
        if A[i]%2==0:
            B[i]=2
        else:
            B[i]=1
L=[0,2,1,2,0]
dp=[[10**9]*5 for i in range(N+1)]
for k in range(5):
    dp[0][k]=0
for i in range(1,N+1):
    for k in range(5):
        for k1 in range(k+1):
            if 1<=k<=3:
                dp[i][k]=min(dp[i][k],dp[i-1][k1]+abs(L[k]-B[i-1]))
            else:
                dp[i][k]=min(dp[i][k],dp[i-1][k1]+A[i-1])
ans=10**9
#print(B)
#print(dp)
for a in range(5):
    ans=min(ans,dp[N][a])
print(ans)v
Problem p03152 (python)

Fixed submission:
import bisect
import sys
from collections import defaultdict
N,M=map(int,input().split())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]
dd=defaultdict(int)
for a in A:
    dd[a]+=1
for b in B:
    dd[b]+=1
for d,n in dd.items():
    if n>=3:
        print(0)
        sys.exit()
A=sorted(A)
B=sorted(B)
ans=1
mod=10**9+7
for i in range(N*M,0,-1):
    x=bisect.bisect_left(A,i)
    y=bisect.bisect_left(B,i)
    if x>=N or y>=M:
        print(0)
        sys.exit()
    if A[x]==i and B[y]==i:
        #print(i)
        continue
    elif A[x]==i:
        ans*=(M-y)
        ans%=mod
    elif B[y]==i:
        ans*=(N-x)
        ans%=mod
    else:
        ans*=((N-x)*(M-y)-(N*M-i))
        ans%=mod
    #print(x,y,i,ans)
print(ans)

Buggy submission (verdict: Runtime Error):
import bisect
import sys
from collections import defaultdict
N,M=map(int,input().split())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]
dd=defaultdict(int)
for a in A:
    dd[a]+=1
for b in B:
    dd[b]+=1
for d,n in dd.items():
    if n>=3:
        print(0)
        sys.exit()
A=sorted(A)
B=sorted(B)
ans=1
mod=10**9+7
for i in range(N*M,0,-1):
    x=bisect.bisect_left(A,i)
    y=bisect.bisect_left(B,i)
    if A[x]==i and B[y]==i:
        #print(i)
        continue
    elif A[x]==i:
        ans*=(M-y)
        ans%=mod
    elif B[y]==i:
        ans*=(N-x)
        ans%=mod
    else:
        ans*=((N-x)*(M-y)-(N*M-i))
        ans%=mod
    #print(x,y,i,ans)
print(ans)
Problem p03175 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10**7)
N=int(input())
table=[[] for i in range(N)]
for i in range(N-1):
    w,v=map(int,input().split())
    table[w-1].append(v-1)
    table[v-1].append(w-1)
mod=10**9+7
f=[-1]*N
g=[-1]*N
def ser(par,son):
    if f[son]!=-1 and g[son]!=-1:
        return f[son],g[son]
    s=1
    t=1
    for grason in table[son]:
        if grason==par:
            continue
        x,y=ser(son,grason)
        s*=x
        s%=mod
        t*=(x+y)
        t%=mod
    f[son],g[son]=t,s
    return t,s
z=1
w=1
for i in table[0]:
    ser(0,i)
    z*=f[i]
    z%=mod
    w*=(f[i]+g[i])
    w%=mod
print((z+w)%mod)

Buggy submission (verdict: Runtime Error):
N=int(input())
table=[[] for i in range(N)]
for i in range(N-1):
    w,v=map(int,input().split())
    table[w-1].append(v-1)
    table[v-1].append(w-1)
mod=10**9+7
f=[-1]*N
g=[-1]*N
def ser(par,son):
    if f[son]!=-1 and g[son]!=-1:
        return f[son],g[son]
    s=1
    t=1
    for grason in table[son]:
        if grason==par:
            continue
        x,y=ser(son,grason)
        s*=x
        s%=mod
        t*=(x+y)
        t%=mod
    f[son],g[son]=t,s
    return t,s
z=1
w=1
for i in table[0]:
    ser(0,i)
    z*=f[i]
    z%=mod
    w*=(f[i]+g[i])
    w%=mod
print((z+w)%mod)
Problem p03216 (python)

Fixed submission:
N=int(input())
S=input()
Q=int(input())
K=[int(i) for i in input().split()]
def f(k):
    num=0
    L=[0,0,0]
    for i in range(N):
        if i-k>=0:
            if S[i-k]=='D':
                L[0]-=1
                L[2]-=L[1]
            elif S[i-k]=='M':
                L[1]-=1
        if S[i]=='D':
            L[0]+=1
        elif S[i]=='M':
            L[1]+=1
            L[2]+=L[0]
        elif S[i]=='C':
            num+=L[2]
        #print(i,':',L,':',num)
    return num
for k in K:
    print(f(k))

Buggy submission (verdict: Runtime Error):
N=int(input())
S=input()
Q=int(input())
K=[int(i) for i in input().split()
def f(k):
    num=0
    L=[0,0,0]
    for i in range(N):
        if i-k>=0:
            if S[i-k]=='D':
                L[0]-=1
                L[2]-=L[1]
            elif S[i-k]=='M':
                L[1]-=1
        if S[i]=='D':
            L[0]+=1
        elif S[i]=='M':
            L[1]+=1
            L[2]+=L[0]
        elif S[i]=='C':
            num+=L[2]
        #print(i,':',L,':',num)
    return num
for k in K:
    print(f(k))