Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(100000)
def num_steps(steps, cache={}):
    if steps in out or steps < 0 :
        return 0
    elif steps == 0:
        return 1

    if steps in cache:
        return cache[steps]
    else:
        one = num_steps(steps-1, cache)
        two = num_steps(steps-2, cache)
        ans = one + two
        cache[steps] = ans

        return ans%mod

n, m = map(int, input().split())
out = set(int(input()) for _ in range(m))
mod = 1000000007

print(num_steps(n))



Fixed submission:
import sys
sys.setrecursionlimit(200000)
def num_steps(steps, cache={}):
    if steps in out or steps < 0 :
        return 0
    elif steps == 0:
        return 1

    if steps in cache:
        return cache[steps]
    else:
        one = num_steps(steps-1, cache)
        two = num_steps(steps-2, cache)
        ans = one + two
        cache[steps] = ans

        return ans%mod

n, m = map(int, input().split())
out = set(int(input()) for _ in range(m))
mod = 1000000007

print(num_steps(n))

Buggy python submission (verdict: Runtime Error):
def num_steps(steps, cache={}):
    if steps in out or steps < 0 :
        return 0
    elif steps == 0:
        return 1

    if steps in cache:
        return cache[steps]
    else:
        one = num_steps(steps-1, cache)
        two = num_steps(steps-2, cache)
        ans = one + two
        cache[steps] = ans

        return ans%mod

n, m = map(int, input().split())
out = set(int(input()) for _ in range(m))
mod = 1000000007

print(num_steps(n))



Fixed submission:
import sys
sys.setrecursionlimit(200000)
def num_steps(steps, cache={}):
    if steps in out or steps < 0 :
        return 0
    elif steps == 0:
        return 1

    if steps in cache:
        return cache[steps]
    else:
        one = num_steps(steps-1, cache)
        two = num_steps(steps-2, cache)
        ans = one + two
        cache[steps] = ans

        return ans%mod

n, m = map(int, input().split())
out = set(int(input()) for _ in range(m))
mod = 1000000007

print(num_steps(n))
Buggy python submission (verdict: Runtime Error):
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

def lcm(a, b):
    return a // gcd (a, b) * b

n, m = map(int, input().split())
s = input()
t = input()

ans = lcm(n, m)

chk = [None]*ans

for i in range(n):
    d = ans//n
    chk[i*d] = s[i]
for i in range(m):
    d = ans//m
    target = chk[i*d]
    if target is not None and target != t[i]:
        print(-1)
        exit()
print(ans)



Fixed submission:
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

def lcm(a, b):
    return a // gcd (a, b) * b

n, m = map(int, input().split())
s = input()
t = input()

ans = lcm(n, m)

chk = {}

for i in range(n):
    d = ans//n
    chk[i*d] = s[i]
for i in range(m):
    d = ans//m
    if i*d in chk and chk[i*d] != t[i]:
        print(-1)
        exit()
print(ans)
Buggy python submission (verdict: Runtime Error):
from operator import mul
from functools import reduce

def cmb(n,r):
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under

n = int(input())
ss = [input() for _ in range(n)]

march = ['M','A','R','C','H']
d = dict(zip(march, [0]*5))

nn = 0

for s in ss:
    if s[0] in march:
        d[s[0]] += 1
        nn += 1

ans = cmb(nn, 3)
for i in d.values():
    if i >= 2:
        if i > 3:
            ans -= cmb(i, 3)
        ans -= cmb(i,2)*(nn-i)

print(int(ans))



Fixed submission:
from operator import mul
from functools import reduce

def cmb(n,r):
    if n < r: return 0
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under

n = int(input())
ss = [input() for _ in range(n)]
march = ['M','A','R','C','H']
d = dict(zip(march, [0]*5))

nn = 0

for s in ss:
    if s[0] in march:
        d[s[0]] += 1
        nn += 1
if nn < 3:
    print(0)
    exit()

