Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
N = int(input())
 
link = [[] for _ in range(N)]
for _ in range(N - 1):
    u, v, w = map(int, input().split())
    link[u - 1].append((v - 1, w))
    link[v - 1].append((u - 1, w))
 
d = [-1] * N
d[0] = 0
q = [0]
while q:
  i = q.pop(0)
  for n, w in link[i]:
    if d[n] == -1:
      d[n] = d[i] + w
      q.append(n)
      print(q)
 
for i in d:
  print(i % 2)

Fixed submission:
N = int(input())
 
link = [[] for _ in range(N)]
for _ in range(N - 1):
    u, v, w = map(int, input().split())
    link[u - 1].append((v - 1, w))
    link[v - 1].append((u - 1, w))
 
d = [-1] * N
d[0] = 0
q = [0]
while q:
  i = q.pop(0)
  for n, w in link[i]:
    if d[n] == -1:
      d[n] = d[i] + w
      q.append(n)
 
for i in d:
  print(i % 2)
Buggy python submission (verdict: Runtime Error):
def prime(p):
    memo = [p]
    for i in range(2,(int(p**0.5)+1)):
        if p%i == 0:
            memo.append(i)
            memo.append(p//i)
    return memo

x = int(input())

memo = prime(x)

ans = 0

for i in memo:
    if x%(i-1)==x//(i-1):
        ans += i-1
print(ans)

Fixed submission:
n=int(input())

ans = 0

for i in range(1,int(n**0.5)+2):
    if n%i == 0 and n//i>=i+2:
        ans += (n//i)-1
print(ans)
Buggy python submission (verdict: Runtime Error):
n=int(input())

a= list(map(int,input().split()))

plus = []

minas = []

for value in a:
    if value < 0:
        minas.append(abs(value))
    else:
        plus.append(value)

if len(minas)%2 == 0:
    ans = sum(plus)+sum(minas)
else:
    mini = min(min(plus),min(minas))
    ans = sum(plus)+sum(minas)-mini*2

print(ans)

Fixed submission:
n=int(input())

t = list(map(int,input().split()))

s = 0
mini = 10000000000000
count = 0
for i in t:
    if i < 0:
        count += 1
    s += abs(i)
    mini = min(mini, abs(i))
if count%2 == 0:
    print(s)
else:
    print(s-mini*2)
Buggy python submission (verdict: Runtime Error):
n,k = map(int,input().split())

s=list(input())

memo = [0]

for i in range(1,n):
  if s[i] != s[i-1]:
    memo.append(i)

if len(memo) == 1:
  print(n)
else:
  if s[-1] == s[-2]:
    memo.append(n)

  ans = 0
  print(memo)

  kyoukai_math = len(memo)

  for i in range(kyoukai_math):
    if s[i] == '0':
      hasi = min(kyoukai_math-1,i+k*2)
    else:
      hasi = min(kyoukai_math-1,i+k*2+1)
    ans = max(ans,memo[hasi]-memo[i])
print(ans)

Fixed submission:
n,k = map(int,input().split())

s=input()
s = list(map(int,s))

memo = [0]

for i in range(1,n):
    if s[i-1] != s[i]:
        memo.append(i)

ans = 0
for i in range(len(memo)):
    start = memo[i]
    if (s[start] == 1):
        end = i + 2*k +1
    else:
        end = i + 2*k
    if end < len(memo):
        end = memo[end]
    else:
        end = len(s)
    ans = max(ans, end-start)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,k = map(int,input().split())

s=input()
s = list(s)
if s[0] == '0':
    count0 = 1
else:
    count0 = 0

memo = []
ans = 0
a = ''
for i in range(len(s)):
    #print(i)
    memo.append(s[i])
    ans = max(ans, len(memo))
    if i<n-1:
        if s[i] == '1' and s[i+1] == '0':
            count0 += 1
            if count0 == k+1:
                while True:
                    a = memo.pop(0)
                    if a == '0' and memo[0] == '1':
                        count0 = k
                        break
print(ans)
    

Fixed submission:
n,k = map(int,input().split())

s=input()
s = list(map(int,s))

memo = [0]

for i in range(1,n):
    if s[i-1] != s[i]:
        memo.append(i)

ans = 0
for i in range(len(memo)):
    start = memo[i]
    if (s[start] == 1):
        end = i + 2*k +1
    else:
        end = i + 2*k
    if end < len(memo):
        end = memo[end]
    else:
        end = len(s)
    ans = max(ans, end-start)
print(ans)
Buggy python submission (verdict: Runtime Error):
x,y,z,k = map(int,input().split())

a = list(map(int,input().split()))

b = list(map(int,input().split()))

c = list(map(int,input().split()))

ab = []
ans = []
for i in range(x):
    for j in range(y):
        ab.append(a[i]+b[j])
ab.sort(reverse=True)

for i in range(k):
    for j in range(z):
        ans.append(ab[i]+c[j])
ans.sort(reverse=True)

for i in range(k):
    print(ans[i])

Fixed submission:
x,y,z,k = map(int,input().split())

a = list(map(int,input().split()))

b = list(map(int,input().split()))

c = list(map(int,input().split()))

ab = []
ans = []
for i in range(x):
    for j in range(y):
        ab.append(a[i]+b[j])
ab.sort(reverse=True)

if len(ab) < k:
    for i in range(len(ab)):
        for j in range(z):
            ans.append(ab[i]+c[j])
else:
    for i in range(k):
        for j in range(z):
            ans.append(ab[i]+c[j])

ans.sort(reverse=True)

for i in range(k):
    print(ans[i])
Buggy python submission (verdict: Runtime Error):
x,y = map(int,input().split())
 
t = list(map(int,input().split()))
 
res = [float('inf')]*x
res[0] = 0
 
for i in range(x):
    for j in range(i-y, i):
        if j < 0:
            continue
        res[i] = min(res[i], res[j]+abs(t[i]-t[j]))
 
print(res[-1])

Fixed submission:
a=int(input())

t = list(map(int,input().split()))

res = [float('inf')]*a
res[0] = 0

for i in range(1, a):
    for j in range(i-2, i):
        res[i] = min([res[i], res[j]+abs(t[i]-t[j])])
        
print(res[-1])
Buggy python submission (verdict: Time Limit Exceeded):
x,y = map(int,input().split())

t = list(map(int,input().split()))

res = [float('inf')]*x
res[0] = 0

for i in range(x):
    for j in range(i-y, i):
        if j < 0:
            continue
        res[i] = min(res[i], res[j]+abs(t[i]-t[j]))

print(res[-1])

Fixed submission:
x,y = map(int,input().split())
 
t = list(map(int,input().split()))
 
res = [100000000000000000000]*x
res[0] = 0
 
for i in range(x):
    for j in range(i-y, i):
        if j < 0:
            continue
        res[i] = min(res[i], res[j]+abs(t[i]-t[j]))
 
print(res[-1])
Buggy python submission (verdict: Runtime Error):
n=int(input())

ab = []
for _ in range(n):
    a, b, c = (int(x) for x in input().split())
    ab.append([a, b, c])

dp = [[0 for i in range(3)] for j in range(n+1)]

for i in range(n):
    for j in range(3):
        for k in range(3):
            if j != k:
                dp[i+1][k] = max([dp[i+1][k], dp[i][j]+ab[i][k]])

ptint(max(dp[n]))

Fixed submission:
n=int(input())

ab = []
for _ in range(n):
    a, b, c = (int(x) for x in input().split())
    ab.append([a, b, c])

dp = [[0 for i in range(3)] for j in range(n+1)]

for i in range(n):
    for j in range(3):
        for k in range(3):
            if j != k:
                dp[i+1][k] = max([dp[i+1][k], dp[i][j]+ab[i][k]])

print(max(dp[n]))
Buggy python submission (verdict: Time Limit Exceeded):
n,w = map(int,input().split())

ab = []
for _ in range(n):
    a, b= (int(x) for x in input().split())
    ab.append([a, b])

dp = [[0 for i in range(w+1)] for j in range(n+1)]

for i in range(n):
    for j in range(w+1):
        if j < ab[i][0]:
            dp[i+1][j] = dp[i][j]
        else:
            dp[i+1][j] = max([dp[i][j], dp[i][j-ab[i][0]]+ab[i][1]])
print(dp[n][w])

Fixed submission:
n,w = map(int,input().split())

ab = []
for _ in range(n):
    a, b= (int(x) for x in input().split())
    ab.append([a, b])

dp = [[0 for i in range(w+1)] for j in range(n+1)]

for i in range(n):
    for j in range(w+1):
        if j < ab[i][0]:
            dp[i+1][j] = dp[i][j]
        else:
            dp[i+1][j] = max([dp[i][j], dp[i][j-ab[i][0]]+ab[i][1]])
print(dp[n][w])
Buggy python submission (verdict: Time Limit Exceeded):
def bell(edges, n):
    dist = [0 for i in range(n)]
 
    changed = True
 
    while changed:
        changed = False
        for edge in edges:
            if dist[edge[1]] < dist[edge[0]] + 1:
                changed = True
                dist[edge[1]] = max(dist[edge[0]] + 1, dist[edge[1]])
                
    return dist
 
 
n,m = map(int,input().split())
edges = []
for _ in range(m):
    a, b= (int(x) for x in input().split())
    edges.append([a-1, b-1])
 
res = bell(edges, n)
print(max(res))

Fixed submission:
import sys
sys.setrecursionlimit(10**5)
 
n, m = map(int, input().split())
 
edges = [[] for _ in range(n+1)]
for _ in range(m):
    x, y = map(int, input().split())
    edges[x].append(y)
 
dp = [-1 for _ in range(n+1)]
 
def longest_path(u):
    if dp[u] != -1:
        return dp[u]
    d = 0
    for v in edges[u]:
        d = max(d, 1+longest_path(v))
    dp[u] = d
    return d
 
for u in range(1, n+1):
    longest_path(u)
 
print(max(dp))
Buggy python submission (verdict: Time Limit Exceeded):
N,M = map(int,input().split())

tmp = M//N
ans = 0
for i in range(1,tmp+1):
    if M%i==0:
        ans = i
print(ans)

Fixed submission:
n,m = map(int,input().split())

a = (m//n)

for i in range(a, 0, -1):
    if m%i == 0:
        print(i)
        break
Buggy python submission (verdict: Time Limit Exceeded):
x,y = map(int,input().split())
t = list(map(int,input().split()))
ans = 1000000000000000000000000000000
for i in range(x-y+1):
    hani = t[i:i+y]
    if hani[0] <= 0 and 0 <= hani[-1]:
        ans = min(ans, abs(hani[-1]*2)+abs(hani[0]), abs(hani[-1])+abs(hani[0]*2))
    elif hani[-1] <= 0:
        ans = min(ans, abs(hani[0]))
    elif 0 <= hani[0]:
        ans = min(ans, abs(hani[-1]))
print(ans)

Fixed submission:
N, K = map(int, input().split())
X = list(map(int, input().split()))
ans = float("inf")
for left in range(0, N-K+1):
    right = left+K
    minus = 0
    plus = 0
    if X[left] < 0:
        minus = min(minus, X[left])
    if X[right-1] > 0:
        plus = max(plus, X[right-1])
    ans = min(ans, 2*abs(minus)+plus, abs(minus)+2*plus)
 
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
x,y = map(int,input().split())

t = list(map(int,input().split()))

hani = []

for i in range(x-y+1):
    hani.append(t[i:i+y])
ans = 1000000000000000000000000000000
for i in range(len(hani)):
    if hani[i][0] <= 0 and 0 <= hani[i][-1]:
        ans = min(ans, abs(hani[i][-1]*2)+abs(hani[i][0]), abs(hani[i][-1])+abs(hani[i][0]*2))
    elif hani[i][-1] <= 0:
        ans = min(ans, abs(hani[i][0]))
    elif 0 <= hani[i][0]:
        ans = min(ans, abs(hani[i][-1]))
print(ans)

Fixed submission:
N, K = map(int, input().split())
X = list(map(int, input().split()))
ans = float("inf")
for left in range(0, N-K+1):
    right = left+K
    minus = 0
    plus = 0
    if X[left] < 0:
        minus = min(minus, X[left])
    if X[right-1] > 0:
        plus = max(plus, X[right-1])
    ans = min(ans, 2*abs(minus)+plus, abs(minus)+2*plus)
 
print(ans)
Buggy python submission (verdict: Runtime Error):
D,G = map(int,input().split())
ab = []
for _ in range(D):
    p, c = (int(x) for x in input().split())
    ab.append([p,c])

ans = 10000000

pick_num = []

for i in range(D):
    pick_num.append(i)

for i in range(0,D+1):
    for v in itertools.combinations(pick_num, i):      
        tmp_point = 0
        tmp_count = 0
        for j in v:
            tmp_point += ab[j][0]*(j+1)*100+ab[j][1]
            tmp_count += ab[j][0]
        if tmp_point < G:
            nokori = G-tmp_point
            for k in range(D-1, -1,-1):
                if k not in v:
                    if nokori%(100*(k+1)) == 0:
                        if nokori//(100*(k+1)) < ab[k][0]:
                            tmp_count += nokori//(100*(k+1))
                            tmp_point += nokori
                    else:
                        if nokori//(100*(k+1))+1 < ab[k][0]:
                            tmp_count += nokori//(100*(k+1))+1
                            tmp_point += nokori
                    break
        if G <= tmp_point:
            ans = min(ans, tmp_count)
print(ans)

Fixed submission:
import itertools

D,G = map(int,input().split())
ab = []
for _ in range(D):
    p, c = (int(x) for x in input().split())
    ab.append([p,c])

ans = 10000000

pick_num = []

for i in range(D):
    pick_num.append(i)

for i in range(0,D+1):
    for v in itertools.combinations(pick_num, i):      
        tmp_point = 0
        tmp_count = 0
        for j in v:
            tmp_point += ab[j][0]*(j+1)*100+ab[j][1]
            tmp_count += ab[j][0]
        if tmp_point < G:
            nokori = G-tmp_point
            for k in range(D-1, -1,-1):
                if k not in v:
                    if nokori%(100*(k+1)) == 0:
                        if nokori//(100*(k+1)) < ab[k][0]:
                            tmp_count += nokori//(100*(k+1))
                            tmp_point += nokori
                    else:
                        if nokori//(100*(k+1))+1 < ab[k][0]:
                            tmp_count += nokori//(100*(k+1))+1
                            tmp_point += nokori
                    break
        if G <= tmp_point:
            ans = min(ans, tmp_count)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
s=input()

n=int(input())

ans = []

for i in range(len(s)+1):
    for j in range(i):
        if s[j:i] not in ans:
            ans.append(s[j:i])
ans.sort()
print(ans[n-1])

Fixed submission:
s=input()
 
n=int(input())
 
ans = []
 
for i in range(len(s)):
    for j in range(1,7):
        if s[i:i+j] not in ans and j <= len(s):
            ans.append(s[i:i+j])
ans.sort()
#print(ans)
print(ans[n-1])
Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())

p = list(map(int,input().split()))

root = [[] for i in range(n)]
for _ in range(m):
    a, b = (int(x) for x in input().split())
    root[b-1].append(a-1)
    root[a-1].append(b-1)

dp = [False]*n

ans = 0

for j in range(n):
    if dp[j]:
        continue
    check = [-1]*n
    check[j] = 0
    stack=[j]
    dp[j] = True
    while len(stack):
        v = stack.pop()
        for i in root[v]:
            if check[i] == -1:
                dp[i] = True
                check[i]=i
                stack.append(i)
    for k in range(n):
        if check[k] != -1 and check[p[k]-1] != -1:
            ans += 1

print(ans)

Fixed submission:
n,m = map(int,input().split())

p = list(map(int,input().split()))

root = [[] for i in range(n)]
for _ in range(m):
    a, b = (int(x) for x in input().split())
    root[b-1].append(a-1)
    root[a-1].append(b-1)

check = [-1]*n

ans = 0

for j in range(n):
    if check[j] != -1:
        continue
    stack=[j]
    check[j]=j
    while len(stack):
        v = stack.pop(0)
        for i in root[v]:
            if check[i] == -1:
                check[i]=j
                stack.append(i)
for k in range(n):
    if check[k] == check[p[k]-1] :
        ans += 1

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import math

def combination(n, k):
    return math.factorial(n) // math.factorial(k) // math.factorial(n - k)


n=int(input())

t = list(map(int,input().split()))

a1 = max(t)

ans = 0

memo = 0

for i in t:
    if ans <= combination(a1,i):
        ans = combination(a1,i)
        memo = i
print(a1, memo)

Fixed submission:
n=int(input())
 
t = list(map(int,input().split()))
t.sort()
a1 = max(t)
 
ans = 100000000000000
 
memo = 0

for i in t:
    if ans > abs(a1/2-i):
        ans = abs(a1/2-i)
        memo = i
print(a1, memo)
Buggy python submission (verdict: Runtime Error):
n=int(input())
r1=2
r2=1

if n == 1:
    print(r2)
else :
    for i in range(n-1):
        r3 = r1+r2
        r1 = r2
        r2 = r3
print(r3)

Fixed submission:
n=int(input())
r1=2
r2=1

if n == 1:
    print(1)
else :
    for i in range(n-1):
        r3 = r1+r2
        r1 = r2
        r2 = r3
    print(r3)
Buggy python submission (verdict: Runtime Error):
n,m = map(int,input().split())
count = 0
ab = []
for _ in range(m):
    a, b = (int(x) for x in input().split())
    ab.append([a, b])

for i in range(m):
    root = [[] for i in range(n)]
    done = [False]*n
    cd = copy.copy(ab)
    cd.pop(i)
    for j in range(m-1):
        a, b = cd[j]
        root[b-1].append(a-1)
        root[a-1].append(b-1)
        
    stack=[0]
    while len(stack):
        v = stack.pop()
        for k in root[v]:
            if done[k]: continue
            done[k] = True
            stack.append(k)
    if False in done:
        count += 1

print(count)

Fixed submission:
import copy     
n,m = map(int,input().split())
count = 0
ab = []
for _ in range(m):
    a, b = (int(x) for x in input().split())
    ab.append([a, b])

for i in range(m):
    root = [[] for i in range(n)]
    done = [False]*n
    cd = copy.copy(ab)
    cd.pop(i)
    for j in range(m-1):
        a, b = cd[j]
        root[b-1].append(a-1)
        root[a-1].append(b-1)
        
    stack=[0]
    while len(stack):
        v = stack.pop()
        for k in root[v]:
            if done[k]: continue
            done[k] = True
            stack.append(k)
    if False in done:
        count += 1

print(count)
Buggy python submission (verdict: Time Limit Exceeded):
n,t=map(int, input().split())
x=[]
y=[]
m=[]
for i in range(n):
    a,b=map(int, input().split())
    x.append(a)
    y.append(b)
    m.append([a,b])
x.sort()
y.sort()
ans=float('inf')
for i in range(n-1):
    for j in range(i,n):
        if i>=j:continue
        for k in range(n-1):
            for l in range(k,n):
                if k>=l:continue
                count=0
                for a,b in m:
                    if x[i]<=a<=x[j] and y[k]<=b<=y[l]:
                        count+=1
                if count>=t:
                    ans=min(ans,(x[j]-x[i])*(y[l]-y[k]))
print(ans)
    

Fixed submission:
n,t=map(int, input().split())
x=[]
y=[]
m=[]
for i in range(n):
    a,b=map(int, input().split())
    x.append(a)
    y.append(b)
    m.append([a,b])
x.sort()
y.sort()
ans=float('inf')
for i in range(n-1):
    for j in range(i,n):
        if i>=j:continue
        for k in range(n-1):
            for l in range(k,n):
                if k>=l:continue
                count=0
                for a,b in m:
                    if x[i]<=a<=x[j] and y[k]<=b<=y[l]:
                        count+=1
                if count>=t:
                    ans=min(ans,(x[j]-x[i])*(y[l]-y[k]))
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
def warshall_floyd(d):
    #d[i][j]: iからjへの最短距離
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d[i][j] = min(d[i][j],d[i][k] + d[k][j])
    return d

##############################
n,w,r = map(int,input().split()) #n:頂点数　w:辺の数
t = list(map(int,input().split()))

d = [[float("inf")]*n for i in range(n)] 
#d[u][v] : 辺uvのコスト(存在しないときはinf)
for i in range(w):
    x,y,z = map(int,input().split())
    d[x-1][y-1] = z
    d[y-1][x-1] = z
for i in range(n):
    d[i][i] = 0 #自身のところに行くコストは０
res = warshall_floyd(d)

print(max([res[t[0]-1][i-1] for i in t]))

Fixed submission:
import itertools
 
n,m,r_ = map(int, input().split( ))
 
r = list(map(int, input().split( )))
for i in range(r_):
  	r[i] -= 1
d = [[10**10 for _ in range(n)] for i in range(n)]
 
for i in range(m):
    ai,bi,ci = map(int, input().split( ))
    ai-=1
    bi-=1
    d[ai][bi] = d[bi][ai] = ci

for k in range(n):
    for i in range(n):
        for j in range(n):
            d[i][j] = min(d[i][k]+d[k][j],d[i][j])
ans= 100000000000000
for l in itertools.permutations(r,r_):
    tmp = 0
    for i in range(r_-1):
        tmp += d[l[i]][l[i+1]]
    ans = min(ans, tmp)
 
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())

t = list(map(int,input().split()))

t.sort()

if len(t) == 1:
    print(1)
    exit()

ans = 0

for i in range(n-1):  
    move = i+1
    memo = 1
    while t[i]+2 >= t[move]:
        memo += 1
        move += 1
        if move > n-1:
            break
    ans = max(memo, ans)

print(ans)

Fixed submission:
n=int(input())

t = list(map(int,input().split()))

memo = [0]*(2+10**5)

for a in t:
    memo[a-1] += 1
    memo[a] += 1
    memo[a+1] += 1
print(max(memo))
Buggy python submission (verdict: Runtime Error):
N = int(input())
S1 = input()
S2 = input()
mod = 10 ** 9 + 7
 
if S1[0] == S2[0] :
    ret = 3
    pre = False
    i = 1
else :
    ret = 6
    pre = True
    i = 2
 
while i < N :
    cur = (S1[i] != S2[i])
    if not pre :
        ret *= 2
    elif pre and cur :
        ret *= 3
    
    ret %= mod
    
    if cur :
        i += 2
    else :
        i += 1
    pre = cur
    
print(ret)
N = int(input())
S1 = input()
S2 = input()
mod = 10 ** 9 + 7

# False->tate True->yoko
if S1[0] == S2[0] :
    ret = 3
    pre = False
    i = 1
else :
    ret = 6
    pre = True
    i = 2

while i < N :
    cur = (S1[i] != S2[i])
    if not pre :
        ret *= 2
    elif pre and cur :
        ret *= 3
    
    ret %= mod
    
    if cur :
        i += 2
    else :
        i += 1
    pre = cur
    
print(ret)

Fixed submission:
N = int(input())
S1 = input()
S2 = input()
mod = 10 ** 9 + 7

# False->tate True->yoko
if S1[0] == S2[0] :
    ret = 3
    pre = False
    i = 1
else :
    ret = 6
    pre = True
    i = 2

while i < N :
    cur = (S1[i] != S2[i])
    if not pre :
        ret *= 2
    elif pre and cur :
        ret *= 3
    
    ret %= mod
    
    if cur :
        i += 2
    else :
        i += 1
    pre = cur
    
print(ret)
Buggy python submission (verdict: Runtime Error):
import math
from functools import reduce

def lcm_base(x, y):
    return (x * y) // math.gcd(x, y)

def lcm(*numbers):
    return reduce(lcm_base, numbers, 1)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)

