Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
A, B, K = map(int, input().split())
N = min(A, B)
list_gcd = [] 

for i in range(1, N):
  if A % i == 0 and B % i == 0:
    list_gcd.append(i)
    
if N == 1:
  Y = max(A,B)
  if Y == 1:
    list_gcd.append(1)
  else:
    for i in range(1, Y):
      if Y % i == 0:
        list_gcd.append(i)
    
print(list_gcd[K-1])

Fixed submission:
A, B, K = map(int, input().split())
N = min(A, B)
list_gcd = [] 

for i in range(1, N+1):
  if A % i == 0 and B % i == 0:
    list_gcd.append(i)
    
if N == 1:
  Y = max(A,B)
  if Y == 1:
    list_gcd.append(1)
  else:
    for i in range(1, Y):
      if Y % i == 0:
        list_gcd.append(i)

print(list_gcd[-K])

Buggy python submission (verdict: Runtime Error):
A, B, K = map(int, input().split())
N = min(A, B)
list_gcd = [] 

for i in range(1, N):
  print(A)
  if A % i == 0 and B % i == 0:
    list_gcd.append(i)
    
if N == 1:
  Y = max(A,B)
  if Y == 1:
    list_gcd.append(1)
  else:
    for i in range(1, Y):
      if Y % i == 0:
        list_gcd.append(i)
    
print(list_gcd[K-1])

Fixed submission:
A, B, K = map(int, input().split())
N = min(A, B)
list_gcd = [] 

for i in range(1, N+1):
  if A % i == 0 and B % i == 0:
    list_gcd.append(i)
    
if N == 1:
  Y = max(A,B)
  if Y == 1:
    list_gcd.append(1)
  else:
    for i in range(1, Y):
      if Y % i == 0:
        list_gcd.append(i)

print(list_gcd[-K])

Buggy python submission (verdict: Runtime Error):
A, B, K = map(int, input().split())
A, B, K = map(int, input().split())
N = min(A, B)
list_gcd = [] 

for i in range(1, N):
  print(A)
  if A % i == 0 and B % i == 0:
    list_gcd.append(i)
    
if N == 1:
  Y = max(A,B)
  if Y == 1:
    list_gcd.append(1)
  else:
    for i in range(1, Y):
      if Y % i == 0:
        list_gcd.append(i)
    
print(list_gcd[K-1])

Fixed submission:
A, B, K = map(int, input().split())
N = min(A, B)
list_gcd = [] 

for i in range(1, N+1):
  if A % i == 0 and B % i == 0:
    list_gcd.append(i)
    
if N == 1:
  Y = max(A,B)
  if Y == 1:
    list_gcd.append(1)
  else:
    for i in range(1, Y):
      if Y % i == 0:
        list_gcd.append(i)

print(list_gcd[-K])

Buggy python submission (verdict: Runtime Error):
A, B, K = map(int, input().split())
N = min(A, B)
list_gcd = [] 

for i in range(1, N):
  if A % i == 0 and B % i == 0:
    list_gcd.append(i)
    
print(list_gcd[K-1])

Fixed submission:
A, B, K = map(int, input().split())
N = min(A, B)
list_gcd = [] 

for i in range(1, N+1):
  if A % i == 0 and B % i == 0:
    list_gcd.append(i)
    
if N == 1:
  Y = max(A,B)
  if Y == 1:
    list_gcd.append(1)
  else:
    for i in range(1, Y):
      if Y % i == 0:
        list_gcd.append(i)

print(list_gcd[-K])
Buggy python submission (verdict: Time Limit Exceeded):
S = str(input())
count = 0
T = ""
while S != T:
  S = S.replace("01","a").replace("10","a")
  T = S
  count += S.count("a") * 2
  S = S.replace("a","")
  
print(count)

Fixed submission:
S = str(input())
count_0 = S.count("0")
count_1 = S.count("1")

print(min(count_0, count_1)*2)
Buggy python submission (verdict: Runtime Error):
N = int(input())
cost = [list(map(int, inpu().split())) for i in range(N)]
count = 0

for i in range(N):
  if cost[i][1] == "JPY":
    count += cost[i][0]
  else:
    count += cost[i][0] * 380000.0
    
print(count)