ans = cmb(nn, 3)
for i in d.values():
    if i >= 2:
        if i > 3:
            ans -= cmb(i, 3)
        ans -= cmb(i,2)*(nn-i)

print(int(ans))

Buggy python submission (verdict: Runtime Error):
from scipy.special import comb
def cmb(n,r):
    return comb(n, r)

n = int(input())
ss = [input() for _ in range(n)]

march = ['M','A','R','C','H']
d = dict(zip(march, [0]*5))

nn = 0

for s in ss:
    if s[0] in march:
        d[s[0]] += 1
        nn += 1

ans = comb(nn, 3)
for i in d.values():
    if i >= 2:
        if i > 3:
            ans -= comb(i, 3)
        ans -= comb(i,2)*(nn-i)

print(int(ans))

Fixed submission:
from operator import mul
from functools import reduce

def cmb(n,r):
    if n < r: return 0
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under

n = int(input())
ss = [input() for _ in range(n)]
march = ['M','A','R','C','H']
d = dict(zip(march, [0]*5))

nn = 0

for s in ss:
    if s[0] in march:
        d[s[0]] += 1
        nn += 1
if nn < 3:
    print(0)
    exit()

ans = cmb(nn, 3)
for i in d.values():
    if i >= 2:
        if i > 3:
            ans -= cmb(i, 3)
        ans -= cmb(i,2)*(nn-i)

print(int(ans))
Buggy python submission (verdict: Runtime Error):
a, b = map(int, input().split())

def cal(a,b):
    if a < b:
        dist1 = b - a
        dist2 = abs(b + a) + 1
        return min(dist1, dist2)
    elif a > b:
        if a >= 0 and b < 0:
            return abs(a+b) + 1
        else:
            dist1 = a - b + 2
        return min(dist1, dist2)

print(cal(a,b))
    

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

def cal(a,b):
    if a < b:
        dist1 = b - a
        dist2 = abs(b + a) + 1
        return min(dist1, dist2)
    elif a > b:
        if a >= 0 and b <= 0:
            return abs(a+b) + 1

        else:
            return a - b + 2

print(cal(a,b))
Buggy python submission (verdict: Runtime Error):
string = input()
ans = 0
for i in range(len(string)-1):
    if string[i] != string[i+1]:
        ans += 1
if string[-1] != string[-2]:
    ans += 1
if ans > 0:
    print(ans-1)
else:
    print(ans)

Fixed submission:
import re
string = input()
ans = 0
for i in range(len(string)-1):
    if string[i] != string[i+1]:
        ans += 1
print(ans)
Buggy python submission (verdict: Runtime Error):
data = input()
n = [int(x) for x in data]
if n[1] * n[2] < n[0]: print("No")
else print("Yes")


Fixed submission:
data = input()
n = [int(x) for x in data.split()]
if n[1] * n[2] < n[0]: print("No")
else: print("Yes")
Buggy python submission (verdict: Runtime Error):
n = int(input())
print(n - (math.floor(n - 1 / 1000) + 1) * 1000)


Fixed submission:
import math
n = int(input())
print((math.floor((n - 1) / 1000) + 1) * 1000 - n)
Buggy python submission (verdict: Runtime Error):
n = int(input())
ac = wa = tle = re = 0
for i in range(n):
  s = input()
  if s = "AC":
    ac += 1
  elif s = "WA":
    wa += 1
  elif s = "TLE":
    tle += 1
  elif s = "RE":
    re += 1
print("AC x", ac)
print("WA x", wa)
print("TLE x ", tle)
print("RE x", re)


Fixed submission:
n = int(input())
ac = wa = tle = re = 0
for i in range(n):
  s = input()
  if s == "AC":
    ac += 1
  elif s == "WA":
    wa += 1
  elif s == "TLE":
    tle += 1
  elif s == "RE":
    re += 1
print("AC x", ac)
print("WA x", wa)
print("TLE x ", tle)
print("RE x", re)
Buggy python submission (verdict: Runtime Error):
a = input()
print(a + a ** 2 + a ** 3)

