Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np
import fractions
from functools import reduce

rr = lambda: sys.stdin.buffer.readline().rstrip()
rs = lambda: sys.stdin.buffer.readline().split()
ri = lambda: int(sys.stdin.buffer.readline())
rm = lambda: map(int, sys.stdin.buffer.readline().split())
rl = lambda: list(map(int, sys.stdin.buffer.readline().split()))

n = ri()
li = rl()
ans = 1
for i in range(n):
  lii = li[i]
  a = fractions.gcd(lii, ans)
  ans = ans * lii // a
print(ans)


Fixed submission:
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np
import fractions
from functools import reduce

rr = lambda: sys.stdin.buffer.readline().rstrip()
rs = lambda: sys.stdin.buffer.readline().split()
ri = lambda: int(sys.stdin.buffer.readline())
rm = lambda: map(int, sys.stdin.buffer.readline().split())
rl = lambda: list(map(int, sys.stdin.buffer.readline().split()))

n = ri()
li = [ri() for _ in range(n)]
ans = 1
for i in li:
  a = fractions.gcd(i, ans)
  ans = ans * i // a
print(ans)
Buggy python submission (verdict: Runtime Error):
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np
from functools import reduce

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

s = rr()
print(s[0] + str(len(s-2)) + s[-1])









Fixed submission:
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np
from functools import reduce

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

s = rr()
print(s[0] + str(len(s) - 2) + s[-1])
Buggy python submission (verdict: Runtime Error):
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

a, b, c = rm()
if a%3 == 0 or b%3 == 0 or (a+b)%3 == 0:
  print('Possible')
else:
  print('Impossible')









Fixed submission:
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

a, b = rm()
if a%3 == 0 or b%3 == 0 or (a+b)%3 == 0:
  print('Possible')
else:
  print('Impossible')
Buggy python submission (verdict: Runtime Error):
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np
from functools import reduce

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

n, m = rm()
if n == m:
    print((math.factorial(n) * math.factorial(m) * 2) % mod)