#print(lcm(27, 18, 9, 3))
n=int(input())
ab = []
for _ in range(n):
    a= int(input())
    ab.append(a)
print(lcm_list(ab))

Fixed submission:
def lcm(a):
    from fractions import gcd
    x = a[0]
    for i in range(1, len(a)):
        x = (x * a[i]) // gcd(x, a[i])
    return x

#print(lcm(27, 18, 9, 3))
n=int(input())
ab = []
for _ in range(n):
    a= int(input())
    ab.append(a)
print(lcm(ab))
Buggy python submission (verdict: Time Limit Exceeded):
def bell(edges, n):
    count = 0
    dist = [-10000000000000000 for i in range(n)]
    dist[0] = 0

    changed = True

    while changed:
        count  += 1
        if count == 500000:
            print('inf')
            exit()
        changed = False
        for edge in edges:
            if dist[edge[1]] < dist[edge[0]] + edge[2]:
                changed = True
            dist[edge[1]] = max(dist[edge[0]] + edge[2], dist[edge[1]])
                
    return dist


n,m = map(int,input().split())
edges = []
for _ in range(m):
    a, b, c = (int(x) for x in input().split())
    edges.append([a-1, b-1, c])

res = bell(edges, n)

print(res[n-1])

Fixed submission:
N, M = map(int, input().split())
Edges = []
for i in range(M):
    a, b, c = map(int, input().split())
    a, b = a - 1, b - 1
    c = -c
    Edges.append([a, b, c])
 