Fixed submission:
N = int(input())
cost = [list(input().split()) for i in range(N)]
count = 0

for i in range(N):
  if cost[i][1] == "JPY":
    count += float(cost[i][0])
  else:
    count += float(cost[i][0]) * 380000.0
    
print(count)
Buggy python submission (verdict: Time Limit Exceeded):
import bisect
A, B, Q = map(int, input().split())
INF = 10 ** 18
s = [-INF] + [int(input()) for i in range(A)] + [INF]
t = [-INF] + [int(input()) for i in range(B)] + [INF]
for q in range(Q):
  x = int(input())
  b, d = bisect.bisect_left(s, x), bisect.bisect_left(t, x)
  res = INF
  for S in [s[b - 1], s[b]]:
    for T in [t[d - 1], t[d]]:
      d1, d2 = abs(S - x) + abs(T - S), abs(T - x) + abs(S - T)
      res = min(res, d1, d2)
  print(res)

Fixed submission:
import bisect
A, B, Q = map(int, input().split())
INF = 10 ** 18
s = [-INF] + [int(input()) for i in range(A)] + [INF]
t = [-INF] + [int(input()) for i in range(B)] + [INF]
for q in range(Q):
  x = int(input())
  b, d = bisect.bisect_right(s, x), bisect.bisect_right(t, x)
  res = INF
  for S in [s[b - 1], s[b]]:
    for T in [t[d - 1], t[d]]:
      d1, d2 = abs(S - x) + abs(T - S), abs(T - x) + abs(S - T)
      res = min(res, d1, d2)
  print(res)
Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
X = sorted(list(map(int, input().split())))
subX = []

for i in range(M-1):
  subX.append(X[i+1]-X[i])
  
subX = sorted(subX)  
ans = sum(subX)

for i in range(N-1):
  ans -= subX[-i-1]

print(ans)

Fixed submission:
N, M = map(int, input().split())
X = sorted(list(map(int, input().split())))
subX = []

if N >= M:
  print(0)
else:
  for i in range(M-1):
    subX.append(X[i+1]-X[i])
  
  subX = sorted(subX)  
  ans = sum(subX)

  if N>= 2:
    for i in range(N-1):
      ans -= subX[-i-1]

  print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N, K= map(int, input().split())
H = list(map(int, input().split()))

DP = [10 ** 10] * N
DP[0] = 0
DP[1] = abs(H[1] - H[0])
for i in range(2, N):
  for j in range(1, K + 1):
    if i - j >= 0:
      DP[i] = min(DP[i], DP[i - j] + abs(H[i] - H[i - j]))

#print(DP)
print(DP[-1])


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

DP = [10 ** 10] * N
DP[0] = 0
DP[1] = abs(H[1] - H[0])
for i in range(2, N):
  for j in range(1, K + 1):
    if i - j >= 0:
      DP[i] = min(DP[i], DP[i - j] + abs(H[i] - H[i - j]))

#print(DP)
print(DP[-1])

Buggy python submission (verdict: Time Limit Exceeded):
N, K= map(int, input().split())
H = list(map(int, input().split()))

DP = [10 ** 10] * N
for i in range(N):
  if i == 0:
    DP[i] = 0
  elif i == 1:
    DP[i] = DP[i-1] + abs(H[i] - H[i - 1])
  else:
    for j in range(1, K + 1):
      if i - j >= 0:
        DP[i] = min(DP[i], DP[i - j] + abs(H[i] - H[i - j]))

#print(DP)
print(DP[-1])


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

DP = [10 ** 10] * N
DP[0] = 0
DP[1] = abs(H[1] - H[0])
for i in range(2, N):
  for j in range(1, K + 1):
    if i - j >= 0:
      DP[i] = min(DP[i], DP[i - j] + abs(H[i] - H[i - j]))

#print(DP)
print(DP[-1])
Buggy python submission (verdict: Runtime Error):
N = int(input())

def dfs(s): # 文字列 s で始まる七五三数の個数
  if int(s) > N:
    return 0
  ret = 1 if all(s.count(c) > 0 for c in '753') else 0 # s 自体が七五三数なら +1
  for c in '753':
  ret += dfs(s + c)
  return ret

print(dfs('0')) # 本当は dfs('') と書きたいが 4 行目でのエラーを防ぐため仕方なく