Fixed submission:
a = int(input())
print(a + a ** 2 + a ** 3)
Buggy python submission (verdict: Runtime Error):
print(int(input())*int(input()))


Fixed submission:
data = input().split()
print(int(data[0])*int(data[1]))
Buggy python submission (verdict: Runtime Error):
i = int(input())
s = input()
print(s[0:k-1] if len(s) > k else s)


Fixed submission:
k = int(input())
s = input()
print(s[0:k] + "..." if len(s) > k else s)
Buggy python submission (verdict: Runtime Error):
if input() == ABC:
  print("ARC")
else:
  print("ABC")

Fixed submission:
print("ABC" if input() == "ARC" else "ARC")

Buggy python submission (verdict: Runtime Error):
if input() == ABC:print("ARC") else:print("ABC")

Fixed submission:
print("ABC" if input() == "ARC" else "ARC")
Buggy python submission (verdict: Runtime Error):
k = int(input())
data = input.split()
a = data[0]
b = data[1]

if a - b > k:
  print("OK")
else:
  ans = False
  for i in range(a, b + 1):
    if i % k == 0:
      ans = True
  print("OK" if ans else "NG")


Fixed submission:
k = int(input())
data = input().split()
a = int(data[0])
b = int(data[1])
 
ans = False
 
if abs(a - b) > k:
  ans = True
else:
  for i in range(a, b + 1):
    if i % k == 0:
      ans = True
      break
print("OK" if ans else "NG")
Buggy python submission (verdict: Runtime Error):
data = input().split()

i = [int(n) for n in data]

print(f"{i[2]} {i[0]} {i[1]}")


Fixed submission:
data = input().split()
i = [int(n) for n in data]
res = "{} {} {}"
print(res.format(i[2], i[0], i[1]))
Buggy python submission (verdict: Runtime Error):
data = input().split()

n = int(data[0])
m = int(data[1])

pro = [int(s) for s in input().split()]

vote = 0
for i in pro:
  vote += i

pro.sort(reverse=true)

if pro[m - 1] < 1 / (4 * vote):
  print("No")
else:
  print("Yes")


Fixed submission:
data = input().split()
 
n = int(data[0])
m = int(data[1])
 
pro = [int(s) for s in input().split()]
 
vote = 0
for i in pro:
  vote += i
 
s_pro = sorted(pro, reverse=True)

if s_pro[m - 1] < vote * 1 / (4 * m):
  print("No")
else:
  print("Yes")
Buggy python submission (verdict: Runtime Error):
data = input()

if data[3] == data[4] and data[5] == data[6]:
  print("Yes")
else:
  print("No")


Fixed submission:
data = input()
 
if data[2] == data[3] and data[4] == data[5]:
  print("Yes")
else:
  print("No")
Buggy python submission (verdict: Runtime Error):
data = input().split()
 
a = int(data[0])
b = int(data[1])
 
pricea = a // 0.08
priceb = b // 0.1
 
if pricea == priceb:
  ans = pricea
elif pricea + 1 == priceb:
  ans = priceb
elif pricea == priceb + 1:
  ans = pricea
else:
  ans = -1
  
print(ans)


Fixed submission:
com = input()
if com[0] == com[1] == com[2]:
  print("No")
else:
  print("Yes")
Buggy python submission (verdict: Runtime Error):
data = input().split

a = int(data[0])
b = int(data[1])

pricea = a // 0.08
priceb = b // 0.1

if a == b:
  ans = a
elif a + 1 == b:
  ans = b
elif a == b + 1:
  ans = a
else:
  ans = -1
  
print(ans)


Fixed submission:
data = input().split()

a = int(data[0])
b = int(data[1])

n = min(int(a / 0.08), int(b / 0.1))

def lookup():    
    for i in range(n, 10000):
        tax_a = int(i * 0.08)
        tax_b = int(i * 0.1)
        if tax_a == a and tax_b == b:
            print(i)
            return
    print(-1)