dist = [float('inf')] * N
dist[0] = 0
 
for i in range(N):
    for fr, to, cost in Edges:
        if (dist[fr] != float('inf')) and (dist[to] > dist[fr] + cost):
            dist[to] = dist[fr] + cost
            if (i == N - 1) & (to == N - 1):
                print('inf')
                exit()
 
print(-dist[-1])

Buggy python submission (verdict: Time Limit Exceeded):
def bell(edges, n):
    count = 0
    dist = [-10000000000000000 for i in range(n)]
    dist[0] = 0

    changed = True

    while changed:
        count  += 1
        if count == 1000000:
            print('inf')
            exit()
        changed = False
        for edge in edges:
            if dist[edge[1]] < dist[edge[0]] + edge[2]:
                changed = True
            dist[edge[1]] = max(dist[edge[0]] + edge[2], dist[edge[1]])
                
    return dist


n,m = map(int,input().split())
edges = []
for _ in range(m):
    a, b, c = (int(x) for x in input().split())
    edges.append([a-1, b-1, c])

res = bell(edges, n)

print(res[n-1])

Fixed submission:
N, M = map(int, input().split())
Edges = []
for i in range(M):
    a, b, c = map(int, input().split())
    a, b = a - 1, b - 1
    c = -c
    Edges.append([a, b, c])
 
dist = [float('inf')] * N
dist[0] = 0
 
for i in range(N):
    for fr, to, cost in Edges:
        if (dist[fr] != float('inf')) and (dist[to] > dist[fr] + cost):
            dist[to] = dist[fr] + cost
            if (i == N - 1) & (to == N - 1):
                print('inf')
                exit()
 
print(-dist[-1])