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

Fixed submission:
#!/usr/bin/env python3
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right

def main():
    X, Y, A, B, C = map(int, input().split())
    p = sorted(list(map(int, input().split())), reverse=True)
    q = sorted(list(map(int, input().split())), reverse=True)
    r = sorted(list(map(int, input().split())), reverse=True)

    now = sum(p[:X]) + sum(q[:Y])
    ans = now
    i = X-1
    j = Y-1

    for k in range(min(C,X+Y)):
        if i < 0:
            now += r[k]-q[j]
            j -= 1
        elif j < 0:
            now += r[k]-p[i]
            i -= 1
        elif p[i] < q[j]:
            now += r[k]-p[i]
            i -= 1
        else:
            now += r[k]-q[j]
            j -= 1
        ans = max(ans,now)
    print(ans)

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right

def main():
    X, Y, A, B, C = map(int, input().split())
    p = sorted(list(map(int, input().split())), reverse=True)
    q = sorted(list(map(int, input().split())), reverse=True)
    r = sorted(list(map(int, input().split())), reverse=True)

    ans = 0
    i = 0 # pのindex
    j = 0 # qのindex
    k = 0 # rのindex
    for l in range(X):
        if p[i] < r[k] and k < C:
            ans += r[k]
            k += 1
        else:
            ans += p[i]
            i += 1
            

    for l in range(Y):
        if q[j] < r[k] and k < C:
            ans += r[k]
            k += 1
        else:
            ans += q[j]
            j += 1
    print(ans)

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right

def main():
    X, Y, A, B, C = map(int, input().split())
    p = sorted(list(map(int, input().split())), reverse=True)
    q = sorted(list(map(int, input().split())), reverse=True)
    r = sorted(list(map(int, input().split())), reverse=True)

    ans = 0
    i = 0 # pのindex
    j = 0 # qのindex
    k = 0 # rのindex
    for l in range(X):
        if p[i] >= r[k] or k == C:
            ans += p[i]
            i += 1
        else:
            ans += r[k]
            k += 1

    for l in range(Y):
        if q[j] >= r[k] or k == C:
            ans += q[j]
            j += 1
        else:
            ans += r[k]
            k += 1
    print(ans)

if __name__ == "__main__":
    main()
Problem p02756 (python)

Fixed submission:
S = input()
N = int(input())
Q = [input().split() for _ in range(N)]
start = ''
end = ''

# Sの向きを定義1:前から0:後ろから
direct = 0
for i in range(N):
  if Q[i][0] =='1':
    direct^=1
  else:
    if direct == int(Q[i][1])-1:
      start += Q[i][2]
    else:
      end += Q[i][2]
      
ans = start[::-1]+S+end
print(ans) if direct == 0 else print(ans[::-1])

Buggy submission (verdict: Runtime Error):
S = input()
N = int(input())
Q = [input().split() for _ in range(N)]
start = ''
end = ''

# Sの向きを定義1:前から0:後ろから
direct = 0
for i in range(N):
  if Q[i][0] =='1':
    direct^=1
  else:
    if direct == int(Q[i][1])-1:
      start += Q[i][2]
      print('start',start)
    else:
      end += Q[i][2]
      print('end',end)
      
ans = start[::-1]+S+end
print(ans) if direct == 0 else print(ans[::-1])
Problem p02765 (python)

Fixed submission:
N,R=map(int,input().split())
print(R)if N>=10 else print(R+100*(10-N))

Buggy submission (verdict: Runtime Error):
N, R = map(int, input().split())
if N >= 10:
    print(R)
else:
    print(R+100*(10-K))
Problem p02772 (python)

Fixed submission:
N=int(input())
A=list(map(int,input().split()))
flag=0
for i in range(N):
  if A[i]%2 ==0:
    if A[i]%3==0 or A[i]%5==0:
      pass
    else:
      flag=1
print(['APPROVED','DENIED'][flag])

Buggy submission (verdict: Runtime Error):
N=int(input())
A=list(map(int,input().split()))
flag=0
for i in range(N):
  if A[i]%2 ==0:
    if A[i]%3==0 or A[i]%5==0:
    else:
      flag=1
