import sys
input = lambda : sys.stdin.readline().rstrip()
N = int(input())
M = int(input())
# cost 내림차순 정렬
G = [tuple(map(int,input().split())) for _ in range(M)]
G.sort(key=lambda x:-x[2])
P = [i for i in range(N+1)]
def find(x):
if P[x] != x:
P[x] = find(P[x])
return P[x]
answer = 0
# 간선 코스트 작은거부터 하나씩 연결하기
while G:
a,b,cost = G.pop()
pa,pb = find(a),find(b)
if pa!=pb:
P[max(pa,pb)] = min(pa,pb)
answer += cost
print(answer)
댓글