lookup()
Buggy python submission (verdict: Runtime Error):
data = input().split()
if int(data[0]) > 10:
  ans = data[1]
else:
  ans = data[1] - 100 * (10 - int(data[0]))
 
print(ans)


Fixed submission:
data = input().split()

n = int(data[0])
r = int(data[1])

if n < 10:
  ans = r + 100 * (10 - n)
else:
  ans = r

print(ans)

Buggy python submission (verdict: Runtime Error):
data = input().split()
if int(data[0]) > 10:
  ans = data[1]
else:
  ans = 100 * (10 - data[0])

print(ans)


Fixed submission:
data = input().split()

n = int(data[0])
r = int(data[1])

if n < 10:
  ans = r + 100 * (10 - n)
else:
  ans = r

print(ans)
Buggy python submission (verdict: Runtime Error):
n = int(input())
data = input().split()
 
ans = "APPROVED"
 
for i in range(n):
  num = int(data[i])
  if num % 2 == 0 and (num % 3 != 0 or num % 5 != 0)
    ans = "DENIED"
    break
 
print(ans)


Fixed submission:
n = int(input())
data = input().split()
 
ans = "APPROVED"
 
for i in range(n):
  num = int(data[i])
  if num % 2 == 0:
    if num % 3 != 0 and num % 5 != 0:
      ans = "DENIED"
      break  
 
print(ans)

Buggy python submission (verdict: Runtime Error):
n = int(input())
data = input().split()

ans = "APPROVED"

for i in range(n):
  num = int(data[i])
  if !(num % 6 == 0 or num % 10 == 0):
    ans = "DENIED"
    break

print(ans)


Fixed submission:
n = int(input())
data = input().split()
 
ans = "APPROVED"
 
for i in range(n):
  num = int(data[i])
  if num % 2 == 0:
    if num % 3 != 0 and num % 5 != 0:
      ans = "DENIED"
      break  
 
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
num = input().split()
 
def check():
  for s in num:
    if(num.count(s) >= 2):
      print("NO")
      return
  print("YES")

check()

Fixed submission:
n = int(input())
num = input().split()

if len(set(num)) == n:
  print("YES")
else:
  print("NO")
Buggy python submission (verdict: Runtime Error):
import math

n = int(input())
kaisu = math.log2(n)
ans = 0

for i in range(kaisu):
  ans += 2 ** i

print(ans)


Fixed submission:
import math
 
n = int(input())
kaisu = math.log2(n) + 1
ans = 0
 
for i in range(int(kaisu)):
  ans += 2 ** i
 
print(ans)
Buggy python submission (verdict: Runtime Error):
data = input()

n = int(data[0])
m = int(data[1])

if n == m:
  print("Yes")
else:
  print("No")


Fixed submission:
data = input().split()
 
n = data[0]
m = data[1]
 
if n == m:
  print("Yes")
else:
  print("No")
Buggy python submission (verdict: Runtime Error):
data = input().split()

a = data[0]
b = data[1]

ans = min(a, b) * max(a, b)

print(ans)


Fixed submission:
data = input().split()
 
a = data[0]
b = data[1]
 
ans = min(a, b) * int(max(a, b))
 
print(ans)
Buggy python submission (verdict: Runtime Error):
k = int(input())
x = int(input())

if k * 500 >= x:
  print("Yes")
else:
  print("No")


Fixed submission:
data = input().split()
k = int(data[0])
x = int(data[1])
 
if k * 500 >= x:
  print("Yes")
else:
  print("No")

Buggy python submission (verdict: Runtime Error):
data = input().spilt()

k = int(data[0])
x = int(data[1])

if x <= 500 * k:
  print("Yes")
else:
  print("No")


Fixed submission:
data = input().split()
k = int(data[0])
x = int(data[1])
 
if k * 500 >= x:
  print("Yes")