print(['APPROVED','DENIED'][flag])
Problem p02783 (python)

Fixed submission:
H,A = map(int,input().split())
print(1 + H // A) if H % A != 0 else print(H // A)

Buggy submission (verdict: Runtime Error):
H,N=map(int,input().split())
print('NYoe s'[H<=sum(list(map(int,input().split())))::2])
Problem p02784 (python)

Fixed submission:
H,N=map(int,input().split())
print('NYoe s'[H<=sum(list(map(int,input().split())))::2])

Buggy submission (verdict: Runtime Error):
H,N=map(int,input().split())
A=[]
for i in range(N):
  A.append(int(input()))
print('YNeos'[H<sum(A)::2])
Problem p02801 (python)

Fixed submission:
print(chr(ord(input())+1))

Buggy submission (verdict: Runtime Error):
print(char(ord(input())+1))
Problem p02817 (python)

Fixed submission:
S,T = map(str,input().split())
print(T+S)

Buggy submission (verdict: Runtime Error):
S = input()
T = input()
print(T+S)
Problem p02900 (python)

Fixed submission:
#!/usr/bin/env python3
import fractions

def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f**2 <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

def main():
    A, B = map(int, input().split())
    C = fractions.gcd(A,B)
    if C == 1:
        print(1)
    else:
        ans = prime_factorize(C)
        print(len(set(ans)) + 1)


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import math

def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f**2 <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

def main():
    A, B = map(int, input().split())
    C = math.gcd(A,B)
    if C == 1:
        print(1)
    else:
        ans = prime_factorize(C)
        print(len(set(ans)) + 1)


if __name__ == "__main__":
    main()
Problem p02909 (python)

Fixed submission:
T = ['Sunny','Cloudy','Rainy']
S = input()
print(T[1]) if S == T[0] else print(T[2]) if S == T[1] else print(T[0])

Buggy submission (verdict: Runtime Error):
T = ['Sunny','Cloudy','Rainy']
S = input()
print(T[1]) if S == T[0] elif print(T[2]) if S == T[1] else print(T[0])
Problem p02921 (python)

Fixed submission:
S = input()
T = input()
print(sum([1 for i in range(3) if S[i] == T[i]]))

Buggy submission (verdict: Runtime Error):
S = input()
T = input()
print(sum([1 for i in range(3) if S[i] == R[i]]))
Problem p02936 (python)

Fixed submission:
import sys
input = sys.stdin.readline #for input speed
sys.setrecursionlimit(10**6)

def DFS(v, parent):
  for u in adj[v]:
    if u != parent:
      v_count[u] += v_count[v]
      DFS(u,v)

N, Q = map(int, input().split())

adj = [[] for _ in range(N)]
v_count = [0] * N

for i in range(N-1):
  a, b = map(int, input().split())
  a -= 1
  b -= 1
  adj[a].append(b)
  adj[b].append(a)# aがbの親とは限らないため

for j in range(Q):
  p, x = map(int, input().split())
  v_count[p-1] += x

# 0起点に親ノードの数字を子ノードに足していく
DFS(0, -1)# 現在のノード, 親ノード
print(*v_count)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**6)

def DFS(v,i,count):
  global ans
  if v == i:# 探索終了
    #print('ans:{}, v:{}'.format(ans,v_count[v]))
    ans[i] = count + v_count[v]
    #print('tmp:{}'.format(tmp))
  for u in adj[v]:
    DFS(u,i,count + v_count[v])

N, Q = map(int, input().split())

adj = [[] for _ in range(N)]
v_count = [0] * N

for i in range(N-1):
  a, b = map(int, input().split())
  adj[a-1].append(b-1)
  adj[b-1].append(a-1)

for j in range(Q):
  p, x = map(int, input().split())
  v_count[p-1] += x

# print(v_count)
ans = [None] * N
for i in range(N):
  # iを探索で見つけ出す, そこまで通った頂点のv_count
  # の総和を出力
  DFS(0,i,0)
