EPS/2024/03/11-03-24/a2.py

39 lines
729 B
Python
Raw Normal View History

2024-04-15 13:45:30 +02:00
def repr(uf, i):
if uf[i] == i:
return i
uf[i] = repr(uf, uf[i])
return uf[i]
def union(uf, val, i, j):
ri = repr(uf, i)
rj = repr(uf, j)
uf[rj] = ri
val[ri] = val[rj] = min(val[ri], val[rj])
def main():
[n, k] = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
couples = [[int(i)-1 for i in input().split()] for _ in range(k)]
uf = [i for i in range(n)]
val = [a[i] for i in range(n)]
for (i, j) in couples:
ri = repr(uf, i)
rj = repr(uf, j)
if ri != rj:
union(uf, val, i, j)
mini = 0
for i in range(n):
if repr(uf, i) == i:
mini += val[i]
return mini
print(main())