Fixed submission:
N = int(input())

def dfs(s): # 文字列 s で始まる七五三数の個数
  if int(s) > N:
    return 0
  ret = 1 if all(s.count(c) > 0 for c in '753') else 0 # s 自体が七五三数なら +1
  for c in '753':
    ret += dfs(s + c)
  return ret

print(dfs('0')) # 本当は dfs('') と書きたいが 4 行目でのエラーを防ぐため仕方なく
Buggy python submission (verdict: Runtime Error):
N = int(input())
T, A = map(int, input().split())
H = list(map(int, input().split()))

minT = 50

for i in range(N):
  aveT = T - H[i] * 0.006
  minT = min(minT, abs(aveT - A))
  #print(minT)
  if minT == abs(aveT - A):
    ans = i + 1
    
print(ans)

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

minT = 651
ans = 1

for i in range(N):
  aveT = T - H[i] * 0.006
  minT = min(minT, abs(aveT - A))
  #print(minT)
  if minT == abs(aveT - A):
    ans = i + 1
    
print(ans)
Buggy python submission (verdict: Runtime Error):
N = int(input())
if N == 2:
  A,B = map(int, input().split())
  print(A + B)
else:
  print("Hello World")

Fixed submission:
N = int(input())
if N == 2:
  A = int(input())
  B = int(input())
  print(A + B)
else:
  print("Hello World")
Buggy python submission (verdict: Runtime Error):
N,T = map(int, input().split())
C = [list(map(int,input().split())) for i in range(N)]
X = sorted(C, key = lambda x:x[1])

i = 0
ans = 1001
while X[i][1] <= T:
  ans = min(ans, X[i][0])
  i += 1

if ans <= 1000:
  print(ans)
else:
  print("TLE")


Fixed submission:
N,T = map(int, input().split())
C = [list(map(int,input().split())) for i in range(N)]
X = sorted(C, key = lambda x:x[1])

i = 0
ans = 1001
while X[i][1] <= T:
  ans = min(ans, X[i][0])
  if i + 1 == N:
    break
  i += 1

if ans <= 1000:
  print(ans)
else:
  print("TLE")
Buggy python submission (verdict: Time Limit Exceeded):
s = str(input())
t = str(input())

for i in range(len(s)):
  if s[i] != t[i]:
    x = s[i]
    #print(s[i],t[i])
    s = s.replace(t[i], "#").replace(s[i], t[i]).replace("#", x)
    #print(s)
  if s == t:
    print("Yes")
    quit()
    
if s == t:
  print("Yes")
else:
  print("No")

Fixed submission:
S = str(input())
T = str(input())

exchange = [-1] * 26
alpha2num = lambda c: ord(c) - ord('a')
#print(alpha2num('y')) #==> 24

for i in range(len(S)):
    now_num = alpha2num(S[i])
    #print(now_num, exchange[now_num], alpha2num(T[i]))
    if exchange[now_num] == -1:
      exchange[now_num] = alpha2num(T[i])
    elif exchange[now_num] == alpha2num(T[i]):
      #print("yeee")
      continue
    else:
      print("No")
      quit()
  #print(exchange)    
  
exchange = sorted(exchange)
for i in range(1, 26):
  if exchange[i] == exchange[i - 1]:
    if exchange[i] != -1:
      print("No")
      quit()
  
print("Yes")

Buggy python submission (verdict: Time Limit Exceeded):
s = str(input())
t = str(input())

for i in range(len(s)):
  if s[i] != t[i]:
    x = s[i]
    #print(s[i],t[i])
    s = s.replace(t[i], "#")
    s = s.replace(s[i], t[i])
    s = s.replace("#", x)
    #print(s)
  if s == t:
    print("Yes")
    quit()
    
if s == t:
  print("Yes")
else:
  print("No")

Fixed submission:
S = str(input())
T = str(input())

exchange = [-1] * 26
alpha2num = lambda c: ord(c) - ord('a')
#print(alpha2num('y')) #==> 24

for i in range(len(S)):
    now_num = alpha2num(S[i])
    #print(now_num, exchange[now_num], alpha2num(T[i]))
    if exchange[now_num] == -1:
      exchange[now_num] = alpha2num(T[i])
    elif exchange[now_num] == alpha2num(T[i]):
      #print("yeee")
      continue
    else:
      print("No")
      quit()
  #print(exchange)    
  
