Buggy python submission (verdict: Time Limit Exceeded):
N,M,P = map(int,input().split())
edge = [[]for i in range(N)]
cost = [-float('inf')]*N
cost[0] = 0
for i in range(M):
    a,b,c = map(int,input().split())
    edge[a-1].append([b-1,c-P])

next = [0]*N
next[0] = 1
for i in range(N-1):
    nenext = [0] * N
    for j in range(N):
        if next[j] == 1:
            for b,c in edge[j]:
                cost[b] = max(cost[b],cost[j]+c)
                nenext[b] = 1
    next = nenext[:]


for j in range(N):
    for b,c in edge[j]:
        if cost[b] < cost[j]+c:
            cost[b] = float('inf')


next[0] = 1
for i in range(N-1):
    nenext = [0] * N
    for j in range(N):
        if next[j] == 1:
            for b,c in edge[j]:
                cost[b] = max(cost[b],cost[j]+c)
                nenext[b] = 1
    next = nenext[:]

if cost[-1] == float('inf'):
    print(-1)
else:
    print(max(cost[-1],0))

Fixed submission:
N,M,P = map(int,input().split())
edge = []
cost = [-float('inf')]*N
cost[0] = 0
for i in range(M):
    a,b,c = map(int,input().split())
    edge.append([a-1,b-1,c-P])

for i in range(N-1):
    for a,b,c in edge:
        cost[b] = max(cost[b],cost[a]+c)


for a,b,c in edge:
    if cost[b] < cost[a]+c:
        cost[b] = float('inf')


for i in range(N-1):
    for a,b,c in edge:
        cost[b] = max(cost[b],cost[a]+c)

if cost[-1] == float('inf'):
    print(-1)
else:
    print(max(cost[-1],0))

Buggy python submission (verdict: Time Limit Exceeded):
N,M,P = map(int,input().split())
edge = [[]for i in range(N)]
cost = [-float('inf')]*N
cost[0] = 0
for i in range(M):
    a,b,c = map(int,input().split())
    edge[a-1].append([b-1,c-P])

for i in range(N-1):
    for j in range(N):
        for b,c in edge[j]:
            cost[b] = max(cost[b],cost[j]+c)


for j in range(N):
    for b,c in edge[j]:
        if cost[b] < cost[j]+c:
            cost[b] = float('inf')


for i in range(N-1):
    for j in range(N):
        for b,c in edge[j]:
            cost[b] = max(cost[b],cost[j]+c)


if cost[-1] == float('inf'):
    print(-1)
else:
    print(max(cost[-1],0))

Fixed submission:
N,M,P = map(int,input().split())
edge = []
cost = [-float('inf')]*N
cost[0] = 0
for i in range(M):
    a,b,c = map(int,input().split())
    edge.append([a-1,b-1,c-P])

for i in range(N-1):
    for a,b,c in edge:
        cost[b] = max(cost[b],cost[a]+c)


for a,b,c in edge:
    if cost[b] < cost[a]+c:
        cost[b] = float('inf')


for i in range(N-1):
    for a,b,c in edge:
        cost[b] = max(cost[b],cost[a]+c)

if cost[-1] == float('inf'):
    print(-1)
else:
    print(max(cost[-1],0))

Buggy python submission (verdict: Runtime Error):
N,M,P = map(int,input().split())
edge = [[]for i in range(N)]
cost = [-float('inf')]*N
A = [-1]*N
cost[0] = 0
for i in range(M):
    a,b,c = map(int,input().split())
    edge[a-1].append([b-1,c-P])

for i in range(N-1):
    for j in range(N):
        for b,c in edge[j]:
            if cost[b] < cost[j]+c:
                cost[b] = cost[j]+c
                A[b] = j

if A[0] != -1:
    print(-1)
    exit()
p = N-1
while p != -1 or p != N:
    A[p],p = N,A[p]
if p == -1:
    print(max(cost[-1],0))
else:
    print(-1)

Fixed submission:
N,M,P = map(int,input().split())
edge = []
cost = [-float('inf')]*N
cost[0] = 0
for i in range(M):
    a,b,c = map(int,input().split())
    edge.append([a-1,b-1,c-P])

for i in range(N-1):
    for a,b,c in edge:
        cost[b] = max(cost[b],cost[a]+c)


for a,b,c in edge:
    if cost[b] < cost[a]+c:
        cost[b] = float('inf')


for i in range(N-1):
    for a,b,c in edge:
        cost[b] = max(cost[b],cost[a]+c)

if cost[-1] == float('inf'):
    print(-1)
else:
    print(max(cost[-1],0))

Buggy python submission (verdict: Runtime Error):
N,M,P = map(int,input().split())
side = [[]for i in range(N)]
for i in range(M):
    a,b,c = map(int,input().split())
    side[a-1].append([b-1,c-P])
node = [-float('inf')]*N
node[0] = 0

def DFS(p,l):
    if node[p] == float('inf'):
        return
    elif p in l:
        node[p] = float('inf')
        for i,v in side[p]:
            DFS(i,l+[p])
    else:
        for i,v in side[p]:
            if node[i] < node[p]+v:
                node[i] = node[p]+v
                DFS(i,l+[p])
    return

DFS(0,[])
if node[-1] == float('inf'):
    print(-1)
else:
    print(max(node[-1],0))

Fixed submission:
N,M,P = map(int,input().split())
edge = []
cost = [-float('inf')]*N
cost[0] = 0
for i in range(M):
    a,b,c = map(int,input().split())
    edge.append([a-1,b-1,c-P])

for i in range(N-1):
    for a,b,c in edge:
        cost[b] = max(cost[b],cost[a]+c)


for a,b,c in edge:
    if cost[b] < cost[a]+c:
        cost[b] = float('inf')


for i in range(N-1):
    for a,b,c in edge:
        cost[b] = max(cost[b],cost[a]+c)

if cost[-1] == float('inf'):
    print(-1)
else:
    print(max(cost[-1],0))