print(*ans)
Problem p02975 (python)

Fixed submission:
#!/usr/bin/env python3
import sys
from collections import Counter

def main():
    N = int(input())
    a = list(map(int, input().split()))
    l = Counter(a)
    if len(l)==1 and 0 in l:
        print('Yes')
        exit()
    if N%3==0 and len(l)==2 and 0 in l and l[0] == N//3:
        print('Yes')
        exit()
    if N%3==0 and len(l)==3:
        x,y,z = l.keys()
        if l[x] == l[y] == N//3 and x^y^z == 0:
            print('Yes')
            exit()
    print('No')

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import sys
from collections import Counter

def main():
    N = int(input())
    a = list(map(int, input().split()))
    l = Counter(a)
    if len(l)==1 and 0 in l:
        print('Yes')
        exit()
    if N%3==0 and len(l)==2 and 0 in l and l[0] == N//3:
        print('Yes')
        exit()
    if N%3==0 and len(l)==3:
        x,y,z = l.keys()
        if l[x] == l[y] == N//3 and x^y^z == 0:
            print('Yes')
            exit()
    print('No')


    

if __name__ == '__main__':
    main()
Problem p02982 (python)

Fixed submission:
N,D=map(int,input().split())
c=0
X=[]
for i in range(N):
  X.append(list(map(int,input().split())))

for j in range(N):
  for k in range(j+1,N):
    tmp=sum([(X[j][l]-X[k][l])**2 for l in range(D)])
    if tmp in [y**2 for y in range(130)]:
      c +=1
print(c)

Buggy submission (verdict: Runtime Error):
N,D=map(int,input().split())
c=0
X=[]
for i in range(N):
  X.append(list(map(int,input().split())))

for j in range(N):
  for k in range(j+1,N):
    tmp=sum([(X[j][l]-X[k][l])**2 for l in range(N)])
    if tmp in [y**2 for y in range(21)]:
      c +=1
print(c)
Problem p03006 (python)

Fixed submission:
#!/usr/bin/env python3
def main():
    N = int(input())
    if N == 1:print(1);exit();
    x = [None] * N
    y = [None] * N
    for i in range(N):
        x[i], y[i] = map(int, input().split())
    
    diff = []
    nums = []
    for i in range(N):
        for j in range(N):
            if i == j:
                continue
            if (x[j]-x[i],y[j]-y[i]) in diff:
                nums[diff.index((x[j]-x[i],y[j]-y[i]))] += 1
            else:
                diff.append((x[j]-x[i],y[j]-y[i]))
                nums.append(1)
    # print(diff)
    # print(nums)
    print(N-max(nums))

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right

def main():
    N = int(input())
    x = [None] * N
    y = [None] * N
    for i in range(N):
        x[i], y[i] = map(int, input().split())
    
    diff = []
    nums = []
    for i in range(N):
        for j in range(i+1,N):
            if (x[j]-x[i],y[j]-y[i]) in diff:
                nums[diff.index((x[j]-x[i],y[j]-y[i]))] += 1
            else:
                diff.append((x[j]-x[i],y[j]-y[i]))
                nums.append(1)
    # print(diff)
    # print(nums)
    print(N-max(nums))
    
    

if __name__ == "__main__":
    main()
Problem p03023 (python)

Fixed submission:
print(180*(int(input())-2))

