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

Fixed submission:
import functools


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def solve(inputs):
    a = list(map(lambda x: int(x), inputs))

    count = 0
    light = 1
    N = len(a)
    while 1:
        next_light = a[light - 1]
        count += 1
        if next_light == 2:
            return count
        elif count > N:
            return -1
        light = next_light


if __name__ == "__main__":
    N = int(input())
    ret = solve(inputs(N))
    print(ret)

Buggy submission (verdict: Runtime Error):
import functools


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def solve(inputs):
    a = list(map(lambda x: int(x), inputs))
    a[1] = -1
    return wh(a, 2, 0, len(a))


def wh(a, search, count, N):
    counts = []

    if search == 1:
        return count
    elif count > N:
        return -1

    for i, value in enumerate(a):
        if value == search:
            counts.append(wh(a, i + 1, count + 1, N))

    counts_ignore_minus = list(filter(lambda x: x != -1, counts))

    if len(counts_ignore_minus) == 0:
        return -1

    min = counts_ignore_minus[0]
    for c in counts_ignore_minus:
        if min > c:
            min = c
    return min


if __name__ == "__main__":
    N = int(input())
    ret = solve(inputs(N))
    print(ret)

Buggy submission (verdict: Runtime Error):
import functools


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def solve(inputs):
    a = list(map(lambda x: int(x), inputs))
    a[1] = -1
    return wh(a, 2, 0)


def wh(a, search, count):
    counts = []

    if search == 1:
        return count

    for i, value in enumerate(a):
        if value == search:
            counts.append(wh(a, i + 1, count + 1))

    counts_ignore_minus = list(filter(lambda x: x != -1, counts))

    if len(counts_ignore_minus) == 0:
        return -1

    min = counts_ignore_minus[0]
    for c in counts_ignore_minus:
        if min > c:
            min = c
    return min


if __name__ == "__main__":
    N = int(input())
    ret = solve(inputs(N))
    print(ret)
Problem p03828 (python)

Fixed submission:
import sys
from functools import reduce
import copy
import math
from pprint import pprint
import collections
import bisect
import itertools


sys.setrecursionlimit(4100000)


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def int_inputs(num_of_input):
    ins = [int(input()) for i in range(num_of_input)]
    return ins


def solve(inputs):
    N = int(inputs[0])
    SSSS = 1000000007
    count = collections.Counter()

    for i in range(1, N+1):
        target = i
        is_end = False
        while not is_end:
            is_end = True
            for j in range(2, int(math.sqrt(target)+1)):
                if target % j == 0:
                    count[j] += 1
                    target = target // j
                    is_end = False
                    break
            if is_end and target != 1:
                count[target] += 1
    c = 1
    for k, v in count.items():
        c *= (v + 1)
        c = c % SSSS
    return c


def string_to_int(string):
    return list(map(lambda x: int(x), string.split()))


if __name__ == "__main__":
    ret = solve(inputs(1))
    print(ret)

Buggy submission (verdict: Runtime Error):
import sys
from functools import reduce
import copy
import math
from pprint import pprint
import collections
import bisect
import itertools


sys.setrecursionlimit(4100000)


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def int_inputs(num_of_input):
    ins = [int(input()) for i in range(num_of_input)]
    return ins


def solve(inputs):
    N = int(inputs[0])
    SSSS = 1000000007
    count = collections.Counter()

    for i in range(1, N+1):
        target = i
        is_end = False
        while not is_end:
            is_end = True
            for j in range(2, int(math.sqrt(target)+1)):
                if target % j == 0:
                    count[j] += 1
                    target = target // j
                    is_end = False
                    break
            if is_end and target != 1:
                count[target] += 1
    c = 1
    for k, v in count.items():
        c *= (v + 1)
        c = c % SSSS
    return c


def string_to_int(string):
    return list(map(lambda x: int(x), string.split()))


if __name__ == "__main__":
    N = int(input())
    ret = solve(inputs(N-1))
    for r in ret:
        print(r)
Problem p03963 (python)

Fixed submission:
def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def solve(inputs):
    [N, K] = list(map(lambda x: int(x), inputs[0].split()))
    pattern = K
    for i in range(1, N):
        pattern *= K - 1
    return pattern