elif abs(n-m) == 1:
    print((math.factorial(n) * math.factorial(m) % mod)
else:
    print(0)




















Fixed submission:
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np
from functools import reduce

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

n, m = rm()
if n == m:
    print((math.factorial(n) * math.factorial(m) * 2) % mod)
elif abs(n-m) == 1:
    print((math.factorial(n) * math.factorial(m)) % mod)
else:
    print(0)
Buggy python submission (verdict: Runtime Error):
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np
from functools import reduce

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

n = ri()
li = rl()
print(max(li) - min(li)









Fixed submission:
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np
from functools import reduce

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

n = ri()
li = rl()
print(max(li) - min(li))
Buggy python submission (verdict: Runtime Error):
a, b = ri(), ri()
if a == b:
  print('EQUAL')
elif a > b:
  print('GREATER')
else:
  print('LESS')

Fixed submission:
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np
from functools import reduce

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

 
a, b = ri(), ri()
if a == b:
  print('EQUAL')
elif a > b:
  print('GREATER')
else:
  print('LESS')
Buggy python submission (verdict: Runtime Error):
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np
from functools import reduce

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

o = rr()
e = rr()
s = ''
for a, b in itertools.zip_longest(o, e, fillvalu=''):
  s += a + b
print(s)








Fixed submission:
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np
from functools import reduce

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

o = rr()
e = rr()
s = ''
for a, b in itertools.zip_longest(o, e, fillvalue=''):
  s += a + b
print(s)
Buggy python submission (verdict: Runtime Error):
n = int(input())
a = 1
for i in range(1, n):
  a *= i
li = []
temp = 0
while a % 2 == 0:
  temp += 1
  a //= 2
else:
  li.append(temp+1)
for i in range(3, int(a**0.5)+1, 2):
  temp = 0
  while a % i == 0:
    temp += 1
    a //= i
  else:
    li.append(temp+1)
ans = 1
for i in li:
  ans *= i
else:
  print(ans)

Fixed submission:
import collections
n = int(input())
li = []
for k in range(2, n+1):
  for i in range(2, int(k**0.5)+1):
    while k % i == 0:
      k //= i
      li.append(i)
  if k != 1:
    li.append(k)
c = collections.Counter(li)
ans = 1
for k, v in c.items():
  ans *= (v+1)
print(ans%(10**9+7))
Buggy python submission (verdict: Time Limit Exceeded):
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np
from functools import reduce

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

k, s = rm()
cnt = 0
for x in range(k, -1, -1):
    for y in range(k, -1, -1):
        for z in range(k, -1, -1):
            if x+y+z == s:
                cnt += 1
print(cnt)















Fixed submission:
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np
from functools import reduce

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

k, s = rm()
cnt = 0
for x in range(k, -1, -1):
    for y in range(k, -1, -1):
        if 0 <= s-x-y <= k:
            cnt += 1
print(cnt)
Buggy python submission (verdict: Runtime Error):
import numpy as np
w,h,n=map(int,raw_input().split())
mp = np.zeros((w,h))
for i in range(n):
  xi,yi,ai=map(int,raw_input().split())
  if ai==1: # x < xi
    mp[:xi,:] = 1
  elif ai==2: # x > xi
    mp[xi:,:] = 1
  elif ai==3: # y < yi
    mp[:,:yi] = 1
  elif ai==4: # y > yi
    mp[:,yi:] = 1
print w*h - np.sum(mp)


Fixed submission:
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

w, h, n = rm()
xleft = 0
xright = w
ytop = h
ybottom = 0
for _ in range(n):
  x, y, a = rm()
  if a == 1:
    xleft = max(xleft, x)
  elif a == 2:
    xright = min(xright, x)
  elif a == 4:
    ytop = min(ytop, y)
  else:
    ybottom = max(ybottom, y)
if xleft >= xright or ybottom >= ytop:
  print(0)
else:
  print((xright-xleft)*(ytop-ybottom))
Buggy python submission (verdict: Runtime Error):
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np
from functools import reduce

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rf = lambda: map(float, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

print('test')

Fixed submission:
print((int(input())+int(input()))*int(input())//2)
Buggy python submission (verdict: Time Limit Exceeded):
from math import sqrt, gcd
def factoring(k): #kを因数分解し、素因数とその個数を辞書に入れて返す。
    dic = dict()
    n = int(sqrt(k))+2
    for i in range(2, n):
        count = 0
        while k%i == 0:
            count += 1
            k = k//i
        if count != 0:
            dic[i] = count
    if k != 1: #sqrt(k)までチェックしてもkが1になっていない --> kが素因数
        dic[k] = 1
    return dic

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


from collections import defaultdict


dic = defaultdict(int)

count = 0
for a in A:
    tmp = factoring(a)
    for i in tmp:
        dic[i] = max(tmp[i], dic[i])
        count += tmp[i]

tmp = 0
for i in dic:
    tmp += dic[i]

if count == tmp:
    print ('pairwise coprime')
    exit()

GCD = A[0]
for a in A:
    GCD = gcd(GCD, a)

if GCD == 1:
    print ('setwise coprime')
else:
    print ('not coprime')



Fixed submission:
from math import sqrt, gcd
def factoring(k): #kを因数分解し、素因数とその個数を辞書に入れて返す。
    dic = dict()
    n = int(sqrt(k))+2
    for i in range(2, n):
        count = 0
        while k%i == 0:
            count += 1
            k = k//i
        if count != 0:
            dic[i] = count
    if k != 1: #sqrt(k)までチェックしてもkが1になっていない --> kが素因数
        dic[k] = 1
    return dic

N = int(input())
A = list(map(int, input().split()))
A.sort()

from collections import defaultdict
from bisect import bisect_left

n = bisect_left(A, 1.5) # 1の数

dic = defaultdict(int)

if N <= 10 ** 5 + n:
    count = 0
    for a in A[max(0, n - 1):]:
        tmp = factoring(a)
        for i in tmp:
            dic[i] = max(tmp[i], dic[i])
            count += tmp[i]

    tmp = 0
    for i in dic:
        tmp += dic[i]

    if count == tmp:
        print ('pairwise coprime')
        exit()

GCD = A[0]
for a in A:
    GCD = gcd(GCD, a)
    if GCD == 1:
        break

if GCD == 1:
    print ('setwise coprime')
else:
    print ('not coprime')
Buggy python submission (verdict: Runtime Error):
A, B, C = map(int, input().split())

for _ in range(K):
    if A >= B:
        B *= 2
    elif B >= C:
        C *= 2
    else:
        C *= 2

if (B > A) and (C > B):
    print ('Yes')
else:
    print ('No')

Fixed submission:
A, B, C = map(int, input().split())
K = int(input())

for _ in range(K):
    if A >= B:
        B *= 2
    elif B >= C:
        C *= 2
    else:
        C *= 2

if (B > A) and (C > B):
    print ('Yes')
else:
    print ('No')
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))

tmp = 0
for a in A[2:]:
    tmp ^= a

a0 = A[0]
a1 = A[1]
for i in range(A[0]):
    if tmp ^ (a0 - i) ^ (a1 + i) == 0:
        print (i)
        exit()

print (-1)

Fixed submission:
from functools import lru_cache

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

tmp = 0
for a in A[2:]:
    tmp ^= a

INF = 10 ** 18

@lru_cache(None)
def F(a, b, xor):
    if (a & 1) ^ (b & 1) != (xor & 1): #一桁目が合わない時
        return INF
    if xor == 0:
        return INF if a < b else (a - b) // 2 #xor = 0にできるのは aとbが同じときだけ --> aのほうが大きい必要がある
    #1の位を放置
    x = 2 * F(a // 2, b // 2, xor // 2)
    #1の位を移動
    y = 2 * F((a - 1) // 2, (b + 1) // 2, xor // 2) + 1 # +1 は移動コスト

    x = min(x, y)
    if x >= INF:
        x = INF
    
    return x

x = F(A[0], A[1], tmp)
if x >= A[0]:
    x = -1

print (x)
Buggy python submission (verdict: Time Limit Exceeded):
class Factorial:
    def __init__(self, n, mod):
        self.f = [1]
        self.mod = mod
        for j in range(1, n + 1):
            self.f.append(self.f[-1] * j % mod)
        self.i = [pow(self.f[-1], mod - 2, mod)]
        for j in range(n, 0, -1):
            self.i.append(self.i[-1] * j % mod)
        self.i.reverse()
    def factorial(self, j):
        return self.f[j]
    def ifactorial(self, j):
        return self.i[j]
    def comb(self, n, k):
        return self.f[n] * self.i[n - k] % self.mod * self.i[k] % self.mod if n >= k else 0



K = int(input())
S = list(input())
s = len(S)

N = K + s

MOD = 10 ** 9 + 7

C = Factorial(N + 10, MOD)

lst26 = [1] * (N + 2)
lst25 = [1] * (N + 2)
for i in range(N + 1):
    lst26[i + 1] = (lst26[i] * 26) % MOD
    lst25[i + 1] = (lst25[i] * 25) % MOD

ans = 0
for x in range(K + 1):
    tmp = ((C.factorial(s - 1 + K - x) * C.ifactorial(s - 1)) % MOD) * C.ifactorial(K - x)
    tmp %= MOD
    tmp = (tmp * lst26[x]) % MOD
    tmp = (tmp * lst25[K - x]) % MOD
    ans = (ans + tmp) % MOD

print (ans)    



Fixed submission:
class Factorial:
    def __init__(self, n, mod):
        self.f = [1]
        self.mod = mod
        for j in range(1, n + 1):
            self.f.append(self.f[-1] * j % mod)
        self.i = [pow(self.f[-1], mod - 2, mod)]
        for j in range(n, 0, -1):
            self.i.append(self.i[-1] * j % mod)
        self.i.reverse()
    def factorial(self, j):
        return self.f[j]
    def ifactorial(self, j):
        return self.i[j]
    def comb(self, n, k):
        return self.f[n] * self.i[n - k] % self.mod * self.i[k] % self.mod if n >= k else 0



K = int(input())
S = list(input())
s = len(S)

N = K + s

MOD = 10 ** 9 + 7

C = Factorial(N + 10, MOD)

lst26 = [1] * (K + 2)
lst25 = [1] * (K + 2)
for i in range(K + 1):
    lst26[i + 1] = (lst26[i] * 26) % MOD
    lst25[i + 1] = (lst25[i] * 25) % MOD

ans = 0
for x in range(K + 1):
    tmp = ((C.factorial(s - 1 + K - x) * C.ifactorial(s - 1)) % MOD) * C.ifactorial(K - x)
    tmp %= MOD
    tmp = (tmp * lst26[x]) % MOD
    tmp = (tmp * lst25[K - x]) % MOD
    ans = (ans + tmp) % MOD

print (ans)
Buggy python submission (verdict: Time Limit Exceeded):
A, B, C, D = map(int, input().split())

MOD = 998244353

dp0 = [[0] * (D + 1) for _ in range(C + 1)]
dp1 = [[0] * (D + 1) for _ in range(C + 1)]


dp0[A][B] = 1

for x in range(A, C + 1):
    for y in range(B, D + 1):
        if x == A and y == B:
            continue
        dp0[x][y] = (dp0[x][y - 1] * x + dp1[x][y - 1] * 1) % MOD
        dp1[x][y] = (dp0[x - 1][y] * y + dp1[x - 1][y] * y) % MOD

# print (dp0)
# print (dp1)        

ans = (dp0[C][D] + dp1[C][D]) % MOD
print (ans)

Fixed submission:
A, B, C, D = map(int, input().split())

MOD = 998244353

dp0 = [[0] * (D + 1) for _ in range(C + 1)]
dp1 = [[0] * (D + 1) for _ in range(C + 1)]


dp0[A][B] = 1

for x in range(A, C + 1):
    for y in range(B, D + 1):
        if x == A and y == B:
            continue
        dp0[x][y] = (dp0[x][y - 1] * x + dp1[x][y - 1] * 1) % MOD
        dp1[x][y] = (dp0[x - 1][y] * y + dp1[x - 1][y] * y) % MOD

# print (dp0)
# print (dp1)        

ans = (dp0[C][D] + dp1[C][D]) % MOD
print (ans)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))
A.sort()
# print (A)
ans = 0
for i in range(N - 1, -1, -1):
    a = A[i]
    flag = True
    for j in range(i):
        if a % A[j] == 0:
            flag = False
            break
    if i + 1 < N and A[i] == A[i + 1]:
        flag = False

    if flag:
        ans += 1

print (ans)

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
A.sort()

ans = 0

check = [True] * (10 ** 6 + 1)
for i in range(N):
    a = A[i]
    if not check[a]:
        continue
    if (i >= 1 and A[i] != A[i - 1]) or i == 0:
        tmp = a
        while tmp <= 10 ** 6:
            check[tmp] = False
            tmp += a
    else:
        continue
    if i + 1 < N and A[i] == A[i + 1]:
        continue
    # print (a)
    ans += 1

print (ans)
Buggy python submission (verdict: Runtime Error):
N, K = map(int, input().split())
A = list(map(int, input().split()))

def check(lst):
    # print (set(lst))
    if (N == lst[0]) and (len(set(lst[:-1])) == 1):
        return True
    else:
        return False

def move(lst):
    tmp = [0] * (N + 1)
    for i in range(N):
        a = lst[i]
        left = max(0, i - a)
        right = min(N, i + a + 1)
        tmp[left] += 1
        tmp[right] -= 1
    for i in range(N):
        tmp[i + 1] += tmp[i]
    return tmp

for i in range(K):
    A = move(A)
    if check(A):
        # print (i)
        break

print (*A[:-1])


Fixed submission:
S = str(input())

print (S[:3])
Buggy python submission (verdict: Runtime Error):
N, K = map(int, input().split())
A = list(map(int, input().split()))

def check(lst):
    # print (set(lst))
    if (N == lst[0]) and (len(set(lst[:-1])) == 1):
        return True
    else:
        return False

def move(lst):
    tmp = [0] * (N + 1)
    for i in range(N):
        a = lst[i]
        left = max(0, i - a)
        right = min(N, i + a + 1)
        tmp[left] += 1
        tmp[right] -= 1
    for i in range(N):
        tmp[i + 1] += tmp[i]
    return tmp

for i in range(K):
    A = move(A)
    if check(A):
        # print (i)
        break

print (*A[:-1])


Fixed submission:
A, B = map(str, input().split())

a = int(A)
N = len(B)
b = 100 * int(B[:N - 3]) + int(B[N - 2:])

# print (a, b)

ans = a * b // 100

print (ans)
Buggy python submission (verdict: Runtime Error):
from bisect import bisect_left

N = int(input())

AB = [tuple(map(int, input().split())) for _ in range(N)]
ALL = []
ALL_append = ALL.append
for a, b in AB:
    ALL_append(a)
    ALL_append(b)

ALL.sort()
dic = dict()
for i, x in enumerate(ALL):
    dic[x] = i

lst1 = [0] * (2 * N + 1)
lst2 = [0] * (2 * N + 1)

for a, b in AB:
    lst1[dic[a]] += 1
    lst2[dic[b]] -= 1

for i in range(2 * N):
    lst1[i + 1] += lst1[i]
    lst2[2 * N - 1 - i] += lst2[2 * N - i]

# print (lst1)
# print (lst2)

if N % 2 == 1: #奇数 -->一意に決まる時
    left = bisect_left(lst1, N // 2 + 0.5)
    right = bisect_left(lst2, -((N) // 2) - 0.5) - 1
    # print (left, right)
    upper = ALL[right]
    lower = ALL[left]
    print (upper - lower + 1)
else:
    left = bisect_left(lst1, N // 2 - 0.5)
    right = bisect_left(lst2, -(N // 2) - 0.5)
    # print (left, right)
    upper = ALL[right]
    lower = ALL[left]
    print (upper - lower + 1)
    




Fixed submission:
N = int(input())

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

A.sort()
B.sort()

if N % 2 == 1:
    ans = B[N // 2] - A[N // 2] + 1
    print (ans)
else:
    a = (A[N // 2 - 1] + A[N // 2])
    b = (B[N // 2 - 1] + B[N // 2])
    print (b - a + 1)
Buggy python submission (verdict: Runtime Error):
N, M, Q = map(int, input().split())
ABCD = [tuple(map(int, input().split())) for _ in range(Q)]

stack = [i for i in range(1, M + 1)]

for i in range(N - 1):
    tmp = []
    while stack:
        now = stack.pop()
        start = now % 10
        for i in range(start, M + 1):
            tmp.append(now * 10 + i)
    stack = tmp

ans = 0
for tmp in stack:
    point = 0
    tmp1 = str(tmp)
    for a, b, c, d in ABCD:
        if int(tmp1[b - 1]) - int(tmp1[a - 1]) == c:
            point += d
    ans = max(ans, point)

print (ans)
# print (stack)

Fixed submission:
K = int(input())
A, B = map(int, input().split())

for i in range(A, B + 1):
    if i % K == 0:
        print ('OK')
        exit()

print ('NG')
Buggy python submission (verdict: Time Limit Exceeded):
N, M, Q = map(int, input().split())
ABCD = [tuple(map(int, input().split())) for _ in range(Q)]

stack = [i for i in range(1, M + 1)]

for i in range(N - 1):
    tmp = []
    while stack:
        now = stack.pop()
        start = now % 10
        for i in range(start, M + 1):
            tmp.append(now * 10 + i)
    stack = tmp

ans = 0
for tmp in stack:
    point = 0
    tmp1 = str(tmp)
    for a, b, c, d in ABCD:
        if int(tmp1[b - 1]) - int(tmp1[a - 1]) == c:
            point += d
    ans = max(ans, point)

print (ans)
# print (stack)

Fixed submission:
import sys
input = sys.stdin.readline

from itertools import combinations_with_replacement

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

ABCD = [tuple(map(int, input().split())) for _ in range(Q)]

lst = [i for i in range(M)]

ans = 0
for tmp in combinations_with_replacement(lst, N):
    score = 0
    for a, b, c, d in ABCD:
        if tmp[b - 1] - tmp[a - 1] == c:
            score += d
    ans = max(ans, score)

print (ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, M, Q = map(int, input().split())
ABCD = [tuple(map(int, input().split())) for _ in range(Q)]

stack = [i for i in range(1, M + 1)]

for i in range(N - 1):
    tmp = []
    while stack:
        now = stack.pop()
        start = now % 10
        for i in range(start, M + 1):
            tmp.append(now * 10 + i)
    stack = tmp

ans = 0
for tmp in stack:
    point = 0
    tmp1 = str(tmp)
    for a, b, c, d in ABCD:
        if int(tmp1[b - 1]) - int(tmp1[a - 1]) == c:
            point += d
    ans = max(ans, point)

print (ans)
# print (stack)

Fixed submission:
import sys
input = sys.stdin.readline

from itertools import combinations_with_replacement

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

ABCD = [tuple(map(int, input().split())) for _ in range(Q)]

lst = [i for i in range(M)]

ans = 0
for tmp in combinations_with_replacement(lst, N):
    score = 0
    for a, b, c, d in ABCD:
        if tmp[b - 1] - tmp[a - 1] == c:
            score += d
    ans = max(ans, score)

print (ans)