Buggy submission (verdict: Runtime Error):
print(180*(int(input())-2)
Problem p03044 (python)

Fixed submission:
#!/usr/bin/env python3
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right

def main():
    N = int(input())
    adj = [[] for _ in range(N)]
    for i in range(N-1):
        u, v, w = map(int, input().split())
        adj[u-1].append((v-1,w))
        adj[v-1].append((u-1,w))
    
    queue = deque([0])
    visit = [-1] * N
    visit[0] = 1
    
    while queue:
        now = queue.popleft()
        for u,w in adj[now]:
            if visit[u] < 0:
                queue.append(u)
                if w % 2 == 0:
                    visit[u] = visit[now]
                else:
                    visit[u] = (visit[now]+1) % 2
    [print(v) for v in visit]


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right

# 入力
N = int(input())
adj = [[] for _ in range(N)]
ans = [None] * N

# グラフ探索で解いてみる
# c: 頂点vの色(0or1)
def dfs(v,parent,c):
    ans[v] = c
    for u,w in adj[v]:
        if u == parent:
            continue
        dfs(u,v,1-c) if w & 1 else dfs(u,v,c)

def main():
    for i in range(N-1):
        u, v, w = map(int, input().split())
        adj[u-1].append((v-1,w))
        adj[v-1].append((u-1,w))
    
    dfs(0,-1,1)
    [print(v) for v in ans]

if __name__ == "__main__":
    main()
Problem p03061 (python)

Fixed submission:
#!/usr/bin/env python3
from fractions import gcd

def main():
    N = int(input())
    A = list(map(int, input().split()))
    L = [0] * (N+1)
    R = [0] * (N+1)
    for i in range(N):
        L[i+1] = gcd(L[i], A[i])
        R[N-1-i] = gcd(R[N-i], A[N-1-i])
    ans = 0
    for i in range(N):
        ans = max(ans, gcd(L[i], R[i+1]))
    print(ans)

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import math

def main():
    N = int(input())
    A = list(map(int, input().split()))
    L = [0] * (N+1)
    R = [0] * (N+1)
    for i in range(N):
        L[i+1] = math.gcd(L[i], A[i])
        R[N-1-i] = math.gcd(R[N-i], A[N-1-i])
    ans = 0
    for i in range(N):
        ans = max(ans, math.gcd(L[i], R[i+1]))
    print(ans)

if __name__ == "__main__":
    main()
Problem p03069 (python)

Fixed submission:
#!/usr/bin/env python3
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right

def main():
    N = int(input())
    S = input()
    B = []
    W = []
    i = 0
    fin = None
    while i < N:
        if S[i] == '#':
            if i == N-1:
                B.append(1)
                fin = True
            else:
                tmp = 1
                while S[i+1] == '#':
                    tmp += 1
                    i += 1
                    if i == N-1:
                        fin = True
                        break
                B.append(tmp)
        else:
            if i == N-1:
                W.append(1)
                fin = False
            else:
                tmp = 1
                while S[i+1] == '.':
                    tmp += 1
                    i += 1
                    if i == N-1:
                        fin = False
                        break
                W.append(tmp)
        i += 1

    ans = sum(B)
    L = len(B)+len(W)
    B = B[::-1]
    W = W[::-1]
    now = ans
    if fin:
        for i in range(L):
            if i % 2 == 0:
                now -= B[i//2]
            else:
                now += W[i//2]
            ans = min(ans,now)
    else:
        for i in range(L):
            if i % 2 == 1:
                now -= B[i//2]
            else:
                now += W[i//2]
            ans = min(ans,now)
    print(ans)



    

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right

def main():
    N = int(input())
    S = input()
    B = []
    W = []
    i = 0
    fin = None
    while i < N:
        if S[i] == '#':
            if i == N-1:
                B.append(1)
                fin = True
            else:
                tmp = 1
                while S[i+1] == '#':
                    tmp += 1
                    i += 1
                    if i == N-1:
                        break
                B.append(tmp)
        else:
            if i == N-1:
                W.append(1)
                fin = False
            else:
                tmp = 1
                while S[i+1] == '.':
                    tmp += 1
                    i += 1
                    if i == N-1:
                        break
                W.append(tmp)
        i += 1

    # print(B,W)
    ans = sum(B)
    L = len(B)+len(W)
    now = ans
    if fin:
        for i in range(L):
            if i % 2 == 0:
                now -= B[i//2]
            else:
                now += W[i//2]
            ans = min(ans,now)
    else:
        for i in range(L):
            if i % 2 == 1:
                now -= B[i//2]
            else:
                now += W[i//2]
            ans = min(ans,now)
    print(ans)



    

if __name__ == "__main__":
    main()
Problem p03074 (python)

Fixed submission:
#!/usr/bin/env python3
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right
from itertools import accumulate

def main():
    N, K = map(int, input().split())
    S = input()
    nums = []
    # 1の個数、0の個数を交互に記録していく
    if S[0] == '0': nums.append(0)
    i = 0
    while i < N:
        j = i
        while j < N and S[i]==S[j]:
            j += 1
        nums.append(j-i)
        i = j
    if S[-1] == '0': nums.append(0)
    # print(nums)
    nums = [0] + list(accumulate(nums))
    # print(nums)
    ans = 0
    
    for i in range(0,len(nums),2):
        # print(i)
        ans = max(ans,nums[min(i+2*K+1,len(nums)-1)]-nums[i])
    print(ans)
        

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
N,K = map(int,input().split())
S = input()
N = len(S)
flag = int(S[0])
S_1 = [len(x) for x in S.split('0') if len(x) is not 0]
S_0 = [len(x) for x in S.split('1') if len(x) is not 0]

count = 0
for i in range(len(S_0)-K+1):
  if flag == 0:
    if i == 0:
      tmp = 0
    else:
      tmp = S_1[i-1]
    for j in range(i,i+K):
      if j == len(S_1):
        tmp += S_0[j]
      else:
        tmp += (S_0[j] + S_1[j])
  else:
    tmp = S_1[i+K]
    for j in range(i,i+K):
      tmp += (S_1[j] + S_0[j])
    
  if tmp > count:
    count = tmp
    
if len(S_0) == 0:
  print(S_1[0])
elif len(S_1) == 0:
  print(S_0[0])
else:
  print(count)

Buggy submission (verdict: Runtime Error):
N,K = map(int,input().split())
S = input()
N = len(S)
flag = int(S[0])
S_1 = [len(x) for x in S.split('0') if len(x) is not 0]
S_0 = [len(x) for x in S.split('1') if len(x) is not 0]

count = 0
for i in range(len(S_0)-K+1):
  if flag == 0:
    if i == 0:
      tmp = 0
    else:
      tmp = S_1[i-1]
    for j in range(i,i+K):
      if j == len(S_1):
        tmp += S_0[j]
      else:
        tmp += (S_0[j] + S_1[j])
  else:
    tmp = S_1[i+K]
    for j in range(i,i+K):
      tmp += (S_1[j] + S_0[j])
    
  if tmp > count:
    count = tmp
print(count)
Problem p03103 (python)

Fixed submission:
def unzip(li):
  return map(list, zip(*li))

N, M = map(int, input().split())
tmp_C = [0 for i in range(N)]

for i in range(N):
  a,b = map(int, input().split())
  tmp_C[i] = (a,b)
  
C = sorted(tmp_C)
A, B = unzip(C)
ans = 0
count = 0
cur = 0
while(count < M):
  min_A = A[cur]
  B_min = B[cur]
  if count + B_min >= M:
    print(ans + min_A * (M - count))
    break
  else:
    ans += min_A * B_min
    count += B_min
    cur += 1

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
A = [0 for i in range(N)]
B = [0 for i in range(N)]

for i in range(N):
  a,b = map(int, input().split())
  A[i] = a
  B[i] = b

ans = 0
count = 0
while(count < M):
  min_A = min(A)
  min_Ai = A.index(min_A)
  B_min = B[min_Ai]
  if count + B_min >= M:
    print(ans + min_A * (M - count))
    break
  else:
    ans += min_A * B_min
    count += B_min
    A.remove(min_A)
    B.remove(min_A)
Problem p03126 (python)

Fixed submission:
N, M = map(int, input().split())
ans = [0 for i in range(M)]
for i in range(N):
  tmp = list(map(int, input().split()))
  #print(tmp)
  for j in range(tmp[0]): #K
    ans[tmp[j+1]-1] += 1

count = 0
for k in ans:
  if k == N:
    count += 1
print(count)

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
ans = [0 for i in range(M)]
for i in range(N):
  tmp = list(map(int, input().split()))
  #print(tmp)
  for j in range(tmp[0]): #K
    ans[tmp[j]] += 1

count = 0
for k in ans:
  if k == N:
    count += 1
print(count)
Problem p03140 (python)

Fixed submission:
N = int(input())
A = [input() for i in range(3)]
ans = 0
for i in range(N):
  ans += len(set([A[0][i],A[1][i],A[2][i]]))-1
print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())
A = [input() for i in range(N)]
ans = 0
for i in range(N):
  ans += len(set(A[0][i],A[1][i],A[2][i]))-1
print(ans)
Problem p03156 (python)

Fixed submission:
N = int(input())
A, B = map(int, input().split())
P = list(map(int, input().split()))
a = len([*filter(lambda x: x<=A,P)])
b = len([*filter(lambda x: A<x<=B,P)])
c = len([*filter(lambda x: B<x,P)])
print(min(a,b,c))

Buggy submission (verdict: Runtime Error):
N = int(input())
A, B = map(int, input().split())
P = list(map(int, input().split()))
a = len(filter(lambda x: x<=A,P))
b = len(filter(lambda x: A<x<=B,P))
c = len(filter(lambda x: B<x,P))
print(min(a,b,c))
Problem p03160 (python)

Fixed submission:
#!/usr/bin/env python3
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right

def main():
    N = int(input())
    h = list(map(int, input().split()))
    INF = 10**12
    dp = [INF] * (N+1)
    dp[1] = 0
    for i in range(1,N):
        dp[i+1] = min(dp[i+1],dp[i]+abs(h[i-1]-h[i]))
        if i < N-1:
            dp[i+2] = min(dp[i+2],dp[i]+abs(h[i-1]-h[i+1]))
    print(dp[N])

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
def main():
    N, K = map(int, input().split())
    h = list(map(int, input().split()))
    INF = 10**12
    dp = [INF] * (N+1)
    dp[1] = 0
    for i in range(1,N):
        for j in range(1,K+1):
            if i+j > N:
                break
            else:
                dp[i+j] = min(dp[i+j],dp[i]+abs(h[i-1]-h[i+j-1]))
    print(dp[N])

if __name__ == "__main__":
    main()
Problem p03165 (python)

Fixed submission:
#!/usr/bin/env python3
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right
import sys
sys.setrecursionlimit(10**6)

s = input()
t = input()
N = len(s)
M = len(t)
ans = []

def print_lcs(dp,i,j):
    global ans
    if i == 0 or j == 0:
        return
    if s[i-1] == t[j-1]:
        print_lcs(dp,i-1,j-1)
        ans.append(s[i-1])
    else:
        if dp[i-1][j] >= dp[i][j-1]:
            print_lcs(dp,i-1,j)
        else:
            print_lcs(dp,i,j-1)


def main():
    dp = [[0]*(M+1) for _ in range(N+1)]
    
    for i in range(N):
        for j in range(M):
            if s[i] == t[j]:
                dp[i+1][j+1] = dp[i][j] + 1
            else:
                dp[i+1][j+1] = max(dp[i+1][j], dp[i][j+1])
    
    print_lcs(dp,N,M)
    print(''.join(ans))

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right

s = input()
t = input()
N = len(s)
M = len(t)
ans = []

def print_lcs(dp,i,j):
    global ans
    if i == 0 or j == 0:
        return
    if s[i-1] == t[j-1]:
        print_lcs(dp,i-1,j-1)
        ans.append(s[i-1])
    else:
        if dp[i-1][j] >= dp[i][j-1]:
            print_lcs(dp,i-1,j)
        else:
            print_lcs(dp,i,j-1)


def main():
    dp = [[0]*(M+1) for _ in range(N+1)]
    
    for i in range(N):
        for j in range(M):
            if s[i] == t[j]:
                dp[i+1][j+1] = dp[i][j] + 1
            else:
                dp[i+1][j+1] = max(dp[i+1][j], dp[i][j+1])
    
    print_lcs(dp,N,M)
    print(''.join(ans))

if __name__ == "__main__":
    main()