Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
n, m, p = map(int, input().split())
edges = []
forward_edges = [[] for _ in range(n)]
for _ in range(m):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    edges.append((a, b, p-c))
    forward_edges[a].append(b)

can_reach = [0]*n


def dfs(v, parent):
    can_reach[v] = 1
    for v2 in forward_edges[v]:
        if parent == v2:
            continue
        dfs(v2, v)


dfs(0, -1)

INF = 10**10
dist = [INF]*n
dist[0] = 0
for i in range(n):
    for v, v2, c in edges:
        if dist[v2] <= dist[v]+c:
            continue
        dist[v2] = dist[v]+c

negative = [0]*n
for i in range(n):
    for v, v2, c in edges:
        if dist[v2] <= dist[v]+c:
            continue
        if not can_reach[v2]:
            continue
        dist[v2] = -INF
        negative[v2] = 1

if negative[-1]:
    print(-1)
else:
    print(max(0, -dist[-1]))


Fixed submission:
import sys
input = sys.stdin.readline
n, m, p = map(int, input().split())
edges = []
forward_edges = [[] for _ in range(n)]
for _ in range(m):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    edges.append((a, b, p-c))
    forward_edges[a].append(b)


can_reach = [0]*n
stack = [0]
while stack:
    v = stack.pop()
    if can_reach[v]:
        continue
    can_reach[v] = 1
    for v2 in forward_edges[v]:
        if can_reach[v2]:
            continue
        stack.append(v2)


INF = 10**10
dist = [INF]*n
dist[0] = 0
for i in range(n):
    for v, v2, c in edges:
        if dist[v2] <= dist[v]+c:
            continue
        dist[v2] = dist[v]+c

negative = [0]*n
for i in range(n):
    for v, v2, c in edges:
        if dist[v2] <= dist[v]+c:
            continue
        if not can_reach[v2]:
            continue
        dist[v2] = -INF
        negative[v2] = 1

if negative[-1]:
    print(-1)
else:
    print(max(0, -dist[-1]))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
n, m, p = map(int, input().split())
edges = []
forward_edges = [[] for _ in range(n)]
backward_edges = [[] for _ in range(n)]
for _ in range(m):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    edges.append((a, b, p-c))
    forward_edges[a].append(b)
    backward_edges[b].append(a)

can_goal = [0]*n


def dfs(v, parent):
    can_goal[v] = 1
    for v2 in backward_edges[v]:
        if parent == v2:
            continue
        dfs(v2, v)


dfs(n-1, -1)

can_reach = [0]*n

def dfs(v, parent):
    can_reach[v] = 1
    for v2 in forward_edges[v]:
        if parent == v2:
            continue
        dfs(v2, v)

dfs(0, -1)

INF = 10**10
dist = [INF]*n
dist[0] = 0
for i in range(n):
    for v, v2, c in edges:
        if dist[v2] <= dist[v]+c:
            continue
        dist[v2] = dist[v]+c

negative = [0]*n
for i in range(n):
    for v, v2, c in edges:
        if dist[v2] <= dist[v]+c:
            continue
        dist[v2] = dist[v]+c
        negative[v2] = 1

got_infinity_coins = 0
for i in range(n):
    if negative[i]:
        if can_reach[i] and can_goal[i]:
            got_infinity_coins = 1
            break
            
if got_infinity_coins:
    print(-1)
else:
    print(max(0, -dist[-1]))


Fixed submission:
import sys
input = sys.stdin.readline
n, m, p = map(int, input().split())
edges = []
forward_edges = [[] for _ in range(n)]
for _ in range(m):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    edges.append((a, b, p-c))
    forward_edges[a].append(b)


can_reach = [0]*n
stack = [0]
while stack:
    v = stack.pop()
    if can_reach[v]:
        continue
    can_reach[v] = 1
    for v2 in forward_edges[v]:
        if can_reach[v2]:
            continue
        stack.append(v2)


INF = 10**10
dist = [INF]*n
dist[0] = 0
for i in range(n):
    for v, v2, c in edges:
        if dist[v2] <= dist[v]+c:
            continue
        dist[v2] = dist[v]+c

negative = [0]*n
for i in range(n):
    for v, v2, c in edges:
        if dist[v2] <= dist[v]+c:
            continue
        if not can_reach[v2]:
            continue
        dist[v2] = -INF
        negative[v2] = 1

if negative[-1]:
    print(-1)
else:
    print(max(0, -dist[-1]))

Buggy python submission (verdict: Time Limit Exceeded):
n, m, p = map(int, input().split())
edges = []
for _ in range(m):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    edges.append((a, b, p-c))

