map int input split list map int input split for in range for in range

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
n,m = map(int,input().split())
a = [list(map(int,input().split())) for i in range(m)]
c = [i for i in range(1,n+1)]
a = sorted(a, key=lambda x: x[2])
ans = 0
for i in range(m):
p = c[a[i][0] - 1]
p2 = c[a[i][1] - 1]
if p != p2:
ans += a[i][2]
for j in range(n):
if c[j] == p:
c[j] = p2
print(ans)