exchange = sorted(exchange)
for i in range(1, 26):
  if exchange[i] == exchange[i - 1]:
    if exchange[i] != -1:
      print("No")
      quit()
  
print("Yes")
Buggy python submission (verdict: Runtime Error):
N, X = map(int, input().split())
listx = list(map(int, input().split()))
subx = []

for i in range(N):
  subx.append(abs(listx[i] - X))
if N = 1:
  subx.append(subx[0])

def gcd(a,b):
  if a < b:
    a, b = b, a
  while a % b != 0:
    a, b = b, a % b
  return b

ans = gcd(subx[0], subx[1])

if N > 2:
  for i in range(2, N):
    ans = gcd(ans, subx[i])
  
print(ans)

Fixed submission:
import fractions
from functools import reduce
N, X = map(int, input().split())
listx = list(map(int, input().split()))
subx = []
for i in range(N):
  subx.append(abs(listx[i] - X))
  
def lcm_list(numbers):
    return reduce(fractions.gcd, numbers)

ans = lcm_list(subx)

print(ans)

Buggy python submission (verdict: Runtime Error):
N, X = map(int, input().split())
listx = list(map(int, input().split()))
subx = []

for i in range(N):
  subx.append(abs(listx[i] - X))

def gcd(a,b):
  if a < b:
    a, b = b, a
  while a % b != 0:
    a, b = b, a % b
  return b

ans = gcd(subx[0], subx[1])

for i in range(2, N-1):
  ans = gcd(ans, subx[i])
  
print(ans)

Fixed submission:
import fractions
from functools import reduce
N, X = map(int, input().split())
listx = list(map(int, input().split()))
subx = []
for i in range(N):
  subx.append(abs(listx[i] - X))
  
def lcm_list(numbers):
    return reduce(fractions.gcd, numbers)

ans = lcm_list(subx)

print(ans)
Buggy python submission (verdict: Runtime Error):
import collections
    
n, m = map(int, input().split())
A = list(map(int, input().split()))
ans_list = [0] * (n + 1)
for i in range(1, n + 1):
  A[i - 1] %= m
  ans_list[i] = (ans_list[i - 1] + A[i - 1]) % m
ans_list.pop(ans_list[0])
ans_list = dict(collections.Counter(ans_list))
  
#print(A)
#print(ans_list)
count = ans_list[0]
#print(count)
for v in ans_list.values():
  #print(v)
  if v != 1:
    count +=  v * (v - 1) / 2
    
print(int(count))   
    
    


Fixed submission:
import collections
    
n, m = map(int, input().split())
A = list(map(int, input().split()))
ans_list = [0] * (n + 1)
for i in range(1, n + 1):
  A[i - 1] %= m
  ans_list[i] = (ans_list[i - 1] + A[i - 1]) % m
ans_list.pop(ans_list[0])
ans_list = dict(collections.Counter(ans_list))
  
#print(A)
#print(ans_list)
count = 0
if 0 in ans_list:
  count = ans_list[0]
#print(count)
for v in ans_list.values():
  #print(v)
  if v != 1:
    count +=  v * (v - 1) / 2
    
print(int(count))

Buggy python submission (verdict: Runtime Error):
import collections

def cmb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

p = 1
N = 10 ** 5 + 1  # N は必要分だけ用意する
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用
 
for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)
    
n, m = map(int, input().split())
A = list(map(int, input().split()))
ans_list = [0] * (n + 1)
for i in range(1, n + 1):
  A[i - 1] %= m
  ans_list[i] = (ans_list[i - 1] + A[i - 1]) % m
ans_list.pop(ans_list[0])
ans_list = dict(collections.Counter(ans_list))
  
#print(A)
#print(ans_list)
count = ans_list[0]
#print(count)
for v in ans_list.values():
  #print(v)
  if v != 1:
    count +=  v * (v - 1) / 2
    
print(int(count))   
    
    


Fixed submission:
import collections
    
n, m = map(int, input().split())
A = list(map(int, input().split()))
ans_list = [0] * (n + 1)
for i in range(1, n + 1):
  A[i - 1] %= m
  ans_list[i] = (ans_list[i - 1] + A[i - 1]) % m
