# 計算量:O(|V||E|) def bellman_ford(s: int) -> list: '負のコストを持つ最短経路問題' INF = 10 ** 18 dist = [INF] * n dist[s] = 0 for i in range(n): update = False # 経路更新を行ったか for a, b, cost in g: if dist[a] > dist[b] + cost: dist[a] = dist[b] + cost update = True # 更新が行われなければそれが最短経路となる if not update: break if i == n - 1: return -1 return dist n, m = map(int, input().split()) g = [] for _ in range(m): u, v, cost = map(int, input().split()) u -= 1 v -= 1 g.append((v, u, cost)) for i in range(n): ans = bellman_ford(i) num = 0 for j in ans: if j == 10 ** 18: continue num += j print(num)