INF = 10**10
dist = [INF]*n
dist[0] = 0
for i in range(n):
    for v, v2, c in edges:
        if dist[v2] <= dist[v]+c:
            continue
        dist[v2] = dist[v]+c

negative = [0]*n
for i in range(10**5):
    for v, v2, c in edges:
        if dist[v2] <= dist[v]+c:
            continue
        dist[v2] = dist[v]+c
        negative[v2] = 1

if negative[-1]:
    print(-1)
else:
    print(max(0, -dist[-1]))


Fixed submission:
import sys
input = sys.stdin.readline
n, m, p = map(int, input().split())
edges = []
forward_edges = [[] for _ in range(n)]
for _ in range(m):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    edges.append((a, b, p-c))
    forward_edges[a].append(b)


can_reach = [0]*n
stack = [0]
while stack:
    v = stack.pop()
    if can_reach[v]:
        continue
    can_reach[v] = 1
    for v2 in forward_edges[v]:
        if can_reach[v2]:
            continue
        stack.append(v2)


INF = 10**10
dist = [INF]*n
dist[0] = 0
for i in range(n):
    for v, v2, c in edges:
        if dist[v2] <= dist[v]+c:
            continue
        dist[v2] = dist[v]+c

negative = [0]*n
for i in range(n):
    for v, v2, c in edges:
        if dist[v2] <= dist[v]+c:
            continue
        if not can_reach[v2]:
            continue
        dist[v2] = -INF
        negative[v2] = 1

if negative[-1]:
    print(-1)
else:
    print(max(0, -dist[-1]))

Buggy python submission (verdict: Runtime Error):
N, M, P = map(int, input().split())

class Edge:
    def __init__(self, from_, to_, point):
        self.from_, self.to_, self.point = from_, to_, point
    
edge_list = [Edge(*[int(s)for s in input().split()]) for _ in range(M)]
end_point = max(edge.to_ for edge in edge_list)
end_edge_list = [edge for edge in edge_list if edge.to_ == end_point]


def backToStart(edge, T=1, via=[], infinite=False):
    if edge.from_ == 1:
        return edge, T, via, infinite
    if edge.to_ == edge.from_:
        if edge.point - P > 0:
            ls = [edge_ for edge_ in edge_list if edge_.to_ == edge.from_ and edge_.to_ != edge_.from_]
            if len(ls) == 1:
                return backToStart(ls[0], T+1, infinite=True)
            else:
                return [backToStart(edge, T+1, infinite=True) for edge in ls]
        else:
            return "DEADEND"
    edge_list_ = [edge_ for edge_ in edge_list if edge_.to_ == edge.from_]
    via_ = via[:]
    via_.append(edge)
    if len(edge_list_) == 1:
        return backToStart(edge_list_[0], T+1, via_)
    else:
        return [backToStart(edge, T+1, via_) for edge in edge_list_]

def multiEdges(edge_list):
    return [backToStart(edge) for edge in edge_list]

origin_edge_list = multiEdges(end_edge_list)

# print(origin_edge_list)

while True:
    if 'DEADEND' in origin_edge_list:
        origin_edge_list.remove('DEADEND')
    elif [] in origin_edge_list:
        origin_edge_list.remove([])
    else:
        break

def hoge():
    point_list = []
    for route in origin_edge_list:
        if route[3]:
            return -1
        ls = route[2][:]
        ls.append(route[0])
        point_list.append(sum(e.point for e in ls) - route[1] * P)
    return point_list

ans = hoge()
if isinstance(ans, int):
    print(ans)
else:
    ans.append(0)
    print(max(ans))


Fixed submission:
import sys
input = sys.stdin.readline
n, m, p = map(int, input().split())
edges = []
forward_edges = [[] for _ in range(n)]
for _ in range(m):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    edges.append((a, b, p-c))
    forward_edges[a].append(b)


can_reach = [0]*n
stack = [0]
while stack:
    v = stack.pop()
    if can_reach[v]:
        continue
    can_reach[v] = 1
    for v2 in forward_edges[v]:
        if can_reach[v2]:
            continue
        stack.append(v2)


INF = 10**10
dist = [INF]*n
dist[0] = 0
for i in range(n):
    for v, v2, c in edges:
        if dist[v2] <= dist[v]+c:
            continue
        dist[v2] = dist[v]+c

negative = [0]*n
for i in range(n):
    for v, v2, c in edges:
        if dist[v2] <= dist[v]+c:
            continue
        if not can_reach[v2]:
            continue
        dist[v2] = -INF
        negative[v2] = 1

if negative[-1]:
    print(-1)
else:
    print(max(0, -dist[-1]))