ans_list.pop(ans_list[0])
ans_list = dict(collections.Counter(ans_list))
  
#print(A)
#print(ans_list)
count = 0
if 0 in ans_list:
  count = ans_list[0]
#print(count)
for v in ans_list.values():
  #print(v)
  if v != 1:
    count +=  v * (v - 1) / 2
    
print(int(count))
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))

def gcd(x,y):#ユークリッドの互除法
  if y > x:
    x, y = y, x
  while y != 0:
    x, y = y, x % y
    if y == 0:
      return x
    
def lcm(x,y):
  return x * y / gcd(x,y)

m = 1
for i in range(N):
  m = lcm(m, A[i])
  
count = 0
for i in range(N):
  count += (m - 1) % A[i]
  
print(int(count))

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

count = 0
for i in range(N):
  count += A[i] - 1
  
print(count)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())

def six(n):
  count = 0
  while n > 5:
    x = n
    a = 0
    while x >= 1:
      x = x / 6
      a += 1
    a -= 1
    n -= 6 ** a
    count += 1
  count += n
  return count

def nine(n):
  count = 0
  while n > 8:
    x = n
    a = 0
    while x >= 1:
      x = x / 9
      a += 1
    a -= 1
    n -= 9 ** a
    count += 1
    #print(n)
  count += n
  return count

#print(six(41), nine(41))
ans = 10 ** 7
for i in range(N + 1):
  x = six(i) + nine(N - i)
  ans = min(ans, x)

print(ans)
  

Fixed submission:
import bisect
N = int(input())

six = []
nine = []

now = 6
while now < 100001:
  six.append(now)
  now *= 6
  
now = 9
while now < 100001:
  nine.append(now)
  now *= 9  

#print(six, nine)  
#[6, 36, 216, 1296, 7776, 46656] [9, 81, 729, 6561, 59049]

def coin(n):
  if n < 6:
    return n
  else:
    index_six = bisect.bisect_right(six, n)
    index_nine = bisect.bisect_right(nine, n)
    #print(n, index_six, index_nine, count)
    #print(n, n - six[index_six - 1], n - nine[index_nine - 1])
    if n - six[index_six - 1] >= 0:
      alpha = coin(n - six[index_six - 1]) + 1
    else:
      alpha = 10 ** 7
    if n - nine[index_nine - 1] >= 0:
      beta = coin(n - nine[index_nine - 1]) + 1
    else:
      beta = 10 ** 7
    return min(alpha, beta)
  
print(coin(N))
Buggy python submission (verdict: Time Limit Exceeded):
#X = ["a", "ba", "zxx", "abb", "ab"]
#print(sorted(X))

s = str(input())
K = int(input())
ans = []
N = len(s)

for i in range(N):
  for j in range(i + 1,N + 1):
    x = s[i:j]
    if x not in ans:
      ans.append(x)
#print(ans)
  
print(sorted(ans)[K - 1])


Fixed submission:
#X = ["a", "ba", "zxx", "abb", "ab"]
#print(sorted(X))

s = str(input())
K = int(input())
ans = []
N = len(s)

for i in range(N):
  y = min(N + 1, i + K + 1)
  for j in range(i + 1,y):
    x = s[i:j]
    if x not in ans:
      ans.append(x)
#print(ans)
  
print(sorted(ans)[K - 1])
Buggy python submission (verdict: Runtime Error):
H, W = map(int, input().split())
HW = [0] * H
for i in range(H):
  HW[i] = str(input())
#print(HW)
visited = [[0] * W for i in range(H)]
def bfs(x, y, strock):
  #now = HW[x][y]
  if visited[x][y] == 1:
    return 0
  visited[x][y] = 1
  
  if HW[x][y] == ".":
    return 0
  
  strock += 1
  queue = [[0, 1], [0, -1], [1, 0], [-1, 0]]
  tt = 0
  for i, j in queue:
    if (0 <= x + i < H) and (0 <= y + j < W):
      if HW[x + i][y + j] == "#":
        tt = 1
        bfs(x + i, y + j, strock)
  if tt == 0:
    return strock
  return 0
  
for i in range(H):
  for j in range(W):
    st = bfs(i, j, 0)
    #print(st)
    if st == 1:
      print("No")
      quit()
      