if __name__ == "__main__":
    ret = solve(inputs(1))
    print(ret)

Buggy submission (verdict: Runtime Error):
import math


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def solve(inputs):
    N = int(inputs[0])
    power = 1
    for i in range(1, N + 1):
        power = (i * power) % (10 ** 9 + 7)
    return power


if __name__ == "__main__":
    ret = solve(inputs(1))
    print(ret)
Problem p02574 (python)

Fixed submission:
import math as mt
n=int(input())
l=list(map(int,input().split()))
gd=l[0]
sv=[0]*(10**6+1)
for i in range(2,len(sv)):
  if not sv[i]:
    sv[i]=i
    for j in range(2*i,len(sv),i):
      sv[j]=i
an="not coprime"
for i in range(n):
      gd=mt.gcd(gd,l[i])
if gd!=1:
	print(an)
else:
    an="setwise coprime"
    pos=1
    vis=[0]*(10**6+2)
    for i in range(n):
      cr=l[i]
      while cr>1:
          dv=sv[cr]
          if vis[dv]:
            pos=0;break
          while cr%dv==0:
            cr//=dv
          vis[dv]=1
    if pos:
      an="pairwise coprime"
    print(an)

Buggy submission (verdict: Runtime Error):
import math as mt
n=int(input())
l=list(map(int,input().split()))
gd=l[0]
sv=[0]*(10**6+1)
for i in range(2,len(sv)+1):
  if not sv[i]:
    sv[i]=i
    for j in range(2*i,len(sv),i):
      sv[j]=i
an="not coprime"
for i in range(n):
      gd=mt.gcd(gd,l[i])
if gd!=1:
	print(an)
else:
    an="setwise coprime"
    pos=1
    vis=[0]*(10**6+2)
    for i in range(n):
      cr=l[i]
      while cr>1:
          dv=sv[cr]
          if vis[dv]:
            pos=0;break
          while cr%dv==0:
            cr//=dv
          vis[dv]=1
    if pos:
      an="pairwise coprime"
    print(an)

Buggy submission (verdict: Runtime Error):
import math as mt
n=int(input())
l=list(map(int,input().split()))
gd=l[0]
sv=[0]*(10**6+1)
for i in range(2,int(len(sv)**0.5)+1):
  if not sv[i]:
    sv[i]=i
    for j in range(2*i,len(sv),i):
      sv[j]=i
an="not coprime"
for i in range(n):
      gd=mt.gcd(gd,l[i])
if gd!=1:
	print(an)
else:
    an="setwise coprime"
    pos=1
    vis=[0]*(10**6+2)
    for i in range(n):
      cr=l[i]
      while cr>1:
          dv=sv[cr]
          if vis[dv]:
            pos=0;break
          while cr%dv==0:
            cr//=dv
          vis[dv]=1
    if pos:
      an="pairwise coprime"
    print(an)
Problem p02576 (python)