else:
  print("No")
Buggy python submission (verdict: Runtime Error):
import math

data = input().split()
val = [int(n) for n in data]

res = val[0] * val[1] / math.gcd(val[0], val[1])

print(res)


Fixed submission:
import fractions
 
data = input().split()
val = [int(n) for n in data]
 
res = val[0] * val[1] / fractions.gcd(val[0], val[1])
 
print(int(res))
Buggy python submission (verdict: Runtime Error):
n = int(input())
s = input()

if(s[:n/2-1] == s[n/2:]):
  print("Yes")
else:
  print("No")


Fixed submission:
n = int(input())
s = input()

if n % 2 == 1:
  print("No")
elif s[:(int)(n/2)] == s[(int)(n/2):]:
  print("Yes")
else:
  print("No")
Buggy python submission (verdict: Time Limit Exceeded):
N, K, Q = map(int, input().split())

A = []
point = []
for i in range(N):
    point.append(K)

for i in range(Q):
    A.append(int(input()))


for a in A:
    for i in range(N):
        if i == a-1:
            pass
        else:
            point[i] -= 1

# result
for i in range(N):
    if point[i] > 0:
        print('Yes')
    else:
        print('No')


Fixed submission:
N, K, Q = map(int, input().split())

A = []
point = []
border = 0
for i in range(N):
    point.append(K)

for i in range(Q):
    A.append(int(input()))


for a in A:
    point[a-1] += 1
    border += 1

# result
for i in range(N):
    if point[i] > border:
        print('Yes')
    else:
        print('No')
Buggy python submission (verdict: Time Limit Exceeded):
# C - Lamps

n, k = map(int, input().split())
a = list(map(int, input().split()))
assert len(a) == n

b = [0] * n
for kk in range(k):
    l = [i - a[i] for i in range(n)]
    r = [i + a[i] for i in range(n)]
    l.sort()
    r.sort()
    ln = 0
    rn = 0
    for i in range(n):
        while ln < n and l[ln] <= i:
            ln += 1
        while rn < n and r[rn] <  i:
            rn += 1
        b[i] = ln - rn
    a, b = b, a
    if all(v >= n for v in a):
        break

print(' '.join(map(str, a)))


Fixed submission:
# C - Lamps

n, k = map(int, input().split())
a = list(map(int, input().split()))
assert len(a) == n

b = [0] * n
for kk in range(k):
    l = [i - a[i] for i in range(n)]
    r = [i + a[i] for i in range(n)]
    l.sort()
    r.sort()
    ln = 0
    rn = 0
    for i in range(n):
        while ln < n and l[ln] <= i:
            ln += 1
        while rn < n and r[rn] <  i:
            rn += 1
        b[i] = ln - rn
    a, b = b, a
    if all(v >= n for v in a):
        break

print(' '.join(map(str, a)))
Buggy python submission (verdict: Time Limit Exceeded):
# B - Multiplication 2

n = int(input())
a = list(map(int, input().split()))
limit = 10 ** 18
for i in range(1, n):
    a[i] *= a[i - 1]
    if a[i] > limit:
        a[i] = -1
print(a[-1])


Fixed submission:
# B - Multiplication 2

n = int(input())
a = list(map(int, input().split()))
limit = 10 ** 18
for i in range(1, n):
    a[i] = min(a[i] * a[i - 1], limit + 1)

print(a[-1] if a[-1] <= limit else -1)
Buggy python submission (verdict: Runtime Error):
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# D - Multiple of 2019

s = input()

count = 0
counter = Counter([0])

suffix = 0
pow = 1
for i in range(len(s) - 1, -1, -1):
    suffix = (int(s[i]) * pow + suffix) % 2019
    pow = pow * 10 % 2019
    count += counter[suffix]
    counter[suffix] += 1

print(count)


Fixed submission:
# D - Multiple of 2019

s = input()