print("Yes")
  
  
      
  
  
  

Fixed submission:
H, W = map(int, input().split())
HW = [0] * H
for i in range(H):
  HW[i] = str(input())
#print(HW)
visited = [[0] * W for i in range(H)]
def bfs(x, y):
  #now = HW[x][y]
  if visited[x][y] == 1:
    return 0
  visited[x][y] = 1
  
  if HW[x][y] == ".":
    return 0
    
  queue = [[0, 1], [0, -1], [1, 0], [-1, 0]]
  tt = 0
  for i, j in queue:
    if (0 <= x + i < H) and (0 <= y + j < W):
      if HW[x + i][y + j] == "#":
        tt = 1
        #print(x, y, x + i, y + j, strock)
  if tt == 0:
    return 1
  return 0
  
for i in range(H):
  for j in range(W):
    #print(i, j)
    st = bfs(i, j)
    #print(st)
    if st == 1:
      print("No")
      quit()
      
print("Yes")

Buggy python submission (verdict: Runtime Error):
H, W = map(int, input().split())
HW = [0] * H
for i in range(H):
  HW[i] = str(input())

visited = [[0] * W for i in range(H)]
def bfs(x, y, strock):
  #now = HW[x][y]
  if visited[x][y] == 1:
    return 0
  visited[x][y] = 1
  if HW[x][y] == ".":
    return 0
  strock += 1
  queue = [[0, 1], [0, -1], [1, 0], [-1, 0]]
  tt = 0
  for i, j in queue:
    if (0 <= x + i < H) and (0 <= y + j < W):
      if HW[x + i][y + j] == "#":
        tt = 1
        bfs(x + i, y + j, strock)
  if tt == 0:
    return strock
  
for i in range(H):
  for j in range(W):
    st = bfs(i, j, 0)
    #print(st)
    if st == 1:
      print("No")
      quit()
      
print("Yes")
  
  
      
  
  
  

Fixed submission:
H, W = map(int, input().split())
HW = [0] * H
for i in range(H):
  HW[i] = str(input())
#print(HW)
visited = [[0] * W for i in range(H)]
def bfs(x, y):
  #now = HW[x][y]
  if visited[x][y] == 1:
    return 0
  visited[x][y] = 1
  
  if HW[x][y] == ".":
    return 0
    
  queue = [[0, 1], [0, -1], [1, 0], [-1, 0]]
  tt = 0
  for i, j in queue:
    if (0 <= x + i < H) and (0 <= y + j < W):
      if HW[x + i][y + j] == "#":
        tt = 1
        #print(x, y, x + i, y + j, strock)
  if tt == 0:
    return 1
  return 0
  
for i in range(H):
  for j in range(W):
    #print(i, j)
    st = bfs(i, j)
    #print(st)
    if st == 1:
      print("No")
      quit()
      
print("Yes")
Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
root = [-10 ** 10] * N
for i in range(M):
  l, r, d = map(int, input().split())
  if (root[l] < -10 ** 9 - 1) and (root[r] < -10 ** 9 - 1):
    root[l] = 0
    root[r] = d
  elif (root[l] < -10 ** 9 - 1):
    root[l] = root[r] - d
  elif (root[r] < -10 ** 9 - 1):
    root[r] = root[l] + d
  else:
    if root[l] + d != root[r]:
      print("No")
      quit()

for i in range(N):
  if root[i] < -10 ** 9 - 1:
    root[i] = 0
    
x = min(root)
y = max(root)
if y - x > 10 ** 9:
  print("No")
  quit()
    
print("Yes")      
   
  

Fixed submission:
from heapq import heappop, heappush
N, M = map(int, input().split())
graph = [[] for i in range(N)]
root = [-float("inf")] * N
for i in range(M):
  l, r, d = map(int, input().split())
  graph[l - 1].append([r - 1, d])
  graph[r - 1].append([l - 1, -d])
  
  
for i in range(N):
  if root[i] < -10 ** 10:
    root[i] = 0
    queue = [i]
    while queue:
      now = heappop(queue)
      for j, k in graph[now]:
        if root[j] < - 10 ** 10:
          root[j] = root[now] + k
          heappush(queue, j)
        elif root[j] == root[now] + k:
          continue
        else:
          print("No")
          quit()
          