Fixed submission:
n,x,t = map(int,input().split())
print((n+x-1)//x*t)

Buggy submission (verdict: Runtime Error):
I = lambda : list(map(int,input().split()))
h,w,m=I()
x={};y={}
l=[];dic={}
for i in range(m):
  a,b=I()
  l.append([a,b])
  dic[(a,b)]=1
  x[a]=x.get(a,0)+1
  y[b]=y.get(b,0)+1
p,q=max(x.values()),max(y.values())
a,b=[i for i in x if x[i]==p],[i for i in y if y[i]==q]
an=p+q;fl=0
for i in a:
  for j in b:
    if dic.get((i,j),-1)==-1:
      fl=1;break
  if fl:
    break
an-=(fl==0)
print(an)
Problem p02687 (python)

Fixed submission:
p=list(input())
p[1]='R' if p[1]=='B' else 'B'
print(''.join(p))

Buggy submission (verdict: Runtime Error):
n,a,b,c = list(map(int,input().split()))
an=0;d={};l=[];x=y=z=0
for i in range(n):
  s=input();l.append(s)
  x+=int(s[0]=='A')
  z+=int(s[-1]=='C')
  y+=int(s[-1]=='B')+int(s[0]=='B')
x=a-x;y=b-y;z=c-z
if (x<0)+(y<0)+(z<0)>=2:
  print("No");exit()
a=[]
for i in range(n):
  if l[i]=='AC':
    if x>=0:
      z+=1;a.append('C')
    else:
      x+=1;a.append('A')
  if l[i]=='AB':
    if x>=0:
      y+=1;a.append('B')
    else:
      x+=1;a.append('A')
  if l[i]=='BC':
    if y>=0:
      z+=1;a.append('C')
    else:
      y+=1;a.append('B')
print("Yes")
for i in a:
  print(i)
Problem p02702 (python)

Fixed submission:
s = input()
p=2019;n=len(s)
digit_rm = [0 for _ in range(n)]
for i in range(n):
	digit_rm[i] = (int(s[n-1-i]) * pow(10, i, p) ) % p

remain = [0 for _ in range(n)]
for i in range(n):
	if i == 0:
		remain[i] = digit_rm[i]
	else:
		remain[i] = (remain[i-1] + digit_rm[i]) % p
pcnt = [0 for _ in range(p)]
for i in remain:
	pcnt[i] += 1
ans = pcnt[0]
for num in pcnt:
	if num != 0:
		ans += num * (num - 1) // 2
print(ans)

Buggy submission (verdict: Runtime Error):
a=2019
for i in range(int(a**0.5)):
  if a%i==0:
    print(i)
    break
Problem p02772 (python)

Fixed submission:
def k(x):
  x=int(x)
  if x%2==0:
    if (x%3==0 or x%5==0):
      return 1
    return 0
  return 1
n=int(input());l=list(map(k,input().split()))
if sum(l)==n:
  print("APPROVED")
else:
  print("DENIED")

Buggy submission (verdict: Runtime Error):
def k(x):
  x=int(x)
  if x%2==0 and (x%3==0 or x%5==0):
	return 1
  return 1
n=int(input());l=list(map(k,input().split()))
if sum(l)==n:
  print("APPROVED")
else:
  print("DENIED")
Problem p02783 (python)

Fixed submission:
h,a = map(int,input().split())
print(h//a+(not not h%a))

Buggy submission (verdict: Runtime Error):
h,a = map(int,input().split())
print(h//a+not not h%a)
Problem p02830 (python)

Fixed submission:
n = int(input())
s,t = input().split()
for i in range(n):
  print(s[i]+t[i],end='')

Buggy submission (verdict: Runtime Error):
n = input()
s,t = input().split()
for i in range(n):
  print(s[i]+t[i],end='')
Problem p02641 (python)

Fixed submission:
x,n = map(int,input().split())
p = list(map(int,input().split()))
tmp = []
for i in range(-101,102):
    tmp.append(i)

if n == 0:
    print(x)
else:
    for i in p:
        tmp.remove(i)
    
    ans = []
    for i in tmp:
        a = abs(x-i)
        ans.append(a)
 
    b = min(ans)
    
    c = x+b
    d = x-b
 
    if c and d in tmp:
        print(d)
    elif c in tmp:
        print(c)
    elif d in tmp:
        print(d)

Buggy submission (verdict: Runtime Error):
x,n = map(int,input().split())
p = list(map(int,input().split()))
tmp = []
for i in range(1,101):
    tmp.append(i)

if n == 0:
    print(x)
else:
    for i in p:
        tmp.remove(i)
    
    ans = []
    for i in tmp:
        a = abs(x-i)
        ans.append(a)

    b = min(ans)

    c = x+b
    d = x-b

    if c and d in tmp:
        print(d)
    elif c in tmp:
        print(c)
    elif d in tmp:
        print(d)

Buggy submission (verdict: Runtime Error):
x,n = map(int,input().split())
p = list(map(int,input().split()))
tmp = []
for i in range(1,101):
    tmp.append(i)

if n == 0:
    print(x)
else:
    for i in p:
        tmp.remove(i)
ans = []
for i in tmp:
    a = abs(x-i)
    ans.append(a)

b = min(ans)

c = x+b
d = x-b

if c and d in tmp:
    print(d)
elif c in tmp:
    print(c)
elif d in tmp:
    print(d)
Problem p02706 (python)

Fixed submission:
N,M = map(int,input().split())
list = [int(M) for M in input().split()]

a = N - sum(list)

if a < 0:
    print("-1")
else:
    print(a)

Buggy submission (verdict: Runtime Error):
n,m = map(int,input().split())
A = list(int(input()).split())
tmp = n-sum(A)

if tmp >= 0:
  print(tmp)
else:
  print(-1)
Problem p02922 (python)

Fixed submission:
A,B = map(int,input().split())
N = -(-(B-A)//(A-1)) + 1
print(N)

Buggy submission (verdict: Runtime Error):
A,B = int(input())
x = (B-A)/(A-1)+1
print(x)
Problem p03086 (python)

Fixed submission:
s = input()
t = [0]*len(s)

for i in range(len(s)):
    if s[i] == 'A' or s[i] == 'C' or s[i] == 'G' or s[i] == 'T':
        t[i] = 1

ans = []
tmp = 0
for j in t:
    if j == 1:
        tmp += 1
    else:
       ans.append(tmp)
       tmp = 0
ans.append(tmp)

print(max(ans))

Buggy submission (verdict: Runtime Error):
s = input()
t = [0]*len(s)

for i in range(len(s)):
    if s[i] == 'A' or s[i] == 'C' or s[i] == 'G' or s[i] == 'T':
        t[i] = 1

ans = []
tmp = 0
for j in t:
    if j == 1:
        tmp += 1
    else:
       ans.append(tmp)
       tmp = 0
print(max(ans))
Problem p03149 (python)

Fixed submission:
n = list(map(str,input().split()))
n.sort()
ans = ''.join(n)

if ans == '1479':
    print('YES')
else:
    print('NO')

Buggy submission (verdict: Runtime Error):
n = list(map(int,input().split()))
n.sort()
ans = ''.join(n)

if ans == 1479:
    print('YES')
else:
    print('NO')
Problem p03324 (python)

Fixed submission:
A = []
B = []
C = []

for i in range(1,101):
    t0 = i
    t1 = i*100
    t2 = i*100*100
    A.append(t0)
    B.append(t1)
    C.append(t2)

d,n = map(int,input().split())

if d == 0:
    if n == 100:
        print(101)
    else:
        print(A[n-1])
elif d == 1:
    if n == 100:
        print(100*101)
    else:
        print(B[n-1])
else:
    if n == 100:
        print(100*100*101)
    else:
        print(C[n-1])

Buggy submission (verdict: Runtime Error):
A = []
B = []
C = []

for i in range(1,100):
    t0 = i
    t1 = i*100
    t2 = i*100*100
    A.append(t0)
    B.append(t1)
    C.append(t2)

d,n = map(int,input().split())

if d == 0:
    print(A[n-1])
elif d == 1:
    print(B[n-1])
else:
    print(C[n-1])
Problem p03455 (python)

Fixed submission:
a,b = map(int,input().split())
ab = a*b

if ab%2 == 0:
    print('Even')
else:
    print('Odd')

Buggy submission (verdict: Runtime Error):
a,b = int(input())
ab = a*b

if ab%2 == 0:
    print('Even')
else:
    print('Odd')
Problem p03591 (python)

Fixed submission:
import sys
s = list(str(input()))
if len(s)<4:
    print('No')
    sys.exit()
if s[0]=='Y' and s[1]=='A' and s[2]=='K' and s[3]=='I':
    print('Yes')
else:
    print('No')

Buggy submission (verdict: Runtime Error):
s = list(str(input()))
if s[0]=='Y' and s[1]=='A' and s[2]=='K' and s[3]=='I':
    print('Yes')
else:
    print('No')

Buggy submission (verdict: Runtime Error):
s = list(map(str,input().split()))
if s[0]=='Y' and s[1]=='A' and s[2]=='K' and s[3]=='I':
    print('Yes')
else:
    print('No')
Problem p03844 (python)

Fixed submission:
a,op,b=input().split()
a = int(a)
b = int(b)

if op == '+':
    print(a+b)
elif op == '-':
    print(a-b)

Buggy submission (verdict: Runtime Error):
a,op,b=input().split
a = int(a)
b = int(b)

if op == '+':
    print(a+b)
elif op == '-':
    print(a-b)
Problem p04030 (python)

Fixed submission:
s = list(input())
a = []
# print(s)
 
for i in range(len(s)):
    if s[i]== "0":
        a.append("0")
    elif s[i] == "1":
        a.append("1")
    else:
        if len(a)> 0:
            a.pop()
print("".join(a))

Buggy submission (verdict: Runtime Error):
s = list(input())
ans = []

for i in s:
    if i == 'B':
        ans.pop()
    else:
        ans.append(i)

print("".join(ans))
Problem p02775 (python)

Fixed submission:
S = input()
dp = [0, 1]
for s in S:
    i = int(s)
    a = min(dp[0] + i,   dp[1] + 10 - i)
    b = min(dp[0] + i+1, dp[1] + 10 - (i+1))
    dp = [a, b]
    # print(dp)
print(dp[0])

Buggy submission (verdict: Runtime Error):
S = input()
ans = 0
for s in S:
    i = int(s)
    ans += min(i, 10-i)

s0, s1 = int(S[0]), int(S[1])
if s1 >= 6:
    ans += 1
elif s1 <=4 and s0 >= 5:
    ans += 1
print(ans)

Buggy submission (verdict: Runtime Error):
import os
import sys
input = sys.stdin.buffer.readline
S = input().rstrip().decode()
ans = 0
for s in S:
    i = int(s)
    ans += min(i, 10-i)

s0, s1 = int(S[0]), int(S[1])
if s1 >= 6:
    ans += 1
elif s1 <=4 and s0 >= 5:
    ans += 1
print(ans)

Buggy submission (verdict: Runtime Error):
import os
import sys
input = sys.stdin.buffer.readline
S = input().rstrip().decode()
ans = 0
for s in S:
    i = int(s)
    ans += min(i, 10-i)
if S[0] == '9' or int(S[1]) >= 6:
    ans += 1
print(ans)

Buggy submission (verdict: Runtime Error):
S = input()
ans = 0
for s in S:
    i = int(s)
    ans += min(i, 10-i)
if S[0] == '9' or int(S[1]) >= 6:
    ans += 1
print(ans)
Problem p02804 (python)

Fixed submission:
mod = 10**9+7
f = [1]
for i in range(10**5+7):
    f.append(f[-1]*(i+1)%mod)

def comb(n, r,mod=mod):
    return f[n] * pow(f[r], mod-2, mod) * pow(f[n-r], mod-2, mod) % mod

n, k = map(int, input().split())
a = sorted(list(map(int, input().split())), reverse=True)

ans = 0
for i in range(n-k+1):
    ans += (a[i] - a[-1-i]) * comb(n-1-i, k-1)
    ans %= mod
print(ans)

Buggy submission (verdict: Runtime Error):
MOD = 10**9+7
N, K = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
# print(A)

factorial = [0] * (N+2)

def fact(n, MOD=MOD):
    global factorial
    if n == 0:
        factorial[n] = 1
        return 1
    res = n * fact(n-1)
    res %= MOD
    factorial[n] = res
    return res

fact(N+1)
# print(fact(N, MOD))
# print(factorial)

def nCr(n, r, mod=10**9+7):
    res = factorial[n]
    res //= factorial[r]
    res //= factorial[n-r]
    res %= MOD
    return res

# # フェルマーの小定理
# def nCr(n, r, mod=10**9+7):
#     r = min(r, n-r)
#     numer = denom = 1
#     for i in range(1, r+1):
#         numer = numer * (n+1-i) % mod
#         denom = denom * i % mod
#     return numer * pow(denom, mod-2, mod) % mod

ans = 0
for i in range(K+1):
    c = nCr(i+K-1, i)
    ans += c * A[N-K-1+i]
    ans -= c * A[K-i]
    ans %= MOD
ans %= MOD
print(ans)
Problem p02807 (python)

Fixed submission:
MOD = 10**9+7

N = int(input())
x = list(map(int, input().split()))

ans = 0
mul = 0
for i in range(N-1):
    mul += pow(i+1, MOD-2, MOD)
    ans += (x[i+1] - x[i]) * mul % MOD
    ans %= MOD
for i in range(2, N):
    ans *= i
    ans %= MOD
print(ans)

Buggy submission (verdict: Runtime Error):
import numpy as np
import math
import sys
readline = sys.stdin.buffer.readline
MOD = 10**9+7

N = int(readline())
x = np.array(readline().split(), dtype=np.int64)
# print(N, x)

dif = x[1:] - x[:N-1]
# print(dif)

cnt = np.zeros(N-1, dtype=np.int64)
sm = math.factorial(N-1)
for i in range(N-1):
    if i == 0:
        cnt[i] = sm
        tmp = sm
    else:
        tmp = tmp * i // (i+1)
        cnt[i] = cnt[i-1] + tmp
# print(cnt)

sums = np.mod((dif * cnt), MOD)
# print(sms)
ans = 0
for i in range(N-1):
    ans += sums[i]
    ans %= MOD
print(ans)

Buggy submission (verdict: Runtime Error):
import numpy as np
import math
import sys
readline = sys.stdin.buffer.readline
MOD = 10**9+7

N = int(readline())
x = np.array(readline().split(), dtype=np.int64)
# print(N, x)

dif = x[1:] - x[:N-1]
# print(dif)

cnt = np.zeros(N-1, dtype=np.int64)
sm = math.factorial(N-1)
for i in range(N-1):
    if i == 0:
        cnt[i] = sm
        tmp = sm
    else:
        tmp = tmp * i // (i+1)
        cnt[i] = cnt[i-1] + tmp
# print(cnt)

ans = np.mod((dif * cnt), MOD)
# print(ans)
ans = ans.sum() % MOD
print(ans)

Buggy submission (verdict: Runtime Error):
import numpy as np
import math
import sys
readline = sys.stdin.buffer.readline
MOD = 10**9+7

N = int(readline())
x = np.array(readline().split(), dtype=np.int64)
# print(N, x)

# dif = np.zeros((N-1,N-1), dtype=np.int64)
dif = np.fmax(x[1:].reshape(-1, N-1) - x[:N-1].reshape(N-1, -1), 0)
# print(dif)

cnt = np.zeros((N-1,N-1), dtype=np.int64)
for i in range(N-1):
    for j in range(i+1):
        if i == 0:
            cnt[N-2-i, N-2-j] = math.factorial(N-1)
        elif j == 1:
            cnt[N-2-i, N-2-j] = cnt[N-2-i+1, N-2-j+1] // (i+1)
        elif j == 0:
            cnt[N-2-i, N-2-j] = cnt[N-2-i+1, N-2-j] * i // (i+1)
        else:
            cnt[N-2-i, N-2-j] = cnt[N-2-i+1, N-2-j+1]
# print(cnt)

ans = np.mod((dif * cnt), MOD)
# print(ans)
ans = ans.sum() % MOD
print(ans)

Buggy submission (verdict: Runtime Error):
import numpy as np
import math
import sys
readline = sys.stdin.buffer.readline
MOD = 10**9+7

N = int(readline())
x = np.array(readline().split(), dtype=np.int64)
# print(N, x)

# dif = np.zeros((N-1,N-1), dtype=np.int64)
dif = np.fmax(x[1:].reshape(-1, N-1) - x[:N-1].reshape(N-1, -1), 0)
# print(dif)

cnt = np.zeros((N-1,N-1), dtype=np.int64)
for i in range(N-1):
    for j in range(i+1):
        if i == 0:
            cnt[N-2-i, N-2-j] = math.factorial(N-1)
        elif j == 1:
            cnt[N-2-i, N-2-j] = cnt[N-2-i+1, N-2-j+1] // (i+1)
        elif j == 0:
            cnt[N-2-i, N-2-j] = cnt[N-2-i+1, N-2-j] * i // (i+1)
        else:
            cnt[N-2-i, N-2-j] = cnt[N-2-i+1, N-2-j+1]
# print(cnt)

ans = (dif * cnt) % MOD
# print(ans)
ans = ans.sum()
print(ans)
Problem p02839 (python)

Fixed submission:
H, W = map(int, input().split())
A = [list(map(int, input().split())) for i in range(H)]
B = [list(map(int, input().split())) for i in range(H)]
center = 80 * (H+W)
C = [[abs(A[i][j] - B[i][j]) for j in range(W)] for i in range(H)]

dp = [[0] * W for i in range(H)]
dp[0][0] = (1 << (center + C[0][0])) | (1 >> (center - C[0][0]))

for i in range(H):
    for j in range(W):
        if i:
            dp[i][j] |= dp[i-1][j] << C[i][j]
            dp[i][j] |= dp[i-1][j] >> C[i][j]
        if j:
            dp[i][j] |= dp[i][j-1] << C[i][j]
            dp[i][j] |= dp[i][j-1] >> C[i][j]

ans = center * 2
for i in range(center*2):
    if (dp[H-1][W-1] >> i) & 1:
        ans = min(ans, abs(i-center))

print(ans)

Buggy submission (verdict: Runtime Error):
import sys
# input = sys.stdin.buffer.readline
def input(): return sys.stdin.readline().rstrip()
sys.setrecursionlimit(10 ** 7)

MAX = 500   # (80+80)*80=240
MID = 250   # 250を0とする
H, W = map(int, input().split())
A = [[int(x) for x in input().split()] for _ in range(H)]
B = [[int(x) for x in input().split()] for _ in range(H)]
C = [[0] * W for _ in range(H)]
for i in range(H):
    for j in range(W):
        C[i][j] = abs(A[i][j] - B[i][j])

dp = [[[0]*500 for _ in range(W)] for _ in range(H)]
dp[0][0][MID+C[0][0]] = 1
dp[0][0][MID-C[0][0]] = 1
for i in range(H):
    for j in range(W):
        if i:
            for k in range(MAX):
                if dp[i-1][j][k] == 1:
                    dp[i][j][k+C[i][j]] = 1
                    dp[i][j][k-C[i][j]] = 1
        if j:
            for k in range(MAX):
                if dp[i][j-1][k] == 1:
                    dp[i][j][k+C[i][j]] = 1
                    dp[i][j][k-C[i][j]] = 1
ans = MAX
for i in range(MAX):
    if dp[H-1][W-1][i] == 1:
        ans = min(ans, abs(i-MID))
print(ans)
Problem p02850 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10**8)

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

ans = [0] * (N-1)

def dfs(v, c=-1, p=-1):
    global g, ans
    k = 1
    for u, ei in g[v]:
        if u == p:
            continue
        if k == c:
            k += 1
        ans[ei] = k
        k += 1
        dfs(u, ans[ei], v)

dfs(0)
mx = 0
for i in range(N):
    mx = max(mx, len(g[i]))
print(mx)
for i in range(N-1):
    print(ans[i])

Buggy submission (verdict: Runtime Error):
N = int(input())
g = [[] for _ in range(N)]
for i in range(N-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    g[a].append((b, i))
    g[b].append((a, i))

ans = [0] * (N-1)

def dfs(v, c=-1, p=-1):
    global g, ans
    k = 1
    for u, ei in g[v]:
        if u == p:
            continue
        if k == c:
            k += 1
        ans[ei] = k
        k += 1
        dfs(u, ans[ei], v)

dfs(0)
mx = 0
for i in range(N):
    mx = max(mx, len(g[i]))
print(mx)
for i in range(N-1):
    print(ans[i])

Buggy submission (verdict: Runtime Error):
N = int(input())
g = [[] for _ in range(N)]
for i in range(N-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    g[a].append((b, i))
    g[b].append((a, i))
# print(g)

ans = [0] * (N-1)

def dfs(v, c=-1, p=-1):
    global g, ans
    k = 1
    for i in range(len(g[v])):
        u = g[v][i][0]
        ei = g[v][i][1]
        if u == p:
            continue
        if k == c:
            k += 1
        ans[ei] = k
        k += 1
        dfs(u, ans[ei], v)

dfs(0)
mx = 0
for i in range(N):
    mx = max(mx, len(g[i]))
print(mx)
for i in range(N-1):
    print(ans[i])