# pow[k] = 10 ** k % 2019
pow = [1]
while len(pow) <= 1 or pow[-1] > 1:
    pow.append(pow[-1] * 10 % 2019)
n = len(pow) - 1  # 10 ** n == 1 mod 2019

# memo[i % n][m] = (int(s[:i+1]) % 2019 == mであるiの個数)
memo = [[0] * 2019 for i in range(n)]
memo[n - 1][0] = 1  # memo[-1][0] = 1

count = 0  # int(s[i:j+1]) == 0 mod 2019 である(i,j)の個数
mod = 0
for j, c in enumerate(s):
    mod = (mod * 10 + int(c)) % 2019  # mod = int(s[:j+1]) % 2019
    for i in range(1, n + 1):
        m = mod * pow[n - i] % 2019  # m == mod * 10 ** (-i) mod 2019
        count += memo[(j - i + n) % n][m]
    memo[j % n][mod] += 1

print(count)

Buggy python submission (verdict: Time Limit Exceeded):
# D - Multiple of 2019

s = input()

# pow[k] = 10 ** k % 2019
pow = [1]
while len(pow) <= 1 or pow[-1] > 1:
    pow.append(pow[-1] * 10 % 2019)
n = len(pow) - 1  # 10 ** n == 1 mod 2019

# memo[i % n][m] = (int(s[:i+1]) % 2019 == mであるiの個数)
memo = [{} for i in range(n)]
memo[n - 1][0] = 1  # memo[-1][0] = 1

count = 0  # int(s[i:j+1]) == 0 mod 2019 である(i,j)の個数
mod = 0
for j, c in enumerate(s):
    mod = (mod * 10 + int(c)) % 2019  # mod = int(s[:j+1]) % 2019
    for i in range(1, n + 1):
        m = mod * pow[n - i] % 2019  # m == mod * 10 ** (-i) mod 2019
        if m in memo[(j - i + n) % n]:
            count += memo[(j - i + n) % n][m]
    if mod not in memo[j % n]:
        memo[j % n][mod] = 0
    memo[j % n][mod] += 1

print(count)


Fixed submission:
# D - Multiple of 2019

s = input()

# pow[k] = 10 ** k % 2019
pow = [1]
while len(pow) <= 1 or pow[-1] > 1:
    pow.append(pow[-1] * 10 % 2019)
n = len(pow) - 1  # 10 ** n == 1 mod 2019

# memo[i % n][m] = (int(s[:i+1]) % 2019 == mであるiの個数)
memo = [[0] * 2019 for i in range(n)]
memo[n - 1][0] = 1  # memo[-1][0] = 1

count = 0  # int(s[i:j+1]) == 0 mod 2019 である(i,j)の個数
mod = 0
for j, c in enumerate(s):
    mod = (mod * 10 + int(c)) % 2019  # mod = int(s[:j+1]) % 2019
    for i in range(1, n + 1):
        m = mod * pow[n - i] % 2019  # m == mod * 10 ** (-i) mod 2019
        count += memo[(j - i + n) % n][m]
    memo[j % n][mod] += 1

print(count)
Buggy python submission (verdict: Time Limit Exceeded):
# E - Active Infants

n = int(input())
a = list(map(int, input().split()))
assert len(a) == n

# 添字のリスト
p = list(range(n))
p.sort(key=lambda i: a[i])

# dp[(j, i)] = 位置jから幅iの区間に小さい方からi個を配置したときの最大うれしさ
dp = {(j, 0): 0 for j in range(n + 1)}

for i in range(n):
    for j in range(n - i):
        dp[(j, i + 1)] = \
            max(dp[(j,     i)] + a[p[i]] * abs(p[i] - (j + i)),
                dp[(j + 1, i)] + a[p[i]] * abs(p[i] - j))

print(dp[(0, n)])


Fixed submission:
# E - Active Infants

n = int(input())
a = list(map(int, input().split()))
assert len(a) == n

# 添字のリスト
p = list(range(n))
p.sort(key=lambda i: a[i])