print("Yes")
Buggy python submission (verdict: Time Limit Exceeded):
N, Y = map(int, input().split())
count = 0

for i in range(N+1):# 10000円札
  for j in range(N+1):# 5000円札
    if count == 0:
      X1 = Y - 10000*i - 5000*j
      X = X1//1000
      if (X1 % 1000 == 0) & (X == N-i-j) & (X>=0):
        print(i, j, X)
        count += 1
        break

if count == 0:
  print('-1 -1 -1')


Fixed submission:
N, Y = map(int, input().split())
count = 0
Y = Y//1000

for i in range(N+1):# 10000円札
  for j in range(N+1):# 5000円札
    if count == 0:
      X = Y - 10*i - 5*j
      if (X == N-i-j) & (X>=0):
        print(i, j, X)
        count += 1
    else:
      break

if count == 0:
  print('-1 -1 -1')

Buggy python submission (verdict: Time Limit Exceeded):
N, Y = map(int, input().split())
count = 0

for i in range(N+1):# 10000円札
  for j in range(N+1):# 5000円札
    if count == 0:
      X1 = Y - 10000*i - 5000*j
      X = X1//1000
      if (X1 % 1000 == 0) & (X == N-i-j) & (X>=0):
        print(i, j, X)
        count += 1
      
if count == 0:
  print('-1 -1 -1')

Fixed submission:
N, Y = map(int, input().split())
count = 0
Y = Y//1000

for i in range(N+1):# 10000円札
  for j in range(N+1):# 5000円札
    if count == 0:
      X = Y - 10*i - 5*j
      if (X == N-i-j) & (X>=0):
        print(i, j, X)
        count += 1
    else:
      break

if count == 0:
  print('-1 -1 -1')

Buggy python submission (verdict: Time Limit Exceeded):
N, Y = map(int, input().split())
count = 0

for i in range(N+1):# 1000円札
  for j in range(N+1):# 5000円札
    if count == 0:
      Z1 = Y - 1000*i - 5000*j
      Z = Z1//10000
      if (Z1 % 10000 == 0) & (Z == N-i-j) & (Z>=0):
        print(Z, j, i)
        count += 1
      
if count == 0:
  print('-1 -1 -1')

Fixed submission:
N, Y = map(int, input().split())
count = 0
Y = Y//1000

for i in range(N+1):# 10000円札
  for j in range(N+1):# 5000円札
    if count == 0:
      X = Y - 10*i - 5*j
      if (X == N-i-j) & (X>=0):
        print(i, j, X)
        count += 1
    else:
      break

if count == 0:
  print('-1 -1 -1')
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
F = [0] * N
for i in range(N):
  F[i] = list(map(int, input().split()))
  
P = [0] * N   
for i in range(N):
  P[i] = list(map(int, input().split()))

if N == 2:
  while True:
    print("yeah")
#print(F, P)
#print(4 >> 1)
ans = - 10 ** 10
#print(ans)
for i in range(1, 2 ** 10):#bit全探索 #0は空いてる時間帯がないのでカット
  open_n = [0] * N
  profit = 0
  for j in range(10):#10240通り
    if ((i >> j) & 1):#jの時間帯に空いてる
      for k in range(N):#100
        if F[k][j] == 1:
          open_n[k] += 1
  for l in range(N):
    profit += P[l][open_n[l]]
  if ans < profit:
    ans = profit
          
print(ans) 


Fixed submission:
N = int(input())
F = [0] * N
for i in range(N):
  F[i] = list(map(int, input().split()))
  
P = [0] * N   
for i in range(N):
  P[i] = list(map(int, input().split()))

#print(F, P)
#print(4 >> 1)
ans = - 10 ** 10
#print(ans)
for i in range(1, 2 ** 10):#bit全探索 #0は空いてる時間帯がないのでカット
  open_n = [0] * N
  profit = 0
  for j in range(10):#10240通り
    if ((i >> j) & 1):#jの時間帯に空いてる
      for k in range(N):#100
        if F[k][j] == 1:
          open_n[k] += 1
  for l in range(N):
    profit += P[l][open_n[l]]
  if ans < profit:
    ans = profit
          
print(ans)