# dp[(j, i)] = 位置jから幅iの区間に小さい方からi個を配置したときの最大うれしさ
dp = {(j, 0): 0 for j in range(n + 1)}

for i in range(n):
    for j in range(n - i):
        dp[(j, i + 1)] = \
            max(dp[(j,     i)] + a[p[i]] * abs(p[i] - (j + i)),
                dp[(j + 1, i)] + a[p[i]] * abs(p[i] - j))

print(dp[(0, n)])
Buggy python submission (verdict: Time Limit Exceeded):
# E - Balanced Path

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)]

assert all(len(row) == W for row in A)
assert all(len(row) == W for row in B)

# そのマスを通過したときの赤・青の差 (の絶対値)
table = [[set() for j in range(W)] for i in range(H)]

table[0][0].add(abs(A[0][0] - B[0][0]))

for i in range(H):
    for j in range(W):
        prev  = table[i - 1][j] if i > 0 else set()
        prev |= table[i][j - 1] if j > 0 else set()
        for pdiff in prev:
            diff = A[i][j] - B[i][j]
            table[i][j].add(abs(pdiff + diff))
            table[i][j].add(abs(pdiff - diff))

m = min(diff for diff in table[H - 1][W - 1])
print(m)


Fixed submission:
# E - Balanced Path

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)]

assert all(len(row) == W for row in A)
assert all(len(row) == W for row in B)

# そのマスを通過したときの赤・青の差 (の絶対値)
table = [[set() for j in range(W)] for i in range(H)]

table[0][0].add(abs(A[0][0] - B[0][0]))

for i in range(H):
    if i >= 2:
        table[i - 2] = None
    for j in range(W):
        prev  = table[i - 1][j] if i > 0 else set()
        prev |= table[i][j - 1] if j > 0 else set()
        for pdiff in prev:
            diff = A[i][j] - B[i][j]
            table[i][j].add(abs(pdiff + diff))
            table[i][j].add(abs(pdiff - diff))

m = min(diff for diff in table[H - 1][W - 1])
print(m)
Buggy python submission (verdict: Time Limit Exceeded):
import bisect

N = int(input())
L = list(map(int, input().split()))
assert len(L) == N

L.sort()

n = 0

# Choose two edges
for i in range(N):
    for j in range(i + 1, N):
        a = L[i] + L[j]
        k = bisect.bisect_left(L, a, lo=j + 1)
        #   assert all(v < a for v in L[:k]) and all(v >= a for v in L[k:])
        # the number of candidates of the third edge
        n += k - (j + 1)

print(n)


Fixed submission:
import bisect

N = int(input())
L = list(map(int, input().split()))
assert len(L) == N

L.sort()

n = 0
# Choose two edges
for i in range(N):
    for j in range(i + 1, N):
        a = L[i] + L[j]
        k = bisect.bisect_left(L, a, lo=j + 1)
        #   assert all(v < a for v in L[:k]) and all(v >= a for v in L[k:])
        # the number of candidates of the third edge
        n += k - (j + 1)

print(n)
Buggy python submission (verdict: Time Limit Exceeded):
S = input()
modulo = 10 ** 9 + 7

remainder = {0: 1}

for s in S:
    rx = {r: 0 for r in range(13)}
    for n in (range(10) if s == '?' else [int(s)]):
        for r in remainder:
            r2 = (r * 10 + n) % 13
            rx[r2] += remainder[r]
            rx[r2]  = rx[r2] % modulo
    remainder = rx

print(remainder[5] % modulo)


Fixed submission:
S = input()
modulo = 10 ** 9 + 7

remainder = {0: 1}

for s in S:
    rx = {r: 0 for r in range(13)}
    for n in (range(10) if s == '?' else [int(s)]):
        for r in remainder:
            r2 = (r * 10 + n) % 13
            rx[r2] += remainder[r]
            rx[r2]  = rx[r2] % modulo
    remainder = rx

print(remainder[5] % modulo)