Vova promised himself that he would never play computer games... But recently Firestorm — a well-known game developing company — published their newest game, World of Farcraft, and it became really popular. Of course, Vova started playing it.

Now he tries to solve a quest. The task is to come to a settlement named Overcity and spread a rumor in it.

Vova knows that there are n characters in Overcity. Some characters are friends to each other, and they share information they got. Also Vova knows that he can bribe each character so he or she starts spreading the rumor; i-th character wants c_{i} gold in exchange for spreading the rumor. When a character hears the rumor, he tells it to all his friends, and they start spreading the rumor to their friends (for free), and so on.

The quest is finished when all n characters know the rumor. What is the minimum amount of gold Vova needs to spend in order to finish the quest?

Take a look at the notes if you think you haven't understood the problem completely.


-----Input-----

The first line contains two integer numbers n and m (1 ≤ n ≤ 10^5, 0 ≤ m ≤ 10^5) — the number of characters in Overcity and the number of pairs of friends.

The second line contains n integer numbers c_{i} (0 ≤ c_{i} ≤ 10^9) — the amount of gold i-th character asks to start spreading the rumor.

Then m lines follow, each containing a pair of numbers (x_{i}, y_{i}) which represent that characters x_{i} and y_{i} are friends (1 ≤ x_{i}, y_{i} ≤ n, x_{i} ≠ y_{i}). It is guaranteed that each pair is listed at most once.


-----Output-----

Print one number — the minimum amount of gold Vova has to spend in order to finish the quest.


-----Examples-----
Input
5 2
2 5 3 4 8
1 4
4 5

Output
10

Input
10 0
1 2 3 4 5 6 7 8 9 10

Output
55

Input
10 5
1 6 2 7 3 8 4 9 5 10
1 2
3 4
5 6
7 8
9 10

Output
15



-----Note-----

In the first example the best decision is to bribe the first character (he will spread the rumor to fourth character, and the fourth one will spread it to fifth). Also Vova has to bribe the second and the third characters, so they know the rumor.

In the second example Vova has to bribe everyone.

In the third example the optimal decision is to bribe the first, the third, the fifth, the seventh and the ninth characters.

Source: codeforces | Difficulty: EASY
Tags: Graph algorithms

Graph traversal

Greedy algorithms


Solutions:
def dfs(index, arr, parent, visited):
	min_ele = arr[index]
	visited[index] = True
	q = parent[index]
	i = 0
	while i < len(q):
		if not visited[q[i]]:
			visited[q[i]] = True
			min_ele = min(min_ele, arr[q[i]])
			q.extend(parent[q[i]])
		i += 1
	return min_ele

def main():
	(N, K) = map(int, input().split())
	parent = [[] for _ in range(N + 4)]
	visited = [False for _ in range(N + 4)]
	arr = [-1]
	arr.extend(list(map(int, input().split())))
	for _ in range(K):
		(a, b) = map(int, input().split())
		parent[a].append(b)
		parent[b].append(a)
	add = 0
	for i in range(1, len(arr)):
		if not visited[i]:
			add += dfs(i, arr, parent, visited)
	print(add)
main()


(n, q) = [int(i) for i in input().split()]
l = [int(i) for i in input().split()]
frie = [i for i in range(n)]

def find(a):
	while frie[a] != a:
		frie[a] = frie[frie[a]]
		a = frie[a]
	return a
for i in range(q):
	s = [int(i) for i in input().split()]
	(x, y) = (s[0] - 1, s[1] - 1)
	(x, y) = (find(x), find(y))
	if l[x] > l[y]:
		(x, y) = (y, x)
	frie[y] = x
visited = [False for i in range(n)]
for i in range(n - 1, -1, -1):
	if frie[i] != i:
		j = i
		mi = l[j]
		visited[j] = True
		while frie[j] != j and (not visited[j]):
			frie[j] = frie[frie[j]]
			mi = min(mi, l[j])
			j = frie[j]
			visited[j] = True
		l[j] = min(mi, l[j])
print(sum((l[frie[j]] for j in range(n) if frie[j] == j)))


def main():
	(n, m) = map(int, input().split())
	costs = list(map(int, input().split()))
	ways = [[] for _ in range(n)]
	for _ in range(m):
		(a, b) = map(int, input().split())
		ways[a - 1].append(b - 1)
		ways[b - 1].append(a - 1)
	s = set()
	ans = 0
	ad = s.add
	for (i, cost) in enumerate(costs):
		if i in s:
			continue
		else:
			Q = [i]
			p = Q.pop
			ad(i)
			m = cost
			while Q:
				new = p()
				for j in ways[new]:
					if j not in s:
						ad(j)
						Q.append(j)
						m = min(m, costs[j])
			ans += m
	print(ans)
main()


def union(a, b):
	a = find_root(a)
	b = find_root(b)
	if a == b:
		return None
	if rank[a] >= rank[b]:
		minn[a] = min(minn[a], minn[b])
		parent[b] = a
		rank[a] += rank[b]
	else:
		minn[b] = min(minn[a], minn[b])
		parent[a] = b
		rank[b] += rank[a]

def find_root(curr):
	if parent[curr] != curr:
		parent[curr] = find_root(parent[curr])
	return parent[curr]

def solve():
	global parent, rank, arr, minn
	(n, m) = [int(x) for x in input().split()]
	arr = [int(x) for x in input().split()]
	parent = [i for i in range(n)]
	rank = [1] * n
	minn = arr[:]
	for i in range(m):
		(a, b) = [int(x) for x in input().split()]
		a -= 1
		b -= 1
		union(a, b)
	done = set()
	ans = 0
	for i in range(n):
		curr = find_root(i)
		if curr not in done:
			done.add(curr)
			ans += minn[curr]
	return ans
print(solve())


import sys
(n, m) = map(int, input().split())
cost = [int(x) for x in input().split()]
adj = [[] for _ in range(n)]
for i in range(m):
	(u, v) = map(int, input().split())
	adj[u - 1].append(v - 1)
	adj[v - 1].append(u - 1)
visited = [0 for x in range(n)]
minCost = -1 - sys.maxsize

def dfs(v):
	l = [v]
	minCost = cost[v]
	while l:
		top = l.pop()
		visited[top] = 1
		minCost = min(minCost, cost[top])
		for u in adj[top]:
			if visited[u] == 0:
				l.append(u)
	return minCost
res = 0
for vertex in range(n):
	if visited[vertex] == 0:
		res += dfs(vertex)
print(res)


import math
import collections
import bisect
import heapq
import time
import random
import itertools
import sys
(N, M) = map(int, input().split())
cost = [0] + [int(x) for x in input().split()]
G = collections.defaultdict(list)
for i in range(M):
	(a, b) = map(int, input().split())
	G[a].append(b)
	G[b].append(a)
group = [0] * (N + 1)
gi = 1

def makeGroup(u, gi):
	q = [u]
	while q:
		u = q.pop()
		group[u] = gi
		for v in G[u]:
			if group[v] == 0:
				q.append(v)
for i in range(1, N + 1):
	if not group[i]:
		gi += 1
		makeGroup(i, gi)
for i in range(1, N + 1):
	if group[i] == 0:
		gi += 1
		group[i] = gi
minCost = {}
for i in range(1, N + 1):
	g = group[i]
	c = cost[i]
	if g not in minCost:
		minCost[g] = c
	else:
		minCost[g] = min(minCost[g], c)
print(sum(minCost.values() or [0]))


import collections

def bfs(visited, sv, arr):
	q = collections.deque([sv])
	visited[sv] = True
	A = []
	while q:
		front = q.popleft()
		A.append(arr[front])
		for i in graph[front]:
			if not visited[i]:
				q.append(i)
				visited[i] = True
	if A:
		return min(A)
	else:
		return 0

def solve(graph, arr, n):
	visited = [False for _ in range(n)]
	ans = 0
	for i in range(n):
		if not visited[i]:
			ans += bfs(visited, i, arr)
	return ans
(n, m) = map(int, input().split())
arr = list(map(int, input().split()))
if m == 0:
	print(sum(arr))
else:
	graph = [[] for _ in range(n)]
	for i in range(m):
		(a, b) = map(int, input().split())
		a -= 1
		b -= 1
		graph[a].append(b)
		graph[b].append(a)
	print(solve(graph, arr, n))


R = lambda : map(int, input().split())
(n, m) = R()
p = [i for i in range(n + 1)]

def find(i):
	while p[i] != i:
		p[i] = p[p[i]]
		i = p[i]
	return i
c = [0] + list(R())
for _ in range(m):
	(x, y) = map(find, R())
	if c[x] < c[y]:
		(x, y) = (y, x)
	p[x] = y
print(sum((c[i] for i in range(n + 1) if p[i] == i)))


import math
import queue
(n, m) = map(int, input().split())
c = [int(cost) for cost in input().split()]
e = []
check = []
for i in range(0, n):
	e.append([])
	check.append(0)
for i in range(0, m):
	[x, y] = list(map(int, input().split()))
	x = x - 1
	y = y - 1
	e[x].append(y)
	e[y].append(x)
checked = 0
answer = 0
cursor = 0
q = queue.Queue()
minimum = 0
while checked < n:
	for i in range(cursor, n):
		if check[i] == 0:
			break
	cursor = i + 1
	minimum = c[i]
	q.put(i)
	checked = checked + 1
	check[i] = 1
	while not q.empty():
		current = q.get()
		for i in range(0, len(e[current])):
			if check[e[current][i]] == 0:
				check[e[current][i]] = 1
				q.put(e[current][i])
				checked = checked + 1
				minimum = min(minimum, c[e[current][i]])
	answer += minimum
print(answer)


from sys import stdin, stdout
import traceback
read = stdin.readline
write = stdout.write
writeln = lambda x: write(str(x) + '\n')

def reads(typ=int):
	return list(map(typ, read().split()))
from typing import List, Dict
from collections import defaultdict
from typing import List, Dict
from collections import defaultdict
from typing import List, Dict
from collections import defaultdict
import sys

class UnionFind:

	def __init__(self, n: int):
		assert n > 0
		self._grp = list(range(n))

	def group(self, x: int) -> int:
		return self._group(x)

	def _group(self, x):
		passed = []
		while self._grp[x] != x:
			passed.append(x)
			x = self._grp[x]
		for p in passed:
			self._grp[p] = x
		return x

	def groups_list(self) -> List[int]:
		return [self.group(i) for (i, _) in enumerate(self._grp)]

	def groups_dict(self) -> Dict[int, List[int]]:
		groupd = defaultdict(list)
		for (i, g) in enumerate(self.groups_list()):
			groupd[g].append(i)
		return groupd

	def merge(self, x: int, y: int):
		x = self.group(x)
		y = self.group(y)
		self._grp[x] = self._grp[y] = min(self._grp[x], self._grp[y])

class Sol:

	@classmethod
	def input(cls):
		s = cls()
		(s.n, s.m) = reads()
		s.cs = reads()
		return s

	def get_pair_info(self):
		for _ in range(self.m):
			yield [x - 1 for x in reads()]

	def _construct_group(self) -> dict:
		uf = UnionFind(self.n)
		for (x, y) in self.get_pair_info():
			uf.merge(x, y)
		return uf.groups_dict()

	def solve(self):
		groupd = self._construct_group()
		ret = 0
		for ns in groupd.values():
			ret += min([self.cs[n] for n in ns])
		return ret
try:
	writeln(Sol.input().solve())
except Exception as e:
	print('Got exception:', repr(e))
	print(traceback.format_exc())


fast = lambda : stdin.readline().strip()
zzz = lambda : [int(i) for i in fast().split()]
(z, zz) = (input, lambda : list(map(int, z().split())))
(szz, graph, mod, szzz) = (lambda : sorted(zz()), {}, 10 ** 9 + 7, lambda : sorted(zzz()))
from re import *
from sys import *
from math import *
from heapq import *
from queue import *
from bisect import *
from string import *
from itertools import *
from collections import *
from math import factorial as f
from bisect import bisect as bs
from bisect import bisect_left as bsl
from collections import Counter as cc
from itertools import accumulate as ac

def lcd(xnum1, xnum2):
	return xnum1 * xnum2 // gcd(xnum1, xnum2)

def prime(x):
	p = ceil(x ** 0.5) + 1
	for i in range(2, p):
		if x % i == 0 and x != 2 or x == 0:
			return 0
	return 1

def dfs(u, visit, graph):
	visit[u] = 1
	for i in graph[u]:
		if not visit[i]:
			dfs(i, visit, graph)

def output(answer):
	stdout.write(str(answer))
(n, m) = zzz()
arr = zzz()
graph = []
for i in range(n):
	graph.append(deque())
for i in range(m):
	(x, y) = zzz()
	graph[x - 1].append(y - 1)
	graph[y - 1].append(x - 1)
vis = [0] * n
r = 0
for i in range(n):
	inf = float('inf')
	if not vis[i]:
		vis[i] = 1
		if inf > arr[i]:
			inf = arr[i]
		while graph[i]:
			k = graph[i].popleft()
			if not vis[k]:
				vis[k] = 1
				for h in graph[k]:
					graph[i].append(h)
				if inf > arr[k]:
					inf = arr[k]
		r += inf
print(r)


import sys
(n, m) = list(map(int, sys.stdin.buffer.readline().decode('utf-8').split()))
a = list(map(int, sys.stdin.buffer.readline().decode('utf-8').split()))
adj = [[] for _ in range(n)]
for (u, v) in (map(int, line.decode('utf-8').split()) for line in sys.stdin.buffer):
	adj[u - 1].append(v - 1)
	adj[v - 1].append(u - 1)
visited = [0] * n
ans = 0
for i in range(n):
	if visited[i]:
		continue
	visited[i] = 1
	cost = a[i]
	stack = [i]
	while stack:
		v = stack.pop()
		for dest in adj[v]:
			if visited[dest]:
				continue
			visited[dest] = 1
			cost = min(cost, a[dest])
			stack.append(dest)
	ans += cost
print(ans)


from __future__ import division, print_function
import itertools, os, sys
from io import BytesIO, IOBase

def ii():
	return int(input())

def si():
	return str(input())

def mi():
	return map(int, input().strip().split(' '))

def li():
	return list(mi())
from collections import deque, Counter, OrderedDict, defaultdict
import threading

def dmain():
	sys.setrecursionlimit(100000000)
	threading.stack_size(40960000)
	thread = threading.Thread(target=main)
	thread.start()
abc = 'abcdefghijklmnopqrstuvwxyz'
abd = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod = 1000000007
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def getKey(item):
	return item[0]

def sort2(l):
	return sorted(l, key=getKey)

def d2(n, m, num):
	return [[num for x in range(m)] for y in range(n)]

def isPowerOfTwo(x):
	return x and (not x & x - 1)

def decimalToBinary(n):
	return bin(n).replace('0b', '')

def ntl(n):
	return [int(i) for i in str(n)]

def powerMod(x, y, p):
	res = 1
	x %= p
	while y > 0:
		if y & 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p1
	return res

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x
graph = defaultdict(list)
graph = defaultdict(list)
visited = [False] * 1000000
mn = 0
l = []

def dfs(v):
	global mn
	visited[v] = True
	mn = min(mn, c[v - 1])
	for i in graph[v]:
		if visited[i] == False:
			dfs(i)
(n, m) = mi()
c = li()

def main():
	for i in range(m):
		(x, y) = mi()
		graph[x].append(y)
		graph[y].append(x)
	ans = 0
	for i in range(1, n + 1):
		if visited[i] == False:
			global mn
			mn = c[i - 1]
			dfs(i)
			ans += mn
	print(ans)
dmain()


(n, m) = map(int, input().split())
l = list(map(int, input().split()))
L = [[] for i in range(n)]
L1 = [-1] * n
k = 0
for i in range(m):
	(a, b) = map(int, input().split())
	L[a - 1].append(b - 1)
	L[b - 1].append(a - 1)
for i in range(n):
	if L1[i] == -1:
		V = [i]
		mi = l[i]
		L1[i] = 0
		while len(V) != 0:
			for x in L[V[0]]:
				if L1[x] == -1:
					V.append(x)
					L1[x] = 0
					mi = min(l[x], mi)
			del V[0]
		k += mi
print(k)


from collections import deque

def dfs(i):
	queue = deque([i])
	while queue:
		ele = queue.popleft()
		if visited[ele] is False:
			for friend in graph[ele]:
				if visited[friend] is False:
					queue.append(friend)
		visited[ele] = True
(n, m) = map(int, input().split())
gold = sorted(zip(map(int, input().split()), range(n)))
graph = [[] for _ in range(n)]
for i in range(m):
	(a, b) = map(int, input().split())
	graph[a - 1].append(b - 1)
	graph[b - 1].append(a - 1)
visited = [False] * n
ans = 0
for i in range(n):
	(a, b) = gold[i]
	if visited[b] is False:
		dfs(b)
		ans += a
print(ans)


import sys

def find(n, list2):
	r = n
	while list2[r] != r:
		r = list2[r]
	i = n
	while i != r:
		temp = list2[i]
		list2[i] = r
		i = temp
	return r

def join(a, b, list2, list1):
	x = find(a, list2)
	y = find(b, list2)
	if x != y:
		list2[y] = x
		list1[x] = min(list1[x], list1[y])

def main():
	sum = 0
	p = sys.stdin.readline().strip('\n')
	if p == '':
		return
	(n, m) = map(int, p.split(' '))
	list2 = [i for i in range(n)]
	p = sys.stdin.readline().strip('\n')
	list1 = [i for i in map(int, p.split(' '))]
	for i in range(m):
		(a, b) = map(int, sys.stdin.readline().strip('\n').split(' '))
		join(a - 1, b - 1, list2, list1)
	for i in range(n):
		if i == find(i, list2):
			sum += list1[i]
	print(sum)
main()


import sys
import threading
from math import *
sys.setrecursionlimit(300000)
threading.stack_size(10 ** 8)

def main():

	def dfs(vertex, g, used):
		used.add(vertex)
		ms = c[vertex]
		if vertex in g:
			for neigthbor in g[vertex]:
				if neigthbor not in used:
					ms = min(dfs(neigthbor, g, used), ms)
		return ms
	used = set()
	S = 0
	(n, m) = map(int, input().split())
	c = [int(s) for s in input().split()]
	g = dict()
	for _ in range(m):
		(a, b) = map(int, input().split())
		a -= 1
		b -= 1
		if a not in g:
			g[a] = {b}
		else:
			g[a].add(b)
		if b not in g:
			g[b] = {a}
		else:
			g[b].add(a)
	for vertex in range(n):
		if vertex not in used:
			S += dfs(vertex, g, used)
	print(S)
t = threading.Thread(target=main)
t.start()
t.join()


import sys
import math
from collections import deque

def II():
	return int(sys.stdin.readline())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def MI():
	return map(int, sys.stdin.readline().split())

def SI():
	return sys.stdin.readline().strip()

def FACT(n, mod):
	s = 1
	facts = [1]
	for i in range(1, n + 1):
		s *= i
		s %= mod
		facts.append(s)
	return facts[n]

def C(n, k, mod):
	return FACT(n, mod) * pow(FACT(k, mod) * FACT(n - k, mod) % mod, mod - 2, mod) % mod
(n, m) = MI()
c = LI()
d = [[] for i in range(n)]
for i in range(m):
	(x, y) = MI()
	x -= 1
	y -= 1
	d[x].append(y)
	d[y].append(x)
v = [0 for i in range(n)]
ans = 0
for i in range(n):
	if v[i] == 0:
		q = deque([i])
		m = c[i]
		while q:
			temp = q.popleft()
			v[temp] = 1
			m = min(m, c[temp])
			for x in d[temp]:
				if v[x] == 0:
					q.append(x)
		ans += m
print(ans)


def dfs(graph, bribes, start=1):
	n = len(graph)
	stack = [start]
	visited[start] = True
	mini = bribes[start]
	ans = 0
	while stack:
		node = stack.pop()
		for child in graph[node]:
			if not visited[child]:
				mini = min(mini, bribes[child])
				visited[child] = True
				stack.append(child)
	return mini
(n, m) = map(int, input().split())
arr = [[] for i in range(n + 1)]
bribes = [0] + list(map(int, input().split()))
for i in range(m):
	(x, y) = map(int, input().split())
	arr[x].append(y)
	arr[y].append(x)
visited = [False] * (n + 1)
tot = 0
for i in range(1, n + 1):
	if not visited[i]:
		tot += dfs(arr, bribes, i)
print(tot)


(n, m) = map(int, input().split())
l = [int(j) for j in input().split()]
cv = [0] * n
dp = [[] for i in range(n)]
for i in range(m):
	(x, y) = map(int, input().split())
	(x, y) = (x - 1, y - 1)
	dp[x].append(y)
	dp[y].append(x)
ans = 0
for i in range(n):
	if cv[i] == 0:
		mx = l[i]
		q = [i]
		cv[i] = 1
		while q:
			x = q.pop()
			for t in dp[x]:
				if cv[t] == 0:
					cv[t] = 1
					mx = min(l[t], mx)
					q.append(t)
		ans += mx
print(ans)


from sys import setrecursionlimit
import threading
setrecursionlimit(10 ** 9)
threading.stack_size(67108864)

def main():

	def dfs(v, cv):
		color[v] = cv
		for u in d[v]:
			if color[u] == 0:
				dfs(u, cv)
	(n, m) = map(int, input().split())
	c = list(map(int, input().split()))
	d = {x: [] for x in range(n + 1)}
	for i in range(m):
		(x, y) = map(int, input().split())
		d[x].append(y)
		d[y].append(x)
	color = [0] * (n + 1)
	cv = 1
	for i in range(1, n + 1):
		if color[i] == 0:
			dfs(i, cv)
			cv += 1
	s = {}
	for i in range(1, n + 1):
		if color[i] in s:
			s[color[i]].append(c[i - 1])
		else:
			s[color[i]] = [c[i - 1]]
	x = 0
	for i in s:
		x += min(s[i])
	print(x)
thread = threading.Thread(target=main)
thread.start()


from collections import defaultdict

def BFS(v, graph, visited):
	stack = [v]
	while stack:
		cur_vertex = stack.pop()
		visited[cur_vertex] = True
		for u in graph[cur_vertex]:
			if not visited[u]:
				visited[u] = True
				stack += [child for child in graph[u] if not visited[child]]

def solve(n, cost, graph):
	sorted_vertices = list(range(n))
	sorted_vertices.sort(key=cost.__getitem__)
	visited = [False] * n
	total_cost = 0
	for v in sorted_vertices:
		if not visited[v]:
			BFS(v, graph, visited)
			total_cost += cost[v]
	return total_cost
(n, m) = map(int, input().split())
c = tuple(map(int, input().split()))
graph = defaultdict(list)
for _ in range(m):
	edge = input().split()
	(u, v) = (int(edge[0]) - 1, int(edge[1]) - 1)
	graph[u].append(v)
	graph[v].append(u)
print(solve(n, c, graph))


def fact(x):
	if x == 0:
		return 1
	else:
		return x * fact(x - 1)

def lower_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] >= num:
			answer = middle
			end = middle - 1
		else:
			start = middle + 1
	return answer

def upper_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] <= num:
			answer = middle
			start = middle + 1
		else:
			end = middle - 1
	return answer

def abs(x):
	return x if x >= 0 else -x

def binary_search(li, val, lb, ub):
	ans = 0
	while lb <= ub:
		mid = (lb + ub) // 2
		if li[mid] > val:
			ub = mid - 1
		elif val > li[mid]:
			lb = mid + 1
		else:
			ans = 1
			break
	return ans

def kadane(x):
	sum_so_far = 0
	current_sum = 0
	for i in x:
		current_sum += i
		if current_sum < 0:
			current_sum = 0
		else:
			sum_so_far = mpos(sum_so_far, current_sum)
	return sum_so_far

def pref(li):
	pref_sum = [0]
	for i in li:
		pref_sum.append(pref_sum[-1] + i)
	return pref_sum
import sys
import threading
from math import *
sys.setrecursionlimit(300000)
threading.stack_size(10 ** 8)

def main():
	for _ in range(1):
		(n, m) = map(int, input().split())
		x = list(map(int, input().split()))
		adj = dict()
		for i in range(1, n + 1):
			adj.setdefault(i, [])
		for i in range(m):
			(a, b) = map(int, input().split())
			adj[a] += [b]
			adj[b] += [a]
		vis = [0] * n

		def dfs(n, ans):
			vis[n - 1] = 1
			ans = min(ans, x[n - 1])
			for i in adj[n]:
				if vis[i - 1] == 0:
					ans = min(ans, dfs(i, ans))
			return ans
		final = 0
		for i in range(n):
			if vis[i] == 0:
				ans = 1000000000.0 + 10
				final += dfs(i + 1, ans)
		print(final)
t = threading.Thread(target=main)
t.start()
t.join()


class Solution(object):

	def __init__(self):
		self.solve()

	def next(self, func=int):
		temp = list(map(func, input().split()))
		return temp if len(temp) > 1 else temp[0]

	def solve(self):
		(n, m) = self.next()
		cost = self.next()
		pairs = [[] for x in range(n)]
		if not m:
			print(sum(cost) if type(cost) is list else cost)
			exit()
		for _ in range(m):
			(firend1, firend2) = self.next(lambda x: int(x) - 1)
			pairs[firend1].append(firend2)
			pairs[firend2].append(firend1)
		visited = [False] * n
		total = 0
		for i in range(n):
			if visited[i]:
				continue
			stack = [i]
			visited[i] = True
			cheaper = cost[i]
			while stack:
				for i in pairs[stack.pop()]:
					if visited[i]:
						continue
					stack.append(i)
					cheaper = min(cheaper, cost[i])
					visited[i] = True
			total += cheaper
		print(total)
Solution()


def solve():
	from collections import defaultdict, deque
	nm = input().split()
	n = int(nm[0])
	m = int(nm[1])
	s = list(map(int, input().split()))
	all_cost = []
	for i in range(n):
		all_cost.append([i + 1, s[i]])
	all_cost = sorted(all_cost, key=lambda item: item[1])
	heard = [False for _ in range(n + 1)]
	friends = defaultdict(list)
	for i in range(m):
		uv = input().split()
		u = int(uv[0])
		v = int(uv[1])
		friends[u].append(v)
		friends[v].append(u)
	total = 0
	for i in range(n):
		bribe = all_cost[i][0]
		if not heard[bribe]:
			heard[bribe] = True
			total += s[bribe - 1]
			q = deque()
			q.append(bribe)
			while q:
				cur = q.popleft()
				for friend in friends[cur]:
					if not heard[friend]:
						heard[friend] = True
						q.append(friend)
	print(total)
solve()


def bfs(i):
	nonlocal p, f, v
	q = [i]
	qi = 0
	v[i] = 1
	best = p[i]
	while qi < len(q):
		j = q[qi]
		if p[j] < best:
			best = p[j]
		for k in f[j]:
			if v[k] != 1:
				q.append(k)
				v[k] = 1
		qi += 1
	return best
(n, m) = [int(i) for i in input().split(' ')]
p = [int(i) for i in input().split(' ')]
v = [0] * n
f = {}
for a in range(n):
	f[a] = []
for a in range(m):
	(x, y) = [int(i) for i in input().split(' ')]
	f[x - 1].append(y - 1)
	f[y - 1].append(x - 1)
cost = 0
for i in range(n):
	if v[i] == 0:
		cost += bfs(i)
print(cost)


from sys import stdout, stdin, setrecursionlimit
from io import BytesIO, IOBase
from collections import *
from itertools import *
from bisect import *
from string import *
from queue import *
from heapq import *
from math import *
from re import *
from os import *

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = read(self._fd, max(fstat(self._fd).st_size, 8192))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = read(self._fd, max(fstat(self._fd).st_size, 8192))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(stdin, stdout) = (IOWrapper(stdin), IOWrapper(stdout))
(graph, mod, szzz) = ({}, 10 ** 9 + 7, lambda : sorted(zzz()))

def getStr():
	return input()

def getInt():
	return int(input())

def listStr():
	return list(input())

def getStrs():
	return input().split()

def isInt(s):
	return '0' <= s[0] <= '9'

def input():
	return stdin.readline().strip()

def zzz():
	return [int(i) for i in input().split()]

def output(answer, end='\n'):
	stdout.write(str(answer) + end)

def lcd(xnum1, xnum2):
	return xnum1 * xnum2 // gcd(xnum1, xnum2)
dx = [-1, 1, 0, 0, 1, -1, 1, -1]
dy = [0, 0, 1, -1, 1, -1, -1, 1]
daysInMounth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
(n, m) = zzz()
arr = [0] + zzz()
for i in range(n):
	graph[i + 1] = []
for i in range(m):
	(x, y) = zzz()
	graph[x].append(y)
	graph[y].append(x)

def solve(start):
	coin = arr[start]
	que = deque()
	que.append(start)
	while que:
		x = que.pop()
		for i in graph[x]:
			if vis[i]:
				continue
			que.append(i)
			vis[i] = 1
			coin = min(coin, arr[i])
	return coin
s = 0
vis = [0] * (n + 1)
for i in range(n):
	if vis[i + 1]:
		continue
	s += solve(i + 1)
print(s)


(n, m) = map(int, input().split())
c = [0] + list(map(int, input().split()))
parent = [i for i in range(n + 1)]

def find(i):
	while i != parent[i]:
		parent[i] = parent[parent[i]]
		i = parent[i]
	return i
for i in range(m):
	(a, b) = map(lambda x: find(int(x)), input().split())
	if c[a] < c[b]:
		(a, b) = (b, a)
	parent[a] = b
print(sum([c[i] if i == parent[i] else 0 for i in range(1, n + 1)]))


import sys
import threading
from collections import defaultdict
from operator import itemgetter
sys.setrecursionlimit(10 ** 5)
threading.stack_size(10 ** 8)
visited = set()
all_visited = set()

def main():
	input_list = get_input()
	print(solve(input_list))

def get_input():
	input_list = []
	for line in sys.stdin:
		input_list.append(line.rstrip('\n'))
	return input_list

def generate_graph(input_list):
	graph = defaultdict(set)
	vertices = [(a, b) for (a, b) in enumerate([int(x) for x in input_list[1].split(' ')], start=1)]
	edges = [[int(x) for x in y.split(' ')] for y in input_list[2:]]
	for vertex in vertices:
		graph[vertex[0], vertex[1]] = set()
	for edge in edges:
		v1 = vertices[edge[0] - 1]
		v2 = vertices[edge[1] - 1]
		graph[v1].add(v2)
		graph[v2].add(v1)
	return graph

def solve(input_list):
	global visited
	global all_visited
	visited = set()
	all_visited = set()
	neighborhoods = set()
	total = 0
	graph = generate_graph(input_list)
	for vertex in graph:
		if vertex not in all_visited:
			visited = set()
			dfs(graph, vertex)
			neighborhoods.add(frozenset(visited))
	for neighborhood in neighborhoods:
		total += min(neighborhood, key=itemgetter(1))[1]
	return total

def dfs(graph, vertex):
	global visited
	global all_visited
	if vertex not in visited:
		visited.add(vertex)
		all_visited.add(vertex)
		for adjacent in graph[vertex]:
			dfs(graph, adjacent)
t = threading.Thread(target=main)
t.start()
t.join()


import sys
get = sys.stdin.readline
(n, m) = map(int, get().split())
cost = list(map(int, get().split()))
adv = [[] for i in range(n)]
visited = [0] * n
sum = 0
for (v, u) in (map(int, get().split()) for _ in range(m)):
	adv[u - 1].append(v - 1)
	adv[v - 1].append(u - 1)
for i in range(n):
	if visited[i]:
		continue
	c = cost[i]
	stack = [i]
	visited[i] = 1
	while stack:
		v = stack.pop()
		for d in adv[v]:
			if visited[d]:
				continue
			visited[d] = 1
			if c > cost[d]:
				c = cost[d]
			stack.append(d)
	sum += c
sys.stdout.write(str(sum) + '\n')


from collections import defaultdict

def dfs(grafo, inicio):
	revisados = set()
	stack = list()
	stack.append(inicio)
	while len(stack) != 0:
		aux = stack.pop()
		if aux in revisados:
			continue
		else:
			revisados.add(aux)
			stack += list(grafo[aux] - revisados)
	costo_min = min([g[v - 1] for v in revisados])
	return (revisados, costo_min)

def main():
	(n, m) = map(int, input().split())
	chars = set(range(1, n + 1))
	global g
	g = list(map(int, input().split(' ')))
	parejas = defaultdict(set)
	if m == 0:
		print(sum(g))
		return
	for i in range(m):
		(a, b) = map(int, input().split())
		parejas[a].add(b)
		parejas[b].add(a)
	total = 0
	while len(chars) != 0:
		actual = chars.pop()
		(revisado, g_actual) = dfs(parejas, actual)
		chars -= revisado
		total += g_actual
	print(total)
	return
main()


import os
import sys
import math
from io import BytesIO, IOBase
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())

def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)

def lcm(a, b):
	return a * b // gcd(a, b)

def ncr(n, r):
	return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def npr(n, r):
	return math.factorial(n) // math.factorial(n - r)

def seive(n):
	primes = [True] * (n + 1)
	ans = []
	for i in range(2, n):
		if not primes[i]:
			continue
		j = 2 * i
		while j <= n:
			primes[j] = False
			j += i
	for p in range(2, n + 1):
		if primes[p]:
			ans += [p]
	return ans

def factors(n):
	factors = []
	x = 1
	while x * x <= n:
		if n % x == 0:
			if n // x == x:
				factors.append(x)
			else:
				factors.append(x)
				factors.append(n // x)
		x += 1
	return factors

def main():
	try:
		(n, m) = invr()
		a = inlt()
		adj = [[] for _ in range(n)]
		visited = [False] * n
		components = []
		for _ in range(m):
			(x, y) = invr()
			x -= 1
			y -= 1
			adj[x].append(y)
			adj[y].append(x)

		def dfs(v):
			comp = []
			q = [v]
			while q:
				vertex = q.pop()
				visited[vertex] = True
				comp.append(a[vertex])
				for vertices in adj[vertex]:
					if not visited[vertices]:
						q.append(vertices)
			return comp
		for i in range(n):
			if not visited[i]:
				components.append(dfs(i))
		ans = 0
		for l in components:
			ans += min(l)
		print(ans)
	except Exception as e:
		print(e)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


from sys import stdin, stdout
import math
from collections import deque

class Graph:

	def __init__(self, n):
		self.mygraph = [[] for x in range(1) for y in range(n + 1)]

	def addEdge(self, u, v):
		self.mygraph[u].append(v)
		self.mygraph[v].append(u)

	def display_graph(self):
		print(self.mygraph)

def bfs(g, source, cost, visited):
	c = 1000000000
	queue = deque()
	count = 0
	queue.append(source)
	count += 1
	while count != 0:
		node = queue.pop()
		count -= 1
		visited[node] = True
		c = min(c, cost[node])
		for i in g.mygraph[node]:
			if visited[i] == False:
				queue.append(i)
				count += 1
	return c

def myfunction(g, cost, n):
	visited = [False] * (n + 1)
	min_cost = 0
	for i in range(1, n + 1):
		if visited[i] == False:
			visited[i] = True
			min_cost += bfs(g, i, cost, visited)
	return min_cost
(n, m) = list(map(int, stdin.readline().split()))
cost = list(map(int, stdin.readline().split()))
cost.insert(0, 0)
g = Graph(n)
for i in range(m):
	(u, v) = list(map(int, stdin.readline().split()))
	g.addEdge(u, v)
print(myfunction(g, cost, n))


(N, M) = list(map(int, input().split()))
cost = list(map(int, input().split()))
graph = [[] for _ in range(N)]
for _ in range(M):
	(u, v) = list(map(int, input().split()))
	u -= 1
	v -= 1
	graph[u].append(v)
	graph[v].append(u)

def DFS(node, min_cost):
	stack = [node]
	while stack:
		last = stack.pop()
		for nei in graph[last]:
			if visited[nei] == False:
				visited[nei] = True
				min_cost = min(min_cost, cost[nei])
				stack.append(nei)
	return min_cost
visited = [False] * N
result = 0
for idx in range(N):
	if graph[idx]:
		if visited[idx] == False:
			visited[idx] = True
			result += DFS(idx, cost[idx])
for (idx, visit) in enumerate(visited):
	if visit == False:
		result += cost[idx]
print(result)


(par, rk, minc, c) = (list(range(100010)), list(range(100010)), list(range(100010)), list(range(100010)))

def init(n):
	for i in list(range(1, n + 1)):
		par[i] = i
		rk[i] = 0
		minc[i] = c[i]

def find(x):
	if par[x] == x:
		return x
	par[x] = find(par[x])
	return par[x]

def unite(x, y):
	(x, y) = (find(x), find(y))
	if x == y:
		return
	if rk[x] < rk[y]:
		par[x] = y
		minc[y] = min(minc[y], minc[x])
	else:
		par[y] = x
		minc[x] = min(minc[x], minc[y])
		if rk[x] == rk[y]:
			rk[x] += 1
(n, m) = map(int, input().split())
c.clear()
c.append(0)
c = c + list(map(int, input().split()))
init(n)
for i in range(m):
	(x, y) = map(int, input().split())
	unite(x, y)
ans = 0
for i in list(range(1, n + 1)):
	if find(i) == i:
		ans += minc[i]
print(ans)


(n, m) = map(int, input().split())
c = list(map(int, input().split()))
g = [[] for i in range(n)]
for i in range(m):
	(a, b) = map(int, input().split())
	g[a - 1].append(b - 1)
	g[b - 1].append(a - 1)
vs = [False for i in range(n)]
ans = 0
for i in range(n):
	if vs[i]:
		continue
	q = [i]
	vs[i] = True
	m = c[i]
	while len(q) > 0:
		v = q[0]
		del q[0]
		for u in g[v]:
			if not vs[u]:
				vs[u] = True
				q.append(u)
				m = min(m, c[u])
	ans += m
print(ans)


from collections import defaultdict

class Graph(object):

	def __init__(self, connections, directed=False):
		self._graph = defaultdict(set)
		self._directed = directed
		self.add_connections(connections)

	def add_connections(self, connections):
		for (node1, node2) in connections:
			self.add(node1, node2)

	def add(self, node1, node2):
		self._graph[node1].add(node2)
		if not self._directed:
			self._graph[node2].add(node1)

	def __str__(self):
		return '{}({})'.format(self.__class__.__name__, dict(self._graph))

	def __getitem__(self, index):
		return self._graph[index]

def bfs(graph, start):
	(visited, queue) = (set(), [start])
	while queue:
		vertex = queue.pop(0)
		if vertex not in visited:
			visited.add(vertex)
			queue.extend(graph[vertex] - visited)
	return visited
(n, m) = list(map(int, input().split()))
g = list(map(int, input().split()))
f = [list(map(int, input().split())) for _ in range(m)]
v = Graph(f)
fg = []
vi = [False] * n
for i in range(0, n):
	if vi[i]:
		continue
	cc = bfs(v, i + 1)
	for e in cc:
		vi[e - 1] = True
	fg.append(cc)
print(sum((min((g[e - 1] for e in _g)) for _g in fg)))


import os
import sys
from math import *
from collections import *
from bisect import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def value():
	return tuple(map(int, input().split()))

def array():
	return [int(i) for i in input().split()]

def Int():
	return int(input())

def Str():
	return input()

def arrayS():
	return [i for i in input().split()]

def dfs(graph, start_vertex):
	traversal = []
	stack = [start_vertex]
	ans = a[start_vertex - 1]
	while stack:
		vertex = stack.pop()
		ans = min(ans, a[vertex - 1])
		if vertex not in visited:
			visited.add(vertex)
			traversal.append(vertex)
			stack.extend(reversed(graph[vertex]))
	return ans
(n, q) = value()
a = array()
adj = {i: [] for i in range(1, n + 1)}
for i in range(q):
	(s, d) = value()
	adj[s].append(d)
	adj[d].append(s)
ans = 0
visited = set()
for i in range(1, n + 1):
	if i not in visited:
		ans += dfs(adj, i)
print(ans)


(n, m) = map(int, input().split())
c = list(map(int, input().split()))
if m == 0:
	print(sum(c))
	exit()
graph = {}
for i in range(m):
	(x, y) = map(int, input().split())
	if x in graph:
		graph[x].append(y)
	else:
		graph[x] = [y]
	if y in graph:
		graph[y].append(x)
	else:
		graph[y] = [x]
is_checked = [False] * (n + 1)
result = 0
for key in graph:
	if not is_checked[key]:
		minimum = c[key - 1]
		stack = [key]
		while len(stack) > 0:
			top = stack.pop(-1)
			minimum = min(c[top - 1], minimum)
			is_checked[top] = True
			vals = graph[top]
			for i in range(len(vals)):
				if not is_checked[vals[i]]:
					stack.append(vals[i])
		result += minimum
for i in range(n):
	if not is_checked[i + 1]:
		result += c[i]
print(result)


(n, m) = map(int, input().split())
c = list(map(int, input().split()))
c.insert(0, 0)
friends = [[] for i in range(n + 1)]
for _ in range(m):
	(a, b) = map(int, input().split())
	friends[a].append(b)
	friends[b].append(a)
visited = [False] * (n + 1)

def bfs(i):
	w = c[i]
	q = [i]
	while q:
		j = q.pop()
		visited[j] = True
		w = min(w, c[j])
		for k in friends[j]:
			if visited[k] == False:
				q.append(k)
	return w
ans = 0
for i in range(1, n + 1):
	if visited[i] == False:
		ans += bfs(i)
print(ans)


from collections import defaultdict
merge_matrix = defaultdict(list)
(n, m) = list(map(int, input().split()))
price_list = [int(x) for x in input().split()]
summary = 0
visited = set()

def dfs(edge):
	queue = []
	visited.add(edge)
	queue.append(edge)
	lowest_cost = price_list[edge - 1]
	while len(queue) > 0:
		cur_edge = queue.pop()
		for i in merge_matrix[cur_edge]:
			if i not in visited:
				lowest_cost = min(lowest_cost, price_list[i - 1])
				queue.append(i)
				visited.add(i)
	return lowest_cost
for i in range(m):
	(x, y) = list(map(int, input().split()))
	merge_matrix[x].append(y)
	merge_matrix[y].append(x)
for i in range(1, n + 1):
	if i not in visited:
		summary += dfs(i)
for i in range(1, n + 1):
	if i not in visited:
		summary += price_list[i - 1]
print(summary)


X = list(map(int, input().split()))
(People, Friends) = (list(map(int, input().split())), [[] for i in range(X[0])])
(Status, Sum) = ([False] * X[0], 0)
for i in range(X[1]):
	Temp = list(map(int, input().split()))
	Friends[Temp[0] - 1].append(Temp[1] - 1)
	Friends[Temp[1] - 1].append(Temp[0] - 1)
for i in range(X[0]):
	if not Status[i]:
		Stack = [i]
		MIN = People[i]
		while len(Stack):
			Last = Stack.pop()
			Status[Last] = True
			MIN = min(MIN, People[Last])
			for j in Friends[Last]:
				if not Status[j]:
					Stack.append(j)
		Sum += MIN
print(Sum)


def get():
	return list(map(int, input().split()))

def dfs(i):
	v = c[i]
	q = [i]
	while q:
		j = q.pop()
		b[j] = 1
		v = min(v, c[j])
		for k in a[j]:
			if b[k] == 0:
				q.append(k)
	return v
(n, m) = get()
c = get()
a = [list() for _ in range(n)]
for _ in range(m):
	(x, y) = get()
	a[x - 1].append(y - 1)
	a[y - 1].append(x - 1)
b = [0] * n
ans = 0
for i in range(n):
	if b[i] == 0:
		ans += dfs(i)
print(ans)


class CodeforcesTask893CSolution:

	def __init__(self):
		self.result = ''
		self.n_m = []
		self.costs = []
		self.connections = []

	def read_input(self):
		self.n_m = [int(x) for x in input().split(' ')]
		self.costs = [int(x) for x in input().split(' ')]
		for x in range(self.n_m[1]):
			self.connections.append([int(y) for y in input().split(' ')])

	def process_task(self):
		graph = [[] for x in range(self.n_m[0])]
		for conn in self.connections:
			graph[conn[0] - 1].append(conn[1])
			graph[conn[1] - 1].append(conn[0])
		comp_cnt = 0
		comp_costs = []
		component = [0] * self.n_m[0]
		for x in range(self.n_m[0]):
			if not component[x]:
				comp_cnt += 1
				mn = 10 ** 10
				to_visit = [x + 1]
				while to_visit:
					visiting = to_visit.pop(-1)
					if not component[visiting - 1]:
						component[visiting - 1] = comp_cnt
						to_visit += graph[visiting - 1]
						mn = min(mn, self.costs[visiting - 1])
				comp_costs.append(mn)
		self.result = str(sum(comp_costs))

	def get_result(self):
		return self.result
Solution = CodeforcesTask893CSolution()
Solution.read_input()
Solution.process_task()
print(Solution.get_result())


(n, m) = map(int, input().split())
arr = list(map(int, input().split()))
graph = [[] for i in range(n + 1)]
for i in range(m):
	(x, y) = map(int, input().split())
	graph[x].append(y)
	graph[y].append(x)
globalVisited = [0 for _ in range(n + 1)]
temp = 10000000000

def dfs(node):
	global temp, globalVisited
	stack = [node]
	temp = arr[node - 1]
	while stack:
		vertex = stack.pop()
		if globalVisited[vertex] == 0:
			globalVisited[vertex] = 1
			temp = min(temp, arr[vertex - 1])
			for neighbors in graph[vertex]:
				if globalVisited[neighbors] == 0:
					stack.append(neighbors)
	return temp
ans = 0
for i in range(1, n + 1):
	if globalVisited[i] == 0:
		ans += dfs(i)
print(ans)


def root(arr, i):
	while arr[i] != i:
		arr[i] = arr[arr[i]]
		i = arr[i]
	return i

def union(arr, a, b, s):
	ra = root(arr, a)
	rb = root(arr, b)
	if ra == rb:
		return
	if c[ra] > c[rb]:
		arr[ra] = arr[rb]
		s[rb] += s[ra]
	else:
		arr[rb] = arr[ra]
		s[ra] += s[rb]

def find(arr, a, b):
	if root(arr, a) == root(arr, b):
		return True
	return False
(n, m) = map(int, input().split())
a = [i for i in range(n + 1)]
s = [1 for i in range(n + 1)]
c = [0] + list(map(int, input().split()))
for _ in range(m):
	(u, v) = map(int, input().split())
	ru = root(a, u)
	rv = root(a, v)
	if ru != rv:
		a[rv] = ru
	c[ru] = min(c[ru], c[rv])
ans = 0
for i in range(len(a)):
	if a[i] == i:
		ans += c[i]
print(ans)


(n, m) = map(int, input().split())
from collections import defaultdict
ar = list(map(int, input().split()))
ax = defaultdict(list)
ans = 0
for i in range(m):
	(a, b) = map(int, input().split())
	a = a - 1
	b = b - 1
	ax[a].append(b)
	ax[b].append(a)
if m == 0:
	print(sum(ar))
else:
	vis = [False] * n
	for i in range(n):
		if vis[i] == False:
			qu = []
			qu.append(i)
			a = ar[i]
			while qu:
				u = qu.pop(0)
				a = min(a, ar[u])
				vis[u] = True
				for j in ax[u]:
					if vis[j] == False:
						qu.append(j)
			ans += a
	print(ans)


def bfs(graph, start):
	global price
	min = 100000000000000000000000
	global done
	queue = [start]
	i = 0
	while i != len(queue):
		if done[queue[i] - 1] == 1:
			i += 1
		else:
			done[queue[i] - 1] = 1
			if price[queue[i] - 1] < min:
				min = price[queue[i] - 1]
			for j in range(0, len(graph[queue[i]])):
				if done[graph[queue[i]][j] - 1] == 0:
					queue.append(graph[queue[i]][j])
			i += 1
	return min
(n, m) = map(int, input().split())
price = list(map(int, input().split()))
graph = {}
sum = 0
for i in range(1, n + 1):
	graph[i] = []
done = [0] * n
for i in range(0, m):
	(a, b) = map(int, input().split())
	graph[b].append(a)
	graph[a].append(b)
for i in range(1, n + 1):
	if done[i - 1] == 0:
		if graph[i] != []:
			min = bfs(graph, i)
			sum = sum + min
		else:
			sum += price[i - 1]
			done[i - 1] = 1
print(sum)


arr = [0 for i in range(0, int(100000.0) + 6)]
adj = [[] for i in range(0, int(100000.0) + 6)]
vis = [0 for i in range(0, int(100000.0) + 6)]

def BFS(index):
	que = []
	mini = arr[index]
	que.append(index)
	while len(que) != 0:
		cur = que[0]
		que.remove(que[0])
		vis[cur] = 1
		mini = min(mini, arr[cur])
		for i in range(0, len(adj[cur])):
			if vis[adj[cur][i]] == 0:
				que.append(adj[cur][i])
	return mini
(n, m) = map(int, input().split(' '))
k = list(map(int, input().split(' ')))
for i in range(0, n):
	arr[i] = k[i]
for i in range(0, m):
	(a, b) = map(int, input().split(' '))
	a -= 1
	b -= 1
	adj[a].append(b)
	adj[b].append(a)
sum = 0
for i in range(0, n):
	if vis[i] == 0:
		sum += BFS(i)
print(sum)


R = lambda : map(int, input().split())
(n, m) = R()
c = [0] + list(R())
f = list(range(n + 1))

def fd(a):
	(rt, tmp) = (a, a)
	while f[rt] != rt:
		rt = f[rt]
	while f[a] != a:
		tmp = f[a]
		f[a] = rt
		a = tmp
	return rt
for _ in range(m):
	(x, y) = R()
	(rx, ry) = (fd(x), fd(y))
	if c[rx] > c[ry]:
		f[rx] = ry
	else:
		f[ry] = rx
print(sum((c[i] for i in range(n + 1) if f[i] == i)))


import sys, threading
sys.setrecursionlimit(100000)

def dfs(node, adj, visited, cost):
	visited[node] = 1
	c = 10 ** 10
	for edge in adj[node]:
		if visited[edge] == 0:
			c = min(dfs(edge, adj, visited, cost), c)
	return min(cost[node], c)

def main():
	tmp = input().split(' ')
	n = int(tmp[0])
	m = int(tmp[1])
	cost = [int(x) for x in input().split(' ')]
	visited = [0 for _ in range(n)]
	adj = [[] for _ in range(n)]
	for _ in range(m):
		tmp = input().split(' ')
		x = int(tmp[0]) - 1
		y = int(tmp[1]) - 1
		adj[x].append(y)
		adj[y].append(x)
	tcost = 0
	for friend in range(n):
		if visited[friend] == 0:
			c = dfs(friend, adj, visited, cost)
			tcost += c
	print(tcost, flush=True)
threading.stack_size(102400000)
thread = threading.Thread(target=main)
thread.start()


import sys

def dfs(node, s, l):
	visited[node] = True
	stack = [node]
	while stack:
		a = stack.pop()
		s = min(s, l[a - 1])
		for child in graph[a]:
			if visited[child] == False:
				s = min(s, l[child - 1])
				stack.append(child)
				visited[child] = True
	return s
(n, m) = map(int, input().split())
l = list(map(int, input().split()))
graph = {}
for i in range(1, n + 1):
	graph[i] = []
for i in range(m):
	(u, v) = map(int, input().split())
	graph[u].append(v)
	graph[v].append(u)
visited = [False for i in range(n + 1)]
s = sys.maxsize
ans = 0
for i in range(1, n + 1):
	if visited[i] == False:
		ans = ans + dfs(i, s, l)
print(ans)


def dfs(curr):
	s = []
	s.append(curr)
	mini = bribes[curr]
	while len(s) > 0:
		n = s.pop()
		if visited[n]:
			continue
		visited[n] = True
		mini = min(mini, bribes[n])
		for ne in aj[n]:
			s.append(ne)
	return mini
mn = [int(s) for s in input().split(' ')]
n = mn[0]
m = mn[1]
bribes = [int(x) for x in input().split(' ')]
aj = [[] for i in range(n)]
for i in range(m):
	xy = input().split(' ')
	x = int(xy[0])
	y = int(xy[1])
	aj[x - 1].append(y - 1)
	aj[y - 1].append(x - 1)
total = 0
visited = [False] * n
for i in range(n):
	if not visited[i]:
		total += dfs(i)
print(total)


from collections import deque

def dfs(n):
	nonlocal visited
	minimum = -1
	queue = deque([n])
	while queue:
		node = queue.popleft()
		visited[node] = True
		if minimum == -1:
			minimum = gold[node]
		else:
			minimum = min(minimum, gold[node])
		for neighbor in adj_list[node]:
			if not visited[neighbor - 1]:
				queue.append(neighbor - 1)
	return minimum
(n, m) = list(map(int, input().split()))
gold = list(map(int, input().split()))
adj_list = [list() for _ in range(n)]
for _ in range(m):
	(x, y) = list(map(int, input().split()))
	adj_list[x - 1].append(y)
	adj_list[y - 1].append(x)
visited = [False for _ in range(n)]
total = 0
for i in range(n):
	if not visited[i]:
		total += dfs(i)
print(total)


(n, m) = list(map(int, input().split()))
vs = list(map(int, input().split()))
if m == 0:
	print(sum(vs))
	return
es = {i: [] for i in range(n)}
visited = {i: False for i in range(n)}
comps = []
for i in range(m):
	(f, t) = list(map(int, input().split()))
	es[f - 1].append(t - 1)
	es[t - 1].append(f - 1)
for v in range(n):
	if visited[v]:
		continue
	comp = []
	deque = [v]
	while deque:
		v_temp = deque.pop(0)
		if visited[v_temp]:
			continue
		visited[v_temp] = True
		comp.append(v_temp)
		for neigh in es[v_temp]:
			if visited[neigh] or neigh == v_temp:
				continue
			deque.append(neigh)
	comps.append(comp)
res = 0
for comp in comps:
	if not comp:
		continue
	res += min((vs[i] for i in comp))
print(res)


from sys import stdin
import queue
(n, m) = list(map(int, stdin.readline().split()))
a = [0] + list(map(int, stdin.readline().split()))
ma = max(a)
d = {}
visited = [False] * (n + 1)

def min_bfs(node):
	q = queue.Queue()
	q.put(node)
	val = ma
	while not q.empty():
		t = q.get()
		if a[t] < val:
			val = a[t]
		if t in d:
			for z in d[t]:
				if not visited[z]:
					visited[z] = True
					q.put(z)
	return val
for _ in range(m):
	(t1, t2) = list(map(int, stdin.readline().split()))
	if t1 not in d:
		d[t1] = set()
	d[t1].add(t2)
	if t2 not in d:
		d[t2] = set()
	d[t2].add(t1)
res = 0
for i in range(1, n + 1):
	if not visited[i]:
		visited[i] = True
		res += min_bfs(i)
print(res)


(n, m) = map(int, input().split())
c = list(map(int, input().split()))
adj_list = [[] for _ in range(n)]
for i in range(m):
	(a, b) = map(lambda x: int(x) - 1, input().split())
	adj_list[a].append(b)
	adj_list[b].append(a)
color = [0 for i in range(n)]
min_cost = 10 ** 9

def dfs(start_v: int):
	st = [start_v]
	min_cost = c[start_v]
	while len(st) != 0:
		v = st.pop()
		color[v] = 1
		min_cost = min(min_cost, c[v])
		for u in adj_list[v]:
			if color[u] == 0:
				st.append(u)
	return min_cost
res = 0
for v in range(n):
	if color[v] == 0:
		res += dfs(v)
print(res)


import math

def main():
	(n, m) = map(int, input().split())
	arr = list(map(int, input().split()))
	adj = [[] for i in range(n)]
	for i in range(m):
		(a, b) = map(int, input().split())
		adj[a - 1].append(b - 1)
		adj[b - 1].append(a - 1)
	visited = [False] * n

	def dfs(s):
		stack = list()
		stack.append(s)
		c = math.inf
		while stack:
			s = stack.pop()
			c = min(c, arr[s])
			if not visited[s]:
				visited[s] = True
			for u in adj[s]:
				if not visited[u]:
					stack.append(u)
		return c
	summ = 0
	for i in range(n):
		if not visited[i]:
			summ += dfs(i)
	print(summ)
main()


import sys
input = sys.stdin.readline
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
g = [[] for i in range(n)]
for i in range(m):
	(u, v) = map(int, input().split())
	g[u - 1].append(v - 1)
	g[v - 1].append(u - 1)
seen = [0] * n
res = 0
for i in range(n):
	if seen[i] == 0:
		stack = [i]
		MIN = float('inf')
		while stack:
			V = stack.pop()
			seen[V] = 1
			MIN = min(MIN, a[V])
			for j in g[V]:
				if seen[j] == 0:
					stack.append(j)
		res += MIN
print(res)


from collections import defaultdict as dd
g = dd(list)

def addE(u, v):
	g[u].append(v)
	g[v].append(u)

def dfs(v):
	stck = []
	stck.append(v)
	cost1 = cost[v - 1]
	visited[v] = True
	while len(stck) != 0:
		cur = stck.pop()
		for ch in g[cur]:
			if visited[ch]:
				continue
			cost1 = min(cost1, cost[ch - 1])
			stck.append(ch)
			visited[ch] = True
	return cost1
(n, m) = map(int, input().split())
cost = [int(x) for x in input().split()]
visited = [False] * (n + 1)
for i in range(m):
	(u, v) = map(int, input().split())
	addE(u, v)
ans = 0
for i in range(1, n + 1):
	if not visited[i]:
		ans += dfs(i)
for i in range(1, n + 1):
	if not visited[i]:
		ans += cost[i - 1]
print(ans)


from collections import deque
(n, m) = list(map(int, input().split()))
c = list(map(int, input().split()))
adj = [[] for _ in range(100000)]
for _ in range(m):
	(x, y) = list(map(int, input().split()))
	x -= 1
	y -= 1
	adj[x].append(y)
	adj[y].append(x)
ans = 0
used = [False] * n
for i in range(n):
	if used[i]:
		continue
	s = [c[i]]
	used[i] = True
	q = deque([])
	q.append(i)
	while len(q) > 0:
		u = q.popleft()
		for j in range(len(adj[u])):
			v = adj[u][j]
			if not used[v]:
				used[v] = True
				s.append(c[v])
				q.append(v)
	ans += min(s)
print(ans)


import sys
input = sys.stdin.readline
from collections import deque
ans1 = 0

def bfs(n):
	if n + 1 not in visit:
		queue = deque([n])
		visit.add(n + 1)
		ans = li[n]
		while queue:
			j = queue.popleft()
			j += 1
			if j not in dict1.keys():
				break
			for i in dict1[j]:
				i -= 1
				if i + 1 not in visit:
					queue += [i]
					visit.add(i + 1)
					ans = min(ans, li[i])
		global ans1
		ans1 += ans
(x, y) = map(int, input().split())
(*li,) = map(int, input().split())
dict1 = {}
for _ in range(y):
	(a, b) = map(int, input().split())
	if a in dict1.keys():
		dict1[a].append(b)
	else:
		dict1[a] = [b]
	if b in dict1.keys():
		dict1[b].append(a)
	else:
		dict1[b] = [a]
visit = set()
for i in range(x):
	bfs(i)
print(ans1)


from io import BytesIO, IOBase
from math import sqrt
import sys, os
inpl = lambda : list(map(int, input().split()))
inpm = lambda : map(int, input().split())
inpi = lambda : int(input())
inp = lambda : input()
(rev, ra, l) = (reversed, range, len)
P = print
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')

def B(n):
	return bin(n).replace('0b', '')

def factors(n):
	arr = []
	for i in ra(2, int(sqrt(n)) + 1):
		if n % i == 0:
			arr.append(i)
			if i * i != n:
				arr.append(int(n / i))
	return arr
from bisect import bisect_left as bl
from bisect import bisect_right as br
import sys, operator, math, operator
from collections import Counter
import random
(n, m) = inpm()
li = inpl()
arr = []
omap = {}
for i in ra(m):
	arr.append(inpl())
pairs = [[i + 1] for i in ra(n)]
vis = [0] * (n + 1)
for i in ra(m):
	pairs[arr[i][0] - 1].append(arr[i][1])
	pairs[arr[i][1] - 1].append(arr[i][0])
seg = []
for i in ra(n):
	stack = [pairs[i]]
	temp = []
	if vis[stack[-1][0]] == 0:
		while len(stack) > 0:
			if vis[stack[-1][0]] == 0:
				temp.append(li[stack[-1][0] - 1])
				vis[stack[-1][0]] = 1
				s = stack.pop()
				for j in s[1:]:
					if vis[j] == 0:
						stack.append(pairs[j - 1])
			else:
				stack.pop()
		seg.append(temp)
ans = 0
for i in ra(l(seg)):
	ans += min(seg[i])
P(ans)


M = lambda : map(int, input().split())
(n, m) = M()
ls = list(M())
graph = [[] for i in range(n)]
visited = [-1 for i in range(n)]
for i in range(m):
	(a, b) = M()
	graph[a - 1].append(b - 1)
	graph[b - 1].append(a - 1)
ans = 0

def dfs(i):
	value = []
	visited[i] = 0
	q = [i]
	while q:
		x = q.pop(0)
		value.append(ls[x])
		for j in graph[x]:
			if visited[j] == -1:
				visited[j] = 0
				q.append(j)
	return min(value)
for i in range(n):
	if visited[i] == -1:
		ans += dfs(i)
print(ans)


import sys
import os
import collections
import math
S = []
size = []
n = 0

def root(a):
	while S[a] != a:
		a = S[S[a]]
		a = S[a]
	return a

def find(a, b):
	ra = root(a)
	rb = root(b)
	if ra == rb:
		return True
	else:
		return False

def union(a, b):
	ra = root(a)
	rb = root(b)
	if size[ra] < size[rb]:
		S[ra] = S[rb]
		size[rb] += size[ra]
	else:
		S[rb] = S[ra]
		size[ra] += size[rb]
s = input()
s = list(map(int, s.split()))
n = s[0]
m = s[1]
for i in range(0, n + 1):
	S.append(i)
for i in range(0, n + 1):
	size.append(1)
s = input()
s = list(map(int, s.split()))
A = []
A.append(0)
for i in s:
	A.append(i)
for i in range(0, m):
	s = input()
	s = s.split()
	s = list(map(int, s))
	u = s[0]
	v = s[1]
	if not find(u, v):
		union(u, v)
ans = [10 ** 15] * (n + 1)
for i in range(1, n + 1):
	ans[root(S[i])] = min(ans[root(S[i])], A[i])
sum_ = 0
for i in range(0, n + 1):
	if ans[i] != 10 ** 15:
		sum_ += ans[i]
print(sum_)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
mod2 = 998244353
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI():
	return [list(map(int, l.split())) for l in sys.stdin.readlines()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def pe(s):
	return print(str(s), file=sys.stderr)

def JA(a, sep):
	return sep.join(map(str, a))

def JAA(a, s, t):
	return s.join((t.join(map(str, b)) for b in a))

def IF(c, t, f):
	return t if c else f

def YES(c):
	return IF(c, 'YES', 'NO')

def Yes(c):
	return IF(c, 'Yes', 'No')

class UnionFind:

	def __init__(self, size):
		self.table = [-1 for _ in range(size)]

	def find(self, x):
		if self.table[x] < 0:
			return x
		else:
			self.table[x] = self.find(self.table[x])
			return self.table[x]

	def union(self, x, y):
		s1 = self.find(x)
		s2 = self.find(y)
		if s1 != s2:
			if self.table[s1] <= self.table[s2]:
				self.table[s1] += self.table[s2]
				self.table[s2] = s1
			else:
				self.table[s2] += self.table[s1]
				self.table[s1] = s2
			return True
		return False

	def same(self, x, y):
		return self.find(x) == self.find(y)

	def subsetall(self):
		a = []
		for i in range(len(self.table)):
			if self.table[i] < 0:
				a.append((i, -self.table[i]))
		return a

def main():
	t = 1
	rr = []
	for _ in range(t):
		(n, m) = LI()
		a = LI()
		xy = [LI_() for _ in range(m)]
		uf = UnionFind(n)
		for (x, y) in xy:
			uf.union(x, y)
		c = collections.defaultdict(lambda : inf)
		for (i, t) in enumerate(a):
			u = uf.find(i)
			c[u] = min(c[u], t)
		rr.append(sum(c.values()))
	return JA(rr, '\n')
print(main())


def DFS(graph, connected_cmpnts, vertex, isVisited):
	cmpnt = []
	stack = [vertex]
	while stack:
		curr = stack.pop()
		if not isVisited[curr - 1]:
			cmpnt.append(curr)
			isVisited[curr - 1] = True
			for kid in graph[curr]:
				stack.append(kid)
	if cmpnt:
		connected_cmpnts.append(cmpnt)

def GetMinGold(values, gold):
	min_val = float('inf')
	for i in values:
		if gold[i - 1] < min_val:
			min_val = gold[i - 1]
	return min_val

def Solve(graph, gold):
	isVisited = [False] * len(gold)
	connected_cmpnts = []
	for i in graph:
		DFS(graph, connected_cmpnts, i, isVisited)
	money = 0
	for i in connected_cmpnts:
		money += GetMinGold(i, gold)
	return money

def main():
	(n, m) = map(int, input().split())
	gold = list(map(int, input().split()))
	graph = {}
	for i in range(1, n + 1):
		graph[i] = []
	for i in range(m):
		(start, end) = map(int, input().split())
		graph[start].append(end)
		graph[end].append(start)
	print(Solve(graph, gold))
main()


(n, m) = map(int, input().split())
gold = [0] + list(map(int, input().split()))
pp = {str(i): set() for i in range(1, n + 1)}
vz = {str(i) for i in range(1, n + 1)}
for i in range(m):
	(x, y) = input().split()
	pp[x].add(y)
	pp[y].add(x)
nums = []
while vz:
	cur = vz.pop()
	min_gold = gold[int(cur)]
	lv1 = pp[cur]
	while lv1:
		lv2 = set()
		for np in lv1:
			vz.discard(np)
			min_gold = min(min_gold, gold[int(np)])
			lv2 |= pp[np]
		lv2 &= vz
		(lv1, lv2) = (lv2, lv1)
	nums.append(min_gold)
print(sum(nums))


(v, e) = list(map(int, input().strip().split()))
goldArray = list(map(int, input().strip().split()))
vertexSet = set()
edges = dict()
for i in range(e):
	pair = list(map(int, input().strip().split()))
	if pair[0] in edges:
		edges[pair[0]].append(pair[1])
	else:
		edges[pair[0]] = [pair[1]]
	if pair[1] in edges:
		edges[pair[1]].append(pair[0])
	else:
		edges[pair[1]] = [pair[0]]
minGold = 0
minSubGold = 0
rootVertex = 1
while rootVertex <= v:
	if rootVertex in vertexSet:
		rootVertex += 1
		continue
	minSubGold = goldArray[rootVertex - 1]
	if rootVertex in edges:
		stack = [rootVertex]
		while stack:
			node = stack.pop()
			nodeGold = goldArray[node - 1]
			if nodeGold < minSubGold:
				minSubGold = nodeGold
			vertexSet.add(node)
			if node in edges:
				for neighbour in edges[node]:
					if neighbour not in vertexSet:
						stack.append(neighbour)
	else:
		vertexSet.add(rootVertex)
	minGold += minSubGold
print(minGold)


(n, m) = list(map(int, input().split(' ')))
gold = list(map(int, input().split(' ')))
edges = []
for _ in range(m):
	edges.append(list(map(int, input().split(' '))))
graph = {}
for node in range(n):
	graph[node + 1] = []
for edge in edges:
	(a, b) = edge
	graph[a].append(b)
	graph[b].append(a)
res = 0
visited = [False for _ in range(n + 1)]
remaining = 1
while remaining < n + 1:
	if not visited[remaining]:
		next = [remaining]
		minimum = float('inf')
		while next != []:
			node = next.pop()
			visited[node] = True
			minimum = min(minimum, gold[node - 1])
			for neighbor in graph[node]:
				if not visited[neighbor]:
					next.append(neighbor)
		res += minimum
	remaining += 1
print(res)


(n, m) = map(int, input().split())
vs = [False for i in range(n)]
vc = list(map(int, input().split()))
v = [[] for i in range(n)]
while m > 0:
	(x, y) = map(int, input().split())
	v[x - 1].append(y - 1)
	v[y - 1].append(x - 1)
	m -= 1
ans = 0

def bfs(nod):
	q = []
	q.append(nod)
	vs[nod] = True
	mn = vc[nod]
	while len(q) > 0:
		node = q.pop()
		vs[node] = True
		mn = min(mn, vc[node])
		for ch in v[node]:
			if not vs[ch]:
				q.append(ch)
	return mn
for i in range(n):
	if vs[i]:
		continue
	ans += bfs(i)
print(ans)


import threading, sys
sys.setrecursionlimit(10 ** 9)
threading.stack_size(67108864)

def main():
	(n, m) = [int(x) for x in input().split()]
	c = [int(x) for x in input().split()]
	graph = {int(x) + 1: [] for x in range(n)}
	for i in range(m):
		(ff, sf) = [int(x) for x in input().split()]
		graph[ff].append(sf)
		graph[sf].append(ff)
	comps = {}
	cl = [-1] * n

	def dfs(v, c, p):
		cl[v - 1] = c
		if c not in comps:
			comps[c] = []
		comps[c].append(v)
		for i in graph[v]:
			if i != p:
				if cl[i - 1] == -1:
					dfs(i, c, v)
	cc = 1
	for i in range(n):
		if cl[i] == -1:
			dfs(i + 1, cc, -1)
			cc += 1
	price = 0
	for i in comps:
		minp = 999999999999999
		for j in comps[i]:
			minp = min(c[j - 1], minp)
		price += minp
	print(price)
thread = threading.Thread(target=main)
thread.start()


(n, m) = map(int, input().split())
c = list(map(int, input().split()))
(a, d, ans) = ([], [], 0)
for i in range(n):
	d.append([])
	a.append(0)

def dfs(i):
	(z, m) = ([i], c[i])
	while len(z) > 0:
		k = z.pop()
		if m > c[k]:
			m = c[k]
		a[k] = 1
		for p in d[k]:
			if a[p] == 0:
				z.append(p)
	return m
for i in range(m):
	(x, y) = map(int, input().split())
	d[x - 1].append(y - 1)
	d[y - 1].append(x - 1)
for i in range(n):
	if a[i] == 0:
		ans += dfs(i)
print(ans)


import bisect
import collections
import copy
import functools
import heapq
import itertools
import math
import random
import re
import sys
import time
import string
from typing import List
sys.setrecursionlimit(99999)
(n, m) = map(int, sys.stdin.readline().split())
c = list(map(int, sys.stdin.readline().split()))
vis = [0] * (n + 1)
g = collections.defaultdict(list)

def bfs(st):
	vis[st] = 1
	q = collections.deque()
	q.append(st)
	while q:
		cq = q.popleft()
		for nc in g[cq]:
			if vis[nc] == 0:
				vis[nc] = 1
				q.append(nc)
for _ in range(m):
	(u, v) = map(int, sys.stdin.readline().split())
	g[u].append(v)
	g[v].append(u)
tc = sorted(list(range(n)), key=lambda x: c[x])
ans = 0
for ck in tc:
	if vis[ck + 1] == 0:
		ans += c[ck]
		bfs(ck + 1)
print(ans)


from sys import stdin

def main():
	(n, m) = map(int, stdin.readline().split())
	ar = list(map(int, stdin.readline().split()))
	parent = [i for i in range(n)]
	min_cost = [ar[i] for i in range(n)]

	def get_parent(index):
		stack = []
		curr = index
		while curr != parent[curr]:
			stack.append(curr)
			curr = parent[curr]
		while stack:
			idx = stack.pop()
			parent[idx] = curr
		return parent[curr]

	def union(index1, index2):
		p1 = get_parent(index1)
		p2 = get_parent(index2)
		if p1 != p2:
			if p1 < p2:
				parent[p2] = p1
				min_cost[p1] = min(min_cost[p1], min_cost[p2])
			else:
				parent[p1] = p2
				min_cost[p2] = min(min_cost[p2], min_cost[p1])
	for _ in range(m):
		(v1, v2) = map(int, stdin.readline().split())
		union(v1 - 1, v2 - 1)
	check = [False] * n
	ans = 0
	for i in range(n):
		p = get_parent(i)
		if not check[p]:
			ans += min_cost[p]
			check[p] = True
	print(ans)
main()


(n, m) = map(int, input().split())
cost = list(map(int, input().split()))
cost.insert(0, 0)
tag = [[] for x in range(n + 1)]
queue = [0 for x in range(n + 1)]
for _ in range(m):
	(a, b) = map(int, input().split())
	tag[a].append(b)
	tag[b].append(a)
nextRound = []
temp_cost = []
ans = []
for i in range(1, n + 1):
	temp_cost = []
	if queue[i] == 0:
		nextRound.append(i)
		temp_cost.append(cost[i])
		queue[i] = 1
		while len(nextRound) != 0:
			f = nextRound.pop(0)
			for friend in tag[f]:
				if queue[friend] == 0:
					queue[friend] = 1
					temp_cost.append(cost[friend])
					nextRound.append(friend)
		ans.append(min(temp_cost))
print(sum(ans))


(n, m) = map(int, input().split())
l = [int(i) for i in input().split()]
if m == 0:
	print(sum(l))
	exit()

def union(parent, size, minim, a, b):
	x = find(parent, a)
	y = find(parent, b)
	if x != y:
		if size[x] < size[y]:
			parent[x] = y
			size[y] = size[x] + size[y]
			minim[y] = min(minim[x], minim[y])
		else:
			parent[y] = x
			size[x] = size[x] + size[y]
			minim[x] = min(minim[x], minim[y])
	return (parent, size, minim)

def find(parent, a):
	while a != parent[a]:
		a = parent[a]
	return a
parent = [i for i in range(n)]
size = [1] * n
minim = [l[i] for i in range(n)]
for i in range(m):
	(a, b) = map(int, input().split())
	a -= 1
	b -= 1
	(parent, size, minim) = union(parent, size, minim, a, b)
sm = 0
for i in range(n):
	if parent[i] == i:
		sm += minim[i]
print(sm)


(n, pairs) = list(map(int, input().split()))
c = list(map(int, input().split()))
c = [0] + [-i for i in c]

def par(i):
	if c[i] > 0:
		c[i] = par(c[i])
		return c[i]
	return i

def uni(a, b):
	para = par(a)
	parb = par(b)
	c[para] = max(c[para], c[parb])
	c[parb] = para
for i in range(pairs):
	(a, b) = list(map(int, input().split()))
	if par(a) != par(b):
		uni(a, b)
print(-sum(filter(lambda i: i < 0, c)))


from sys import stdin, stdout
import bisect

def ii():
	return int(stdin.readline())

def mi():
	return map(int, stdin.readline().split())

def li():
	return list(mi())

def si():
	return stdin.readline()

def find(a):
	temp = []
	while parent[a] > 0:
		temp.append(a)
		a = parent[a]
	for i in temp:
		parent[i] = a
	return a

def union(x, y):
	x = find(x)
	y = find(y)
	if x == y:
		return
	if gold[x] < gold[y]:
		parent[x] += parent[y]
		parent[y] = x
	else:
		parent[y] += parent[x]
		parent[x] = y
(n, m) = mi()
gold = [0]
parent = [-1] * (n + 1)
gold += li()
for i in range(m):
	(x, y) = mi()
	union(x, y)
ans = 0
for i in range(1, n + 1):
	if parent[i] < 0:
		ans += gold[i]
print(ans)


from collections import *
import bisect
import heapq

def ri():
	return int(input())

def rl():
	return list(map(int, input().split()))

def bfs(u):
	if visited[u]:
		return 0
	visited[u] = True
	to_visit = [u]
	gold = cc[u]
	while to_visit:
		v = to_visit.pop()
		for w in graph[v]:
			if not visited[w]:
				visited[w] = True
				to_visit.append(w)
				if cc[w] < gold:
					gold = cc[w]
	return gold
(n, m) = rl()
cc = rl()
graph = [[] for _ in range(n)]
for i in range(m):
	(u, v) = rl()
	u -= 1
	v -= 1
	graph[u].append(v)
	graph[v].append(u)
visited = [False] * n
price = 0
for u in range(n):
	price_component = bfs(u)
	price += price_component
print(price)


import sys
from collections import defaultdict

class Graph:

	def __init__(self):
		self.graph = defaultdict(list)

	def add(self, u, v):
		self.graph[u].append(v)
		self.graph[v].append(u)

	def dfs(self, v, vi, m):
		l1 = []
		l1.append(v)
		m = min(m, l[v - 1])
		while len(l1) > 0:
			c = l1.pop()
			if vi[c] == True:
				continue
			vi[c] = True
			m = min(m, l[c - 1])
			for i in self.graph[c]:
				l1.append(i)
		return m
g = Graph()
(n, m) = map(int, input().split())
l = list(map(int, input().split()))
for _ in range(m):
	k = input().split()
	g.add(int(k[0]), int(k[1]))
vi = [False] * (n + 1)
k = 0
for i in range(1, n + 1):
	if vi[i] == False:
		best = l[i - 1]
		k = k + g.dfs(i, vi, best)
print(k)


import math

def DFS(graph, source, visited):
	(stack, cost) = ([source], math.inf)
	while stack:
		node = stack.pop()
		cost = cost if cost < c[node] else c[node]
		if node not in visited:
			visited.add(node)
			stack.extend(graph[node] - visited)
	return cost
(n, m) = map(int, input().split())
G = {i: set() for i in range(1, n + 1)}
c = [0] + [int(cost) for cost in input().split()]
for edges in range(m):
	(v1, v2) = map(int, input().split())
	G[v1].add(v2)
	G[v2].add(v1)
ans = 0
v = set()
print(sum((DFS(G, next, v) for next in set(G.keys()) - v if next not in v)))


from __future__ import division, print_function
from sys import stdin, stdout
import itertools, os, sys, threading
from collections import deque, Counter, OrderedDict, defaultdict
import heapq
from math import ceil, floor, log, sqrt, factorial, pow, pi, gcd
from fractions import Fraction

def ii():
	return int(input())

def si():
	return str(input())

def mi():
	return map(int, input().split())

def li():
	return list(mi())

def fii():
	return int(stdin.readline())

def fsi():
	return str(stdin.readline())

def fmi():
	return map(int, stdin.readline().split())

def fli():
	return list(fmi())
abc = 'abcdefghijklmnopqrstuvwxyz'
abd = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod = 1000000007
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def getKey(item):
	return item[0]

def sort2(l):
	return sorted(l, key=getKey)

def d2(n, m, num):
	return [[num for x in range(m)] for y in range(n)]

def isPowerOfTwo(x):
	return x and (not x & x - 1)

def decimalToBinary(n):
	return bin(n).replace('0b', '')

def ntl(n):
	return [int(i) for i in str(n)]

def powerMod(x, y, p):
	res = 1
	x %= p
	while y > 0:
		if y & 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x
graph = defaultdict(list)
visited = [0] * 1000000
col = [-1] * 1000000

def dfs(v, c):
	if visited[v]:
		if col[v] != c:
			print('-1')
			exit()
		return
	col[v] = c
	visited[v] = 1
	for i in graph[v]:
		dfs(i, c ^ 1)

def bfs(d, v):
	q = []
	q.append(v)
	visited[v] = 1
	while len(q) != 0:
		x = q[0]
		q.pop(0)
		for i in d[x]:
			if visited[i] != 1:
				visited[i] = 1
				q.append(i)
		print(x)

def make_graph(e):
	d = {}
	for i in range(e):
		(x, y) = mi()
		if x not in d.keys():
			d[x] = [y]
		else:
			d[x].append(y)
		if y not in d.keys():
			d[y] = [x]
		else:
			d[y].append(x)
	return d

def gr2(n):
	d = {}
	for i in range(n):
		(x, y) = mi()
		if x not in d.keys():
			d[x] = [y]
		else:
			d[x].append(y)
	return d

def connected_components(graph):
	seen = set()

	def dfs(v):
		vs = set([v])
		component = []
		while vs:
			v = vs.pop()
			seen.add(v)
			vs |= set(graph[v]) - seen
			component.append(v)
		return component
	ans = []
	for v in graph:
		if v not in seen:
			d = dfs(v)
			ans.append(d)
	return ans

def primeFactors(n):
	s = set()
	while n % 2 == 0:
		s.add(2)
		n = n // 2
	for i in range(3, int(sqrt(n)) + 1, 2):
		while n % i == 0:
			s.add(i)
			n = n // i
	if n > 2:
		s.add(n)
	return s

def find_all(a_str, sub):
	start = 0
	while True:
		start = a_str.find(sub, start)
		if start == -1:
			return
		yield start
		start += len(sub)
(n, m) = mi()
l = li()
vis = [0] * n
d = make_graph(m)
a = connected_components(d)
sum = 0
for i in a:
	min = sys.maxsize
	for j in range(len(i)):
		if l[i[j] - 1] < min:
			min = l[i[j] - 1]
		vis[i[j] - 1] = 1
	sum += min
for i in range(n):
	if vis[i] == 0:
		sum += l[i]
print(sum)


import sys
input = sys.stdin.readline
(n, m) = map(int, input().split())
cost = list(map(int, input().split()))
adj = [[] for _ in range(n)]
for (u, v) in (map(int, input().split()) for _ in range(m)):
	adj[u - 1].append(v - 1)
	adj[v - 1].append(u - 1)
ans = 0
visited = [0] * n
for i in range(n):
	if visited[i]:
		continue
	visited[i] = 1
	c = cost[i]
	stack = [i]
	while stack:
		v = stack.pop()
		for dest in adj[v]:
			if visited[dest]:
				continue
			visited[dest] = 1
			if c > cost[dest]:
				c = cost[dest]
			stack.append(dest)
	ans += c
print(ans)


def dfs(graph, start):
	(visited, stack) = (set(), [start])
	while stack:
		vertex = stack.pop()
		if vertex not in visited:
			visited.add(vertex)
			stack.extend(graph[vertex] - visited)
	return (visited, min([prices[v] for v in visited]))
(n, m) = list(map(int, input().split()))
prices = [0] + list(map(int, input().split()))
friendship = {x: set() for x in range(1, n + 1)}
for _ in range(m):
	(x, y) = list(map(int, input().split()))
	friendship[x].add(y)
	friendship[y].add(x)
citizens = set(range(1, n + 1))
ans = 0
while citizens:
	v = citizens.pop()
	(visited, p) = dfs(friendship, v)
	citizens.difference_update(visited)
	ans += p
print(ans)


(n, m) = map(int, input().strip().split())
arr = list(map(int, input().strip().split()))
gr = [[] for i in range(n)]

def dfs():
	stack = []
	par = {}
	ans = []
	for i in range(n):
		if i + 1 not in par:
			par[i + 1] = 0
			stack.append(i + 1)
			temp = []
			while stack:
				u = stack.pop()
				temp.append(arr[u - 1])
				for v in gr[u - 1]:
					if v not in par:
						stack.append(v)
						par[v] = u
			ans.append(min(temp))
	print(sum(ans))
for i in range(m):
	(x, y) = map(int, input().strip().split())
	gr[x - 1].append(y)
	gr[y - 1].append(x)
dfs()


from collections import deque

class Person:

	def __init__(self):
		self.friends = []
		self.taken = False
		self.price = 0

def fill(person):
	queue = deque([])
	queue.append(person)
	minimum = person.price
	while queue:
		p = queue.popleft()
		p.taken = True
		minimum = min(minimum, p.price)
		for f in p.friends:
			if not f.taken:
				queue.append(f)
	return minimum
(N, M) = map(int, input().split())
people = [Person() for _ in range(0, N)]
prices = list(map(int, input().split()))
for i in range(0, N):
	people[i].price = prices[i]
for _ in range(0, M):
	(u, v) = map(int, input().split())
	people[u - 1].friends.append(people[v - 1])
	people[v - 1].friends.append(people[u - 1])
cost = 0
for p in people:
	if not p.taken:
		cost += fill(p)
print(cost)


import sys
WHITE = 0
GREY = 1
BLACK = 2

class Gr:

	def __init__(self):
		self.tops = {}

	def add_top(self, n, val):
		self.tops[n] = {'val': val, 'rel': [], 'color': WHITE}

	def add_rel(self, a, b):
		if b not in self.tops[a]['rel']:
			self.tops[a]['rel'].append(b)
		if a not in self.tops[b]['rel']:
			self.tops[b]['rel'].append(a)

	def add_rel_n(self, a, b):
		self.tops[a]['rel'].append(b)
		self.tops[b]['rel'].append(a)

def dfs(t, g):
	m = g[t]['val']
	g[t]['color'] = GREY
	for n in g[t]['rel']:
		if g[n]['color'] == WHITE:
			m = min(m, dfs(n, g))
	g[t]['color'] = BLACK
	return m

def dfs_m(g):
	sum = 0
	for top in g.tops:
		if g.tops[top]['color'] == WHITE:
			sum += dfs(top, g.tops)
	return sum

def dfs1(t):
	total = 0
	for top in t:
		if t[top]['color'] == WHITE:
			stack = []
			stack.append([top, 0])
			sum = t[top]['val']
			while len(stack) > 0:
				(cur_top, child_index) = stack.pop()
				if t[cur_top]['color'] != WHITE:
					sum = min(sum, t[cur_top]['val'])
					continue
				else:
					for i in range(child_index, len(t[cur_top]['rel'])):
						child_top = t[t[cur_top]['rel'][i]]
						ci = t[cur_top]['rel'][i]
						if child_top['color'] == WHITE:
							stack.append([cur_top, i])
							stack.append([ci, 0])
							continue
					t[cur_top]['color'] = GREY
					sum = min(sum, t[cur_top]['val'])
			total += sum
	return total
test = False
if test:
	import random
	g = Gr()
	(n, m) = (100000, 99999)
	v = [random.randint(1, m) for i in range(m)]
	j = 1
	for i in v:
		g.add_top(j, i)
		j += 1
	for r in range(m):
		(a, b) = (random.randint(1, m), random.randint(1, m))
		g.add_rel(a, b)
	f = dfs1(g.tops)
	print('-----------------')
	print(f)
else:
	g = Gr()
	(n, m) = list(map(int, sys.stdin.readline().split()))
	v = list(map(int, sys.stdin.readline().split()))
	j = 1
	for i in v:
		g.add_top(j, i)
		j += 1
	for r in range(m):
		(a, b) = list(map(int, sys.stdin.readline().split()))
		g.add_rel_n(a, b)
	f = dfs1(g.tops)
	print(f)


from collections import defaultdict

class Graph:

	def __init__(self):
		self.graph = defaultdict(list)

	def add_Edge(self, u, v):
		self.graph[u].append(v)
		self.graph[v].append(u)

	def BFS(self, start, visited):
		s = [start]
		d = []
		while s:
			node = s.pop(0)
			if visited[node] == False:
				d.append(node)
				visited[node] = True
				for i in self.graph[node]:
					if visited[i] == False:
						s.append(i)
		return d
(n, m) = map(int, input().split())
r = list(map(int, input().split()))
g = Graph()
for i in range(m):
	(a, b) = map(int, input().split())
	g.add_Edge(a - 1, b - 1)
p = []
visited = [False] * n
for i in range(n):
	w = g.BFS(i, visited)
	if len(w) != 0:
		p.append(w)
ans = 0
for i in range(len(p)):
	ans += min(list(map(lambda x: r[x], p[i])))
print(ans)


from collections import deque

def bfs(i, friends, visited, cost):
	d = deque()
	d.append(i)
	amount = cost[i]
	while d:
		k = d.popleft()
		if not visited[k]:
			amount = min(amount, cost[k])
			visited[k] = True
			for i in friends[k]:
				d.append(i)
	return amount

def main():
	(n, m) = map(int, input().split())
	c = list(map(int, input().split()))
	friends = [[] for i in range(n)]
	cost = {}
	amount = 0
	for i in range(0, n):
		cost[i] = c[i]
	for i in range(m):
		(x, y) = map(int, input().split())
		friends[x - 1].append(y - 1)
		friends[y - 1].append(x - 1)
	visited = [False] * n
	for i in range(n):
		if not visited[i]:
			amount += bfs(i, friends, visited, cost)
	print(amount)
main()


import sys, threading
sys.setrecursionlimit(100010)
threading.stack_size(67108864)

def main():
	(n, m) = list(map(int, input().split()))
	c = list(map(int, input().split()))
	edge = [[] for i in range(n)]
	for i in range(m):
		(u, v) = list(map(int, input().split()))
		u -= 1
		v -= 1
		edge[u].append(v)
		edge[v].append(u)
	flag = [0] * n

	def dfs(u, k):
		flag[u] = k
		for v in edge[u]:
			if flag[v] == 0:
				dfs(v, k)
	cnt = 0
	for u in range(n):
		if flag[u] == 0:
			cnt += 1
			dfs(u, cnt)
	mn = [1061109567] * (cnt + 1)
	for u in range(n):
		mn[flag[u]] = min(mn[flag[u]], c[u])
	print(sum(mn[1:]))
thread = threading.Thread(target=main)
thread.start()


import sys
input = sys.stdin.readline
from math import log2, log, ceil

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def nCr(n, k):
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return res

def upper_bound(a, x, lo=0):
	hi = len(a)
	while lo < hi:
		mid = (lo + hi) // 2
		if a[mid] < x:
			lo = mid + 1
		else:
			hi = mid
	return lo

def primefs(n):
	primes = {}
	while n % 2 == 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	while x != link[x]:
		x = link[x]
	return x

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime
MAXN = int(1000000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i

def factoriazation(x):
	ret = {}
	while x != 1:
		ret[spf[x]] = ret.get(spf[x], 0) + 1
		x = x // spf[x]
	return ret

def int_array():
	return list(map(int, input().strip().split()))

def str_array():
	return input().strip().split()
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
(n, m) = int_array()
a = int_array()
link = [i for i in range(n)]
size = [1] * n
for _ in range(m):
	(x, y) = int_array()
	union(x - 1, y - 1, link, size)
comps = [find(i, link) for i in link]
dick = {}
for i in range(n):
	if comps[i] in dick:
		dick[comps[i]].append(i)
	else:
		dick[comps[i]] = [i]
for i in dick:
	for j in range(len(dick[i])):
		dick[i][j] = a[dick[i][j]]
ans = 0
for i in dick:
	ans += min(dick[i])
print(ans)


def root(node):
	if node == parent[node]:
		return node
	parent[node] = root(parent[node])
	return parent[node]

def union(a, b):
	root_a = root(a)
	root_b = root(b)
	if root_a != root_b:
		if rank[root_a] < rank[root_b]:
			(root_a, root_b) = (root_b, root_a)
		parent[root_b] = root_a
		if rank[root_a] == rank[root_b]:
			rank[root_a] += 1

def find(a, b):
	if root(a) == root(b):
		return True
	else:
		return False
(V, E) = map(int, input().strip().split(' '))
gold = list(map(int, input().strip().split(' ')))
parent = [i for i in range(V + 1)]
rank = [0 for i in range(V + 1)]
for i in range(E):
	(u, v) = map(int, input().strip().split(' '))
	union(u, v)
for i in range(1, V + 1):
	parent[i] = root(parent[i])
from collections import defaultdict as dd
d = dd(lambda : 10000000000)
for i in range(1, V + 1):
	d[parent[i]] = min(d[parent[i]], gold[i - 1])
print(sum(d.values()))


from collections import defaultdict
(n, m) = map(int, input().split())
c = list(map(int, input().split()))
d = defaultdict(list)

def DFS(d, visited, x, c):
	z = float('inf')
	stack = [x]
	while len(stack):
		temp = stack.pop()
		visited[temp] = 1
		z = min(z, c[temp - 1])
		for j in d[temp]:
			if visited[j] == 0:
				stack.append(j)
	return z
for i in range(m):
	(x, y) = map(int, input().split())
	d[x].append(y)
	d[y].append(x)
visited = [0 for i in range(n + 1)]
ans = 0
for i in range(1, n + 1):
	if visited[i] == 0:
		ans = ans + DFS(d, visited, i, c)
print(ans)


(n, m) = map(int, input().split())
costs = [int(x) for x in input().split()]
disjointSet = [-1 for x in costs]

def root(x):
	if disjointSet[x] >= 0:
		disjointSet[x] = root(disjointSet[x])
		return disjointSet[x]
	return x
for _ in range(m):
	(a, b) = map(int, input().split())
	a -= 1
	b -= 1
	(r1, r2) = (root(a), root(b))
	if r1 != r2:
		disjointSet[r2] = r1
		costs[r1] = min(costs[r1], costs[r2])
		costs[r2] = 0
print(sum(costs))


(N, M) = list(map(int, input().strip().split()))
visited = [0] * (N + 1)
gold = [0]
gold_vals = list(map(int, input().strip().split()))
groups = []
for i in gold_vals:
	gold.append(i)
edges = {}
for i in range(1, N + 1):
	edges[i] = []
for _ in range(M):
	(f, l) = list(map(int, input().strip().split()))
	edges[f].append(l)
	edges[l].append(f)
iter_visited = 1
group_no = -1
while iter_visited < len(visited):
	if visited[iter_visited] == 1:
		iter_visited += 1
		continue
	groups.append([])
	group_no += 1
	stack = [iter_visited]
	while len(stack) > 0:
		node = stack.pop()
		visited[node] = 1
		groups[group_no].append(gold[node])
		for other_node in edges[node]:
			if visited[other_node] == 0:
				stack.append(other_node)
to_spend = 0
for group in groups:
	to_spend += min(group)
print(to_spend)


from collections import defaultdict

class graph:

	def __init__(self, v):
		self.v = v
		self.graph = defaultdict(list)

	def insert(self, a, b):
		self.graph[a].append(b)
		self.graph[b].append(a)

	def connectedwr(self, w):
		visit = [0] * self.v
		stack = []
		ct = []
		for i in range(self.v):
			stack.append(i)
			l = []
			while stack:
				x = stack.pop()
				if visit[x] == 0:
					l.append(w[x])
					visit[x] = 1
				for i in self.graph[x]:
					if visit[i] == 0:
						stack.append(i)
			if l:
				ct.append(min(l))
		return sum(ct)
(n, m) = map(int, input().split())
g = graph(n)
w = list(map(int, input().split()))
for i in range(m):
	(x, y) = map(int, input().split())
	g.insert(x - 1, y - 1)
print(g.connectedwr(w))


from collections import defaultdict

def dfs(start, edges, path=set()):
	q = [start]
	while q:
		v = q.pop()
		if v not in path:
			path.add(v)
			for e in edges[v]:
				q.append(e)
	return path

def main():
	(n, m) = map(int, input().split())
	gold = [int(c) for c in input().split()]
	if m == 0:
		res = sum(gold)
		print(res)
		return
	edges = defaultdict(list)
	for _ in range(m):
		(a, b) = map(int, input().split())
		edges[a - 1].append(b - 1)
		edges[b - 1].append(a - 1)
	nodes2visit = set(range(n))
	res = 0
	while nodes2visit:
		nodes2remove = dfs(next(iter(nodes2visit)), edges, set())
		res += min((gold[i] for i in nodes2remove))
		nodes2visit -= nodes2remove
	print(res)
main()


import math

def dfscon(gr, cost, n):
	visited = [0] * n
	ans = 0
	for i in range(n):
		stack = [i]
		co = math.inf
		while stack:
			p = stack.pop()
			if visited[p] == 0:
				if cost[p] < co:
					co = cost[p]
				visited[p] = 1
			for yu in gr[p]:
				if visited[yu] == 0:
					stack.append(yu)
		if co != math.inf:
			ans = ans + co
	return ans
from collections import defaultdict
(n, t) = map(int, input().split())
l = list(map(int, input().split()))
gr = defaultdict(list)
for u in range(t):
	(a, b) = map(int, input().split())
	a = a - 1
	b = b - 1
	gr[a].append(b)
	gr[b].append(a)
ans = dfscon(gr, l, n)
print(ans)


(n, m) = map(int, input().split())
cs = [0] + list(map(int, input().split()))
p = [0] + list(range(1, n + 1))

def find(x):
	while p[x] != x:
		p[x] = p[p[x]]
		x = p[x]
	return x
for _ in range(m):
	(x, y) = map(find, map(int, input().split()))
	if cs[x] < cs[y]:
		p[y] = x
	else:
		p[x] = y
ans = 0
for i in range(n + 1):
	if p[i] == i:
		ans += cs[i]
print(ans)


def bfs(i, lst, d, mark, res):
	queue = [i]
	mn = lst[i]
	while queue:
		q = queue.pop(0)
		if lst[q] < mn:
			mn = lst[q]
		for (i, x) in enumerate(d[q]):
			if mark[x] == False:
				mark[x] = True
				queue.append(x)
	res += mn
	return res

def prog():
	from sys import stdin
	(n, m) = map(int, stdin.readline().split())
	lst = {i: int(x) for (i, x) in enumerate(stdin.readline().split())}
	d = {}
	for i in range(m):
		(x, y) = map(int, stdin.readline().split())
		(x, y) = (x - 1, y - 1)
		if d.get(x) == None:
			d[x] = []
		if d.get(y) == None:
			d[y] = []
		d[x].append(y)
		d[y].append(x)
	mark = {i: False for i in range(n)}
	res = 0
	for i in range(n):
		if d.get(i) == None:
			res += lst[i]
			mark[i] = True
		if mark[i] == False:
			mark[i] = True
			res = bfs(i, lst, d, mark, res)
	print(res)
prog()


from collections import Counter
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import sys

def dfs(i, arr, v, s):
	sep = []
	sep.append(i)
	while len(sep) > 0:
		var = sep.pop()
		if v[var] == False:
			v[var] = True
			s.append(var)
			for c in g[var]:
				sep.append(c)
(n, m) = map(int, input().split())
arr = list(map(int, input().split()))
g = [[] for i in range(100003)]
for i in range(m):
	(u, v) = map(int, input().split())
	g[u].append(v)
	g[v].append(u)
ans = []
v = [False] * 100003
for i in range(1, n + 1):
	s = []
	if v[i] == False:
		dfs(i, arr, v, s)
	if len(s) > 0:
		ans.append(s)
sum = 0
for i in ans:
	x = 10 ** 9
	for j in i:
		x = min(x, arr[j - 1])
	sum += x
print(sum)


from collections import defaultdict
import sys

def dfs(i, ans):
	stack = []
	m = 10 ** 18
	stack.append(i)
	while stack:
		v = stack.pop()
		if visited[v]:
			continue
		visited[v] = True
		m = min(m, coin[v - 1])
		for u in d[v]:
			if not visited[u]:
				stack.append(u)
	return m
input = sys.stdin.buffer.readline
(n, m) = map(int, input().split())
coin = list(map(int, input().split()))
d = defaultdict(list)
for _ in range(m):
	(u, v) = map(int, input().split())
	d[u].append(v)
	d[v].append(u)
visited = [False for i in range(n + 1)]
ans = 0
for i in range(1, n + 1):
	if not visited[i]:
		ans += dfs(i, ans)
print(ans)


(n, m) = input().split(' ')
(n, m) = (int(n), int(m))
costs = [int(x) for x in input().split(' ')]
adj_list = [[] for _ in range(n)]
for i in range(m):
	(a, b) = input().split(' ')
	(a, b) = (int(a), int(b))
	a -= 1
	b -= 1
	adj_list[a].append(b)
	adj_list[b].append(a)
visited = [False for _ in range(n)]

def run_dfs(start):
	stack = []
	stack.append(start)
	lowest_cost = costs[start]
	while len(stack) > 0:
		cur_node = stack.pop()
		if visited[cur_node]:
			continue
		visited[cur_node] = True
		lowest_cost = min(lowest_cost, costs[cur_node])
		for neighbor in adj_list[cur_node]:
			stack.append(neighbor)
	return lowest_cost
total_cost = 0
for i in range(n):
	if not visited[i]:
		total_cost += run_dfs(i)
print(total_cost)


from collections import deque
(n, m) = map(int, input().split())
costs = list(map(int, input().split()))
linked_to = [[] for i in range(n)]
visited = [False] * n
for i in range(m):
	(x, y) = map(lambda x: int(x) - 1, input().split())
	linked_to[x].append(y)
	linked_to[y].append(x)
to_visit = deque()
sm = 0
for (i, v) in enumerate(visited):
	if not v:
		csm = costs[i]
		to_visit.append(i)
		while len(to_visit) > 0:
			c = to_visit.pop()
			csm = min(csm, costs[c])
			visited[c] = True
			for l in linked_to[c]:
				if not visited[l]:
					to_visit.append(l)
		sm += csm
print(sm)


(n, m) = map(int, input().split())
cost = list(map(int, input().split()))
g = {i: [] for i in range(n)}
for _ in range(m):
	(u, v) = map(int, input().split())
	g[u - 1].append(v - 1)
	g[v - 1].append(u - 1)
visited = [False] * n
total = 0
stack = []
for i in range(n):
	if not visited[i]:
		stack = [i]
		min_cost = cost[i]
		while stack:
			u = stack.pop()
			visited[u] = True
			min_cost = min(min_cost, cost[u])
			for v in g[u]:
				if not visited[v]:
					stack.append(v)
		total += min_cost
print(total)


from collections import defaultdict
I = lambda : map(int, input().split())
(n, m) = I()
C = list(I())
G = defaultdict(set)
for _ in range(m):
	(x, y) = I()
	G[x].add(y)
	G[y].add(x)
(visited, groups) = (set(), [])
for u in range(1, n + 1):
	if u not in visited:
		(stack, group) = ([u], [])
		while stack:
			u = stack.pop()
			visited.add(u)
			group.append(u)
			for v in G[u]:
				if v not in visited:
					stack.append(v)
		groups.append(group)
print(sum((min((C[u - 1] for u in group)) for group in groups)))


from collections import defaultdict

class het:

	def __init__(self):
		self.t = defaultdict(list)

	def ae(self, u, v):
		self.t[u].append(v)
		self.t[v].append(u)

	def cks(self, v, visited, best):
		s = []
		s.append(v)
		best = min(best, l[v - 1])
		while len(s) > 0:
			c = s.pop()
			if vi[c] == True:
				continue
			vi[c] = True
			best = min(best, l[c - 1])
			for i in self.t[c]:
				s.append(i)
		return best
g = het()
(n, m) = map(int, input().split())
l = list(map(int, input().split()))
for i in range(m):
	(x, y) = map(int, input().split())
	g.ae(x, y)
k = 0
vi = [False] * (n + 1)
for i in range(1, n + 1):
	if vi[i] == False:
		best = l[i - 1]
		k = k + g.cks(i, vi, best)
print(k)


import os
import sys

def log(*args, **kwargs):
	if os.environ.get('CODEFR'):
		print(*args, **kwargs)
(n, m) = tuple(map(int, input().split()))
a = list(map(int, input().split()))

class DSU:

	def __init__(self, n, cost=None, offset=1):
		self.parent = list(range(n + offset))
		self.size = [1] * (n + offset)
		self.cost = [0] * offset + (cost or [0] * n)

	def find_set(self, a):
		if a == self.parent[a]:
			return a
		self.parent[a] = self.find_set(self.parent[a])
		return self.parent[a]

	def union_sets(self, a, b):
		ap = self.find_set(a)
		bp = self.find_set(b)
		if ap != bp:
			if self.size[ap] < self.size[bp]:
				self.parent[ap] = bp
				self.parent[a] = bp
				self.size[bp] += self.size[ap]
				self.size[ap] -= self.size[a]
				self.cost[bp] = min(self.cost[ap], self.cost[bp])
			else:
				self.parent[bp] = ap
				self.parent[b] = ap
				self.size[ap] += self.size[bp]
				self.size[bp] -= self.size[b]
				self.cost[ap] = min(self.cost[bp], self.cost[ap])
dsu = DSU(n, a)
for i in range(m):
	dsu.union_sets(*tuple(map(int, input().split())))
res = 0
for i in range(1, n + 1):
	if dsu.parent[i] == i:
		res += dsu.cost[i]
print(res)


padres = []

def init(number):
	for _ in range(number + 1):
		padres.append(_)

def findP(g):
	while g != padres[g]:
		padres[g] = padres[padres[g]]
		g = padres[g]
	return g

def unir(h, i):
	ph = findP(h)
	pi = findP(i)
	if cost[ph - 1] > cost[pi - 1]:
		padres[ph] = pi
	else:
		padres[pi] = ph
n = input().split(' ')
num = input().split(' ')
cost = list(map(int, num))
(a, b) = n
a = int(a)
b = int(b)
init(a)
for _ in range(b):
	x = input().split(' ')
	(o, p) = x
	o = int(o)
	p = int(p)
	unir(o, p)
cont = 0
for _ in range(1, a + 1):
	if _ == findP(_):
		cont += cost[_ - 1]
print(cont)


def bfs(graph, i):
	min = 90000000000000000000
	queue = [i]
	while queue:
		i = queue.pop(0)
		if i not in graph:
			continue
		(k, price) = graph[i].pop(0)
		if price < min:
			min = price
		for item in graph[i]:
			queue.append(item)
			graph[item].remove(i)
		del graph[i]
	return min
(n, p) = [int(i) for i in input().split()]
graph = {}
index = 1
s = 0
for i in input().split():
	graph[index] = [(0, int(i))]
	index += 1
for _ in range(p):
	(x, y) = [int(i) for i in input().split()]
	graph[x].append(y)
	graph[y].append(x)
for i in range(1, n + 1):
	min = 0
	if i in graph:
		min = bfs(graph, i)
	s += min
print(s)


class Person:
	relations: [int]

	def __init__(self, price, index):
		self.index = index
		self.price = price
		self.relations = []

def dfs(persons):
	queue = persons[:]
	visited = set()
	cost = 0
	while queue:
		s: Person = queue.pop(0)
		if s.index not in visited:
			stack = s.relations
			visited.add(s.index)
			min_price = s.price
			while stack:
				element = stack.pop()
				if element not in visited:
					visited.add(element)
					p: Person = persons[element]
					min_price = min(min_price, p.price)
					stack += p.relations
			cost += min_price
	print(cost)
(m, n) = [int(x) for x in input().split()]
prices = [int(x) for x in input().split()]
pairs = []
for i in range(n):
	(p1, p2) = [int(x) for x in input().split()]
	pairs.append((p1, p2))
persons = []
for i in range(m):
	persons.append(Person(prices[i], i))
for (p1, p2) in pairs:
	persons[p1 - 1].relations.append(p2 - 1)
	persons[p2 - 1].relations.append(p1 - 1)
dfs(persons)


(nodes_nr, edges_nr) = (int(x) for x in input().split())
node_idx___cost = [int(x) for x in input().split()]
node_idx___neigh_idxes = [[] for x in range(nodes_nr)]
for _ in range(edges_nr):
	(node1_idx, node2_idx) = (int(x) - 1 for x in input().split())
	node_idx___neigh_idxes[node1_idx].append(node2_idx)
	node_idx___neigh_idxes[node2_idx].append(node1_idx)
node_idx___is_visited = [False for x in range(nodes_nr)]
ans = 0
for node_idx in range(nodes_nr):
	if node_idx___is_visited[node_idx]:
		continue
	stack = [node_idx]
	node_idx___is_visited[node_idx] = True
	cheapest_cost = node_idx___cost[node_idx]
	while stack:
		curr_node_idx = stack.pop()
		for neigh_idx in node_idx___neigh_idxes[curr_node_idx]:
			if node_idx___is_visited[neigh_idx]:
				continue
			stack.append(neigh_idx)
			cheapest_cost = min(cheapest_cost, node_idx___cost[neigh_idx])
			node_idx___is_visited[neigh_idx] = True
	ans += cheapest_cost
print(ans)


(n, q) = [int(i) for i in input().split()]
l = [int(i) for i in input().split()]
frie = [i for i in range(n)]

def find(a):
	while frie[a] != a:
		frie[a] = frie[frie[a]]
		a = frie[a]
	return a
for i in range(q):
	s = [int(i) for i in input().split()]
	(x, y) = (s[0] - 1, s[1] - 1)
	(x, y) = (find(x), find(y))
	if l[x] > l[y]:
		(x, y) = (y, x)
	frie[y] = x
visited = [False for i in range(n)]
print(sum((l[frie[j]] for j in range(n) if frie[j] == j)))


from collections import deque
(a, b) = map(int, input().split())
m = list(map(int, input().split()))
visited = [False] * a
d = []
for i in range(a):
	d.append(deque())
for i in range(b):
	(e, f) = map(int, input().split())
	d[e - 1].append(f - 1)
	d[f - 1].append(e - 1)
r = 0
for i in range(a):
	mn = 100000000009
	if not visited[i]:
		visited[i] = True
		if mn > m[i]:
			mn = m[i]
		while d[i]:
			k = d[i].popleft()
			if not visited[k]:
				visited[k] = True
				for h in d[k]:
					d[i].append(h)
				if mn > m[k]:
					mn = m[k]
		r += mn
print(r)


def findd(i):
	while i != b[i]:
		b[i] = b[b[i]]
		i = b[i]
	return i
(n, m) = map(int, input().split())
b = [i for i in range(n)]
a = [int(x) for x in input().split()]
for i in range(m):
	(x, y) = map(int, input().split())
	x = findd(x - 1)
	y = findd(y - 1)
	if a[x] > a[y]:
		b[x] = y
	else:
		b[y] = x
s = 0
for i in range(n):
	if i == b[i]:
		s += a[i]
print(s)


import sys
from collections import defaultdict
graph = defaultdict(list)
(n, m) = list(map(int, sys.stdin.readline().lstrip().rstrip().split()))
arr = list(map(int, sys.stdin.readline().lstrip().rstrip().split()))
for i in range(m):
	(u, v) = list(map(int, sys.stdin.readline().lstrip().rstrip().split()))
	u -= 1
	v -= 1
	graph[u].append(v)
	graph[v].append(u)
visited = [False for i in range(n)]
ans = 0
for i in range(n):
	if visited[i] == False:
		q = [i]
		visited[i] = True
		t = 10 ** 10
		while q != []:
			u = q[0]
			q.pop(0)
			t = min(t, arr[u])
			for j in graph[u]:
				if visited[j] == False:
					visited[j] = True
					q.append(j)
		ans += t
sys.stdout.write(str(ans) + '\n')


import string
import math
import collections

def bfs(graph, start, v, tab):
	Q = collections.deque([start])
	mi = 10 ** 9 + 1
	while len(Q) != 0:
		cur = Q[0]
		Q.popleft()
		v[cur] = 1
		mi = min(mi, tab[cur])
		for nei in graph[cur]:
			if not v[nei]:
				Q.append(nei)
	return mi

def main():
	(n, m) = map(int, input().split())
	tab = list(map(int, input().split()))
	graph = [[] for x in range(n)]
	v = [0] * n
	s = 0
	for x in range(m):
		(a, b) = map(int, input().split())
		a -= 1
		b -= 1
		graph[a].append(b)
		graph[b].append(a)
	for x in range(n):
		if not v[x]:
			s += bfs(graph, x, v, tab)
	print(s)
main()


(n, m) = map(int, input().split())
a = [0] + list(map(int, input().split()))
fa = [0] + [i + 1 for i in range(n)]

def findFa(x):
	s = []
	while fa[x] != x:
		s.append(x)
		x = fa[x]
	for v in s:
		fa[v] = x
	return x
while m:
	(u, v) = map(int, input().split())
	u = findFa(u)
	v = findFa(v)
	if u != v:
		fa[v] = u
		a[u] = min(a[u], a[v])
	m -= 1
res = 0
for i in range(1, n + 1):
	if findFa(i) == i:
		res += a[fa[i]]
print(res)


def DFS(graph, visited, m, s):
	stack = []
	stack.append(s)
	mn = m[s]
	while len(stack):
		s = stack[-1]
		stack.pop()
		if not visited[s]:
			visited[s] = True
			if m[s] < mn:
				mn = m[s]
		for node in graph[s]:
			if not visited[node]:
				stack.append(node)
	return mn
(v, e) = map(int, input().split())
g = []
for r in range(v):
	g.append([])
gold = list(map(int, input().split()))
for i in range(e):
	(joy, toy) = map(int, input().split())
	g[joy - 1].append(toy - 1)
	g[toy - 1].append(joy - 1)
ans = 0
visited = [False for i in range(v)]
for k in range(v):
	if visited[k] == False:
		ans += DFS(g, visited, gold, k)
print(ans)


from sys import stdin, stdout
from math import gcd, ceil, sqrt
ii1 = lambda : int(stdin.readline().strip())
is1 = lambda : stdin.readline().strip()
iia = lambda : list(map(int, stdin.readline().strip().split()))
isa = lambda : stdin.readline().strip().split()
mod = 1000000007
d = {}

def bfs(num, visit):
	global d, p
	queue = [num]
	cost = float('inf')
	while len(queue):
		cur = queue.pop(0)
		visit[cur] = 1
		cost = min(cost, p[cur - 1])
		if cur in d:
			for i in d[cur]:
				if not visit[i]:
					queue.append(i)
	return cost
(n, m) = iia()
p = iia()
for i in range(m):
	(a, b) = iia()
	d.setdefault(a, []).append(b)
	d.setdefault(b, []).append(a)
visited = [0] * (n + 1)
cost = 0
for i in range(1, n + 1):
	if not visited[i]:
		cost += bfs(i, visited)
print(cost)


def dfs(node):
	stack = [node]
	mn = int(100000000000.0)
	while stack:
		ele = stack.pop()
		if seen[ele] == 0:
			seen[ele] = 1
			mn = min(mn, arr[ele - 1])
			for each in adj[ele]:
				stack.append(each)
	return mn
(n, m) = map(int, input().split())
arr = list(map(int, input().split()))
adj = [list() for i in range(n + 1)]
seen = [0] * (n + 1)
for _ in range(m):
	(a, b) = map(int, input().split())
	adj[a].append(b)
	adj[b].append(a)
ans = 0
for i in range(1, n + 1):
	if seen[i] == 0:
		ans += dfs(i)
print(ans)


def answer(n, m, A, g):
	cost = 0
	vis = [0] * (n + 1)
	for i in range(1, n + 1):
		if vis[i] == 0:
			mini = A[i - 1]
			s = [i]
			vis[i] = 1
			while s:
				item = s.pop()
				mini = min(mini, A[item - 1])
				for node in g[item]:
					if vis[node] == 0:
						s.append(node)
						vis[node] = 1
			cost += mini
	return cost
(n, m) = map(int, input().split())
A = list(map(int, input().split()))
g = [[] for j in range(n + 1)]
for i in range(m):
	(u, v) = map(int, input().split())
	g[u].append(v)
	g[v].append(u)
print(answer(n, m, A, g))


from sys import setrecursionlimit
setrecursionlimit(10 ** 7)
number = [int(c) for c in input().split(' ')]
zoloto = [-1] + [int(c) for c in input().split(' ')]
vershini = [-1]
it = 1
while it <= number[0]:
	vershini.append([])
	it += 1
it = 1
while it <= number[1]:
	(a, b) = [int(c) for c in input().split(' ')]
	vershini[a].append(b)
	vershini[b].append(a)
	it += 1
mas = []
trumas = [-1] + [False] * number[0]
sluhmas = []
summa = 0
minimum = 0
it = 1
while it <= number[0]:
	if trumas[it] == False:
		mas.append(it)
		minimum = zoloto[it]
		trumas[it] = True
		while len(mas) != 0:
			if len(vershini[mas[len(mas) - 1]]) != 0:
				if minimum > zoloto[vershini[mas[len(mas) - 1]][0]]:
					minimum = zoloto[vershini[mas[len(mas) - 1]][0]]
				mas.append(vershini[mas[len(mas) - 1]][0])
				trumas[mas[len(mas) - 1]] = True
				vershini[mas[len(mas) - 2]].pop(0)
				if mas[len(mas) - 2] in vershini[mas[len(mas) - 1]]:
					vershini[mas[len(mas) - 1]].remove(mas[len(mas) - 2])
			while len(vershini[mas[len(mas) - 1]]) == 0:
				mas.pop()
				if len(mas) == 0:
					break
		summa += minimum
	it += 1
print(summa)


import sys, itertools, math, collections, random

def ia():
	return [int(i) for i in sys.stdin.readline().strip().split(' ')]

def ii():
	return int(sys.stdin.readline().strip())

def istr():
	return sys.stdin.readline().strip()

class UnionFind:

	def __init__(self, N, G):
		self.N = N
		self.P = list(range(N))
		self.G = G
		self.R = [1] * N

	def find(self, n):
		if self.P[n] == n:
			return n
		else:
			self.P[n] = self.find(self.P[n])
			return self.P[n]

	def join(self, a, b):
		ra = self.find(a)
		rb = self.find(b)
		if self.R[ra] < self.R[rb]:
			self.P[ra] = rb
			self.R[rb] += self.R[ra]
		else:
			self.P[rb] = ra
			self.R[ra] += self.R[rb]
		self.G[ra] = min(self.G[ra], self.G[rb])
		self.G[rb] = min(self.G[ra], self.G[rb])

	def same_set(self, a, b):
		return self.find(a) == self.find(b)
(n, m) = ia()
G = [0] + ia()
UF = UnionFind(n + 1, G)
for _ in range(m):
	(a, b) = ia()
	UF.join(b, a)
ans = 0
V = set()
for i in range(1, n + 1):
	ri = UF.find(i)
	if i in V or ri in V:
		continue
	ans += UF.G[UF.find(i)]
	V.add(i)
	V.add(ri)
print(ans)


(n, m) = (int(i) for i in input().split())
priceMap = [None] + [int(i) for i in input().split()]
graph = {}
for _ in range(m):
	(start, end) = (int(i) for i in input().split())
	if start not in graph:
		graph[start] = [end]
	else:
		graph[start].append(end)
	if end not in graph:
		graph[end] = [start]
	else:
		graph[end].append(start)
visited = set()
cost = 0

def findFriends(i, graph, visited):
	stack = [i]
	minCost = priceMap[i]
	while stack:
		s = stack.pop()
		visited.add(s)
		for j in graph[s]:
			if j not in visited:
				stack.append(j)
				minCost = min(minCost, priceMap[j])
	return minCost
for i in range(1, n + 1):
	if i not in visited:
		if i not in graph:
			minCost = priceMap[i]
			visited.add(i)
		else:
			minCost = findFriends(i, graph, visited)
		cost += minCost
print(cost)


import re
import sys
exit = sys.exit
from bisect import bisect_left as bsl, bisect_right as bsr
from collections import Counter, defaultdict as ddict, deque
from functools import lru_cache
cache = lru_cache(None)
from heapq import *
from itertools import *
from math import inf
from pprint import pprint as pp
enum = enumerate
ri = lambda : int(rln())
ris = lambda : list(map(int, rfs()))
rln = sys.stdin.readline
rl = lambda : rln().rstrip('\n')
rfs = lambda : rln().split()
d4 = [(0, -1), (1, 0), (0, 1), (-1, 0)]
d8 = [(-1, -1), (0, -1), (1, -1), (-1, 0), (1, 0), (-1, 1), (0, 1), (1, 1)]
(n, m) = ris()
c = ris()
adj = [[] for _ in range(n)]
for _ in range(m):
	(x, y) = ris()
	(x, y) = (x - 1, y - 1)
	adj[x].append(y)
	adj[y].append(x)
ans = 0
vis = [0] * n
for u in range(n):
	if vis[u]:
		continue
	vis[u] = 1
	stk = [u]
	mi = inf
	while stk:
		u = stk.pop()
		mi = min(mi, c[u])
		for v in adj[u]:
			if vis[v]:
				continue
			vis[v] = 1
			stk.append(v)
	ans += mi
print(ans)


import sys, threading
sys.setrecursionlimit(100010)
threading.stack_size(67108864)
res = 0

def main():
	(n, m) = list(map(int, input().split()))
	c = [int(c) for c in input().split()]
	g = {}
	flag = [0 for i in range(n)]
	for i in range(n):
		g[i] = []
	for i in range(m):
		(p, o) = list(map(int, input().split()))
		o -= 1
		p -= 1
		g[o].append(p)
		g[p].append(o)

	def glu(v, p, k):
		nonlocal res
		if flag[v] == 0:
			flag[v] = k
			for i in g[v]:
				if i != p:
					glu(i, v, k)
	k = 1
	for i in range(n):
		l = flag[i]
		glu(i, -1, k)
		if l == 0:
			k += 1
	mn = {}
	for i in range(max(flag)):
		mn[i] = 10 ** 9
	for i in range(n):
		if mn[flag[i] - 1] >= c[i]:
			mn[flag[i] - 1] = c[i]
	su = 0
	for i in range(len(mn)):
		su += mn[i]
	print(su)
thread = threading.Thread(target=main)
thread.start()


from collections import deque
import sys
sys.setrecursionlimit(5000)

class Graph(object):

	def __init__(self, vertices, adj):
		self.vertices = [i for i in range(1, vertices + 1)]
		self.adj = adj
		self.fix_adj(vertices)

	def fix_adj(self, v):
		for i in range(1, v + 1):
			try:
				a = self.adj[i]
			except KeyError:
				self.adj[i] = []

	def iter_vertices(self):
		for v in self.vertices:
			yield v

	def adj_list(self, v):
		return self.adj[v]

class BFSResult:

	def __init__(self):
		self.level = {}
		self.parent = {}
		self.connected_comp = []

def bfs(g, s, r):
	r.level[s] = 0
	r.parent[s] = None
	q = deque()
	q.append(s)
	c_cp = [s]
	while q:
		u = q.popleft()
		for v in g.adj_list(u):
			if v not in r.level:
				r.parent[v] = u
				r.level[v] = r.level[u] + 1
				q.append(v)
				c_cp.append(v)
	r.connected_comp.append(c_cp)
(people, no_friends) = list(map(int, input().rstrip().split()))
cost = list(map(int, input().rstrip().split()))
adj = {}
for _ in range(no_friends):
	(u, v) = list(map(int, input().rstrip().split()))
	try:
		adj[u].append(v)
	except KeyError:
		adj[u] = [v]
	try:
		adj[v].append(u)
	except KeyError:
		adj[v] = [u]
g = Graph(people, adj)
r = BFSResult()
for s in g.iter_vertices():
	if s not in r.level:
		bfs(g, s, r)
ans = 0
for cc in r.connected_comp:
	choice = None
	for i in cc:
		if choice == None:
			choice = cost[i - 1]
		elif cost[i - 1] < choice:
			choice = cost[i - 1]
	ans += choice
print(ans)


import sys
from math import sqrt, gcd, ceil, log
from collections import defaultdict, Counter, deque
inp = sys.stdin.readline
read = lambda : list(map(int, inp().strip().split()))

def main():
	(n, m) = read()
	cost = [0] + read()
	adj = [[] for i in range(n + 1)]
	visited = [0] * (n + 1)
	for i in range(m):
		(u, v) = read()
		adj[u].append(v)
		adj[v].append(u)
	ans = 0
	stack = []
	for i in range(1, n + 1):
		if not visited[i]:
			min_cost = cost[i]
			stack.append(i)
			while stack:
				node = stack.pop()
				visited[node] = 1
				min_cost = min(min_cost, cost[node])
				for child in adj[node]:
					if not visited[child]:
						stack.append(child)
			ans += min_cost
	print(ans)
main()


import math
import time
from collections import defaultdict, deque
from sys import stdin, stdout
from bisect import bisect_left, bisect_right
(n, m) = map(int, input().split())
a = list(map(int, input().split()))
graph = defaultdict(lambda : [])
visited = defaultdict(lambda : False)
for _ in range(m):
	(x, y) = map(int, input().split())
	graph[x].append(y)
	graph[y].append(x)

def dfs(root):
	toret = a[root - 1]
	q = deque()
	q.append(root)
	visited[root] = True
	while len(q) > 0:
		temp = q.popleft()
		toret = min(toret, a[temp - 1])
		for i in graph[temp]:
			if not visited[i]:
				visited[i] = True
				q.append(i)
	return toret
ans = 0
for i in range(1, n + 1):
	if not visited[i]:
		ans += dfs(i)
print(ans)


(n, m) = map(int, input().split())
c = [0] + list(map(int, input().split()))
visited = [0] * (n + 1)
adj = [[] for i in range(1 + n)]

def bfs(x):
	queue = [x]
	li = [x]
	visited[x] = 1
	while queue:
		u = queue.pop(0)
		for v in adj[u]:
			if visited[v] == 0:
				li.append(v)
				queue.append(v)
				visited[v] = 1
	return li
for i in range(m):
	(x, y) = map(int, input().split())
	adj[x].append(y)
	adj[y].append(x)
ans = 0
if m == 0:
	ans = sum(c)
else:
	for i in range(1, n + 1):
		if visited[i] == 0:
			comp = bfs(i)
			t = float('inf')
			for x in comp:
				t = min(t, c[x])
			if t != float('inf'):
				ans += t
print(ans)


def find(p):
	while p != connection[p]:
		connection[p] = connection[connection[p]]
		p = connection[p]
	return p
inp = lambda : map(int, input().split())
(n, m) = inp()
c = [0] + [*inp()]
connection = [i for i in range(n + 1)]
for i in range(m):
	(x, y) = inp()
	x = find(x)
	y = find(y)
	if c[x] > c[y]:
		(x, y) = (y, x)
	connection[y] = x
print(sum([c[i] if i == connection[i] else 0 for i in range(n + 1)]))


def dfs(i):
	value = cost[i]
	stack = [i]
	while stack:
		popped = stack.pop()
		vistied[popped] = True
		for friend in adj_list[popped]:
			if vistied[friend] == False:
				stack.append(friend)
				value = min(value, cost[friend])
	return value

def get():
	return list(map(int, input().split()))
(n, m) = get()
cost = [0] + get()
adj_list = [list() for _ in range(n + 1)]
for _ in range(1, m + 1):
	(x, y) = get()
	adj_list[x].append(y)
	adj_list[y].append(x)
vistied = [False] * (n + 1)
ans = 0
for i in range(1, n + 1):
	if vistied[i] == False:
		ans += dfs(i)
print(ans)


def zip_sorted(a, b):
	(a, b) = zip(*sorted(zip(a, b)))
	sorted(zip(a, b), key=lambda x: x[1])
	return (a, b)
import sys
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))
S = lambda : list(map(str, input().split()))
(n, m) = I()
a = I()
adj = [[i] for i in range(n)]
for i in range(m):
	(edge_start, edge_end) = I()
	adj[edge_start - 1].append(edge_end - 1)
	adj[edge_end - 1].append(edge_start - 1)
visited = [0] * n

def bfs(adj, n, start, visited):
	q = [start]
	visited[start] = 1
	min2 = a[start]
	while q:
		curr = q.pop()
		for i in adj[curr]:
			if curr == i:
				continue
			if visited[i] == 0:
				q.append(i)
				visited[i] = 1
			min2 = min(min2, a[curr])
	return min2
comp = 0
for i in range(n):
	if visited[i] == 0:
		min1 = bfs(adj, n, i, visited)
		comp += min1
print(comp)


from collections import deque
start = list(map(int, input().strip().split()))
cost = list(map(int, input().strip().split()))
dp = dict()
for _ in range(start[1]):
	k = list(map(int, input().strip().split()))
	if k[0] - 1 not in dp:
		dp[k[0] - 1] = dict()
	if k[1] - 1 not in dp:
		dp[k[1] - 1] = dict()
	dp[k[0] - 1][k[1] - 1] = 0
	dp[k[1] - 1][k[0] - 1] = 0
counted = dict()
answer = 0
for (index, value) in enumerate(cost):
	if index not in dp:
		answer += value
	elif index in counted:
		continue
	else:
		now = index
		plus = value
		counted[index] = 0
		dec = deque()
		for key in dp[now]:
			dec.append(key)
		while len(dec) > 0:
			k = dec.popleft()
			if k in counted:
				continue
			plus = min(plus, cost[k])
			counted[k] = 0
			if k in dp:
				for key in dp[k]:
					dec.append(key)
		answer += plus
print(answer)


from collections import deque

def rec(v):
	mini = 1000000000.0 + 1
	global flag
	global a
	global price
	queue = deque()
	queue.append(v)
	while len(queue) != 0:
		x = queue.popleft()
		mini = min(price[x], mini)
		flag[x] = True
		for i in range(len(a[x])):
			if not flag[a[x][i]]:
				queue.append(a[x][i])
	return mini
(n, m) = [int(x) for x in input().split()]
price = [int(x) for x in input().split()]
a = []
flag = [False] * n
mini = 0
ans = 0
for i in range(n):
	c = []
	a.append(c)
for i in range(m):
	(x, y) = [int(x) for x in input().split()]
	x -= 1
	y -= 1
	a[x].append(y)
	a[y].append(x)
for i in range(n):
	if not flag[i]:
		ans += rec(i)
print(ans)


from collections import defaultdict

def bfs(i):
	global l, g, visited
	visited[i] = True
	queue = [i]
	val = l[i]
	while queue:
		ele = queue.pop(0)
		if val > l[ele]:
			val = l[ele]
		for v in g[ele]:
			if visited[v] == False:
				visited[v] = True
				queue.append(v)
	return val
(n, k) = map(int, input().split())
l = list(map(int, input().split()))
g = defaultdict(list)
for i in range(k):
	(u, v) = map(int, input().split())
	g[u - 1].append(v - 1)
	g[v - 1].append(u - 1)
summ = 0
visited = [False] * n
for i in range(n):
	if visited[i] == False:
		summ += bfs(i)
print(summ)


(n, m) = (5, 2)
c = [2, 5, 3, 4, 8]
B = [[1, 4], [4, 5]]
(n, m) = map(int, input().split())
c = tuple(map(int, input().split()))
d = [{} for _ in range(n)]
for i in range(m):
	(x, y) = map(int, input().split())
	d[x - 1][y - 1] = c[x - 1]
	d[y - 1][x - 1] = c[y - 1]
mark = [False] * n

def dfs(edges, start):
	s = [start]
	comp.append(start)
	m = 0
	while s:
		v = s.pop()
		for (key, val) in edges[v].items():
			if not mark[key]:
				s.append(key)
				comp.append(key)
				mark[key] = True
if m == 0:
	print(sum(c))
else:
	S = 0
	for i in range(n):
		if not mark[i]:
			comp = []
			dfs(d, i)
			S += min(map(lambda x: c[x], comp))
	print(S)


import sys
from collections import defaultdict, deque
(n, m) = map(int, input().split())
cost = list(map(int, input().split()))
graph = defaultdict(list)
vis = [0] * n
for i in range(m):
	(a, b) = map(int, input().split())
	a -= 1
	b -= 1
	graph[a].append(b)
	graph[b].append(a)
ans = 0
for j in range(n):
	l = 1000000000
	if vis[j] == 0:
		d = deque([j])
		nl = 1
		while nl > 0:
			ver = d.pop()
			vis[ver] = 1
			l = min(l, cost[ver])
			nl -= 1
			for i in graph[ver]:
				if vis[i] == 0:
					d.append(i)
					nl += 1
		ans += l
print(ans)


import sys
import threading
from math import *
sys.setrecursionlimit(300000)
threading.stack_size(10 ** 8)

def main():

	def dfs(node):
		v[node] = 1
		b.append(ar[node - 1])
		for child in g[node]:
			if v[child] == 0:
				dfs(child)
	(n, e) = map(int, input().split())
	v = [0] * (n + 1)
	ar = list(map(int, input().split()))
	from collections import defaultdict
	g = defaultdict(list)
	for i in range(0, e):
		(a, b) = map(int, input().split())
		g[a].append(b)
		g[b].append(a)
	c = 0
	for i in range(1, n + 1):
		if v[i] == 0:
			b = []
			dfs(i)
			c = c + min(b)
	print(c)
t = threading.Thread(target=main)
t.start()
t.join()


(n, m) = map(int, input().split())
cost = [0]
cost += list(map(int, input().split()))
parent = [0 for i in range(100100)]
ans = 0

def root_search(node):
	try:
		if parent[node] == 0:
			return node
		else:
			parent[node] = root_search(parent[node])
			return parent[node]
	except:
		return node
for i in range(1, m + 1):
	(f1, f2) = map(int, input().split())
	tep_node1 = root_search(f1)
	tep_node2 = root_search(f2)
	if tep_node1 == tep_node2:
		continue
	if cost[tep_node1] <= cost[tep_node2]:
		parent[tep_node2] = tep_node1
	else:
		parent[tep_node1] = tep_node2
for i in range(1, n + 1):
	if parent[i] == 0:
		ans += cost[i]
print(ans)


import sys
from sys import stdin, stdout
import bisect
import math
sys.setrecursionlimit(1000000)
mod = 10 ** 9 + 7

def st():
	return list(stdin.readline().strip())

def inp():
	return int(stdin.readline())

def li():
	return list(map(int, stdin.readline().split()))

def mp():
	return map(int, stdin.readline().split())

def pr(n):
	stdout.write(str(n) + '\n')

def DFS(dictionary, vertex, visited):
	visited[vertex] = True
	stack = [vertex]
	print(vertex)
	while stack:
		a = stack.pop()
		for i in dictionary[a]:
			if not visited[i]:
				print(i)
				visited[i] = True
				stack.append(i)

def soe(limit):
	l = [1] * (limit + 1)
	l[0] = 0
	l[1] = 0
	prime = []
	for i in range(2, limit + 1):
		if l[i]:
			for j in range(i * i, limit + 1, i):
				l[j] = 0
	for i in range(2, limit + 1):
		if l[i]:
			prime.append(i)
	return prime

def segsoe(low, high):
	limit = int(high ** 0.5) + 1
	prime = soe(limit)
	n = high - low + 1
	l = [0] * (n + 1)
	for i in range(len(prime)):
		lowlimit = low // prime[i] * prime[i]
		if lowlimit < low:
			lowlimit += prime[i]
		if lowlimit == prime[i]:
			lowlimit += prime[i]
		for j in range(lowlimit, high + 1, prime[i]):
			l[j - low] = 1
	for i in range(low, high + 1):
		if not l[i - low]:
			if i != 1:
				print(i)

def gcd(a, b):
	while b:
		a = a % b
		(b, a) = (a, b)
	return a

def power(a, n):
	r = 1
	while n:
		if n & 1:
			r = r * a
		a *= a
		n = n >> 1
	return r

def DFS(d, visited, l, ver):
	ans = l[ver]
	visited[ver] = True
	stack = [ver]
	while stack:
		a = stack.pop()
		for i in d[a]:
			if not visited[i]:
				ans = min(ans, l[i])
				visited[i] = True
				stack.append(i)
	return ans

def solve():
	(n, m) = mp()
	l = li()
	l = [0] + l
	visited = [False for i in range(n + 1)]
	d = {i: [] for i in range(1, n + 1)}
	for i in range(m):
		(a, b) = mp()
		d[a].append(b)
		d[b].append(a)
	ans = 0
	for i in range(1, n + 1):
		if not visited[i]:
			ans += DFS(d, visited, l, i)
	pr(ans)
for _ in range(1):
	solve()


import math
import sys
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
ilele = lambda : map(int, input().split())
alele = lambda : list(map(int, input().split()))

def dfs(curr):
	s = []
	s.append(curr)
	mini = bribes[curr]
	while len(s) > 0:
		n = s.pop()
		if visited[n]:
			continue
		visited[n] = True
		mini = min(mini, bribes[n])
		for ne in aj[n]:
			s.append(ne)
	return mini
mn = alele()
n = mn[0]
m = mn[1]
bribes = alele()
aj = [[] for i in range(n)]
for i in range(m):
	xy = input().split(' ')
	x = int(xy[0])
	y = int(xy[1])
	aj[x - 1].append(y - 1)
	aj[y - 1].append(x - 1)
total = 0
visited = [False] * n
for i in range(n):
	if not visited[i]:
		total += dfs(i)
print(total)


def main():
	(n, m) = map(int, input().split())
	cc = [0, *map(int, input().split())]
	edges = [[] for _ in range(n + 1)]
	for _ in range(m):
		(u, v) = map(int, input().split())
		edges[u].append(v)
		edges[v].append(u)
	(l, r) = ([True] * (n + 1), 0)
	for (i, f) in enumerate(l):
		if f:
			(stack, c) = ([i], 1000000000)
			while stack:
				a = stack.pop()
				if l[a]:
					l[a] = False
					if c > cc[a]:
						c = cc[a]
					stack += edges[a]
			r += c
	print(r)
main()


(N, M) = [int(i) for i in input().split()]
gold = {int(i): int(val) for (i, val) in enumerate(input().split())}
lists = [[] for _ in range(N)]
checked = [False for _ in range(N)]
gold_needed = 0
for _ in range(M):
	(A, B) = [int(i) - 1 for i in input().strip().split()]
	lists[A].append(B)
	lists[B].append(A)
for i in range(N):
	if checked[i]:
		continue
	group_gold = gold[i]
	checked[i] = True
	queue = [i]
	while queue:
		point = queue[0]
		del queue[0]
		for p in lists[point]:
			if not checked[p]:
				checked[p] = True
				queue.append(p)
		group_gold = min(group_gold, gold[point])
	gold_needed += group_gold
print(gold_needed)


class Node:

	def __init__(self, id, cost):
		self.id = id
		self.cost = cost
		self.neighbors = set()
		self.explored = False

class Graph:

	def __init__(self, nodes, costs):
		self.nodes = [Node(x + 1, costs[x]) for x in range(nodes)]

	def add_edge(self, u, v):
		self.nodes[u].neighbors.add(self.nodes[v])
		self.nodes[v].neighbors.add(self.nodes[u])

	def foo(self):
		min_amount = 0
		for node in self.nodes:
			if not node.explored:
				min_u = node.cost
				node.explored = True
				queue = [node]
				while len(queue) != 0:
					current_node = queue.pop(0)
					for neighbor in current_node.neighbors:
						if not neighbor.explored:
							if min_u > neighbor.cost:
								min_u = neighbor.cost
							neighbor.explored = True
							queue.append(neighbor)
				min_amount += min_u
		return min_amount
(n, m) = [int(x) for x in input().split()]
c_l = [int(x) for x in input().split()]
graph = Graph(n, c_l)
for _ in range(m):
	(u, v) = [int(x) for x in input().split()]
	graph.add_edge(u - 1, v - 1)
print(graph.foo())


import sys
from collections import defaultdict
(n, m) = map(int, sys.stdin.readline().split())
c = list(map(int, sys.stdin.readline().split()))
graph = defaultdict(list)
for _ in range(m):
	(u, v) = map(int, sys.stdin.readline().split())
	graph[u - 1].append(v - 1)
	graph[v - 1].append(u - 1)
seen = set()
cost = 0
for i in range(n):
	minn = c[i]
	if i not in seen:
		seen.add(i)
		stack = [i]
		while stack:
			node = stack.pop()
			for nei in graph[node]:
				if nei not in seen:
					seen.add(nei)
					minn = min(c[nei], minn)
					stack.append(nei)
		cost += minn
print(cost)


from collections import defaultdict

class Graph:

	def __init__(self):
		self.graph = defaultdict(list)

	def addEdge(self, fr, to):
		self.graph[fr].append(to)
		self.graph[to].append(fr)

	def solve(self, visited, gold, n):
		Total = 0
		for i in range(n):
			if self.graph[i + 1]:
				if visited[i] == False:
					Min = 10 ** 9 + 7
					queue = [i + 1]
					visited[i] = True
					while queue:
						s = queue.pop(0)
						Min = min(Min, gold[s - 1])
						for j in self.graph[s]:
							if visited[j - 1] == False:
								queue.append(j)
								visited[j - 1] = True
					if Min != 10 ** 9 + 7:
						Total += Min
		for i in range(n):
			if visited[i] == False:
				Total += gold[i]
		return Total
(n, m) = map(int, input().split())
gold = [int(x) for x in input().split()]
visited = [False] * n
G = Graph()
for i in range(m):
	(a, b) = map(int, input().split())
	G.addEdge(a, b)
print(G.solve(visited, gold, n))


import collections

def dfs_gold_min2(start_v, visited, graph, gold):
	my_stack = collections.deque()
	my_stack.append(start_v)
	min_gold = gold[start_v - 1]
	while my_stack:
		v = my_stack.pop()
		visited[v - 1] = 1
		min_gold = min(min_gold, gold[v - 1])
		for u in graph[v]:
			if not visited[u - 1]:
				my_stack.append(u)
	return min_gold
(n, m) = list(map(int, input().split()))
gold = list(map(int, input().split()))
graph = {}
for i in range(1, n + 1):
	graph[i] = []
for _ in range(m):
	(m1, m2) = list(map(int, input().split()))
	graph[m1].append(m2)
	graph[m2].append(m1)
visited = [0] * n
gold_full = 0
for v in graph.keys():
	if not visited[v - 1]:
		gold_full += dfs_gold_min2(v, visited, graph, gold)
print(gold_full)


(n, m) = [int(i) for i in input().split(' ')]
c = [int(i) for i in input().split(' ')]
chars = [[] for _ in range(n)]
for _ in range(m):
	(p, q) = [int(i) for i in input().split(' ')]
	p = p - 1
	q = q - 1
	chars[p].append(q)
	chars[q].append(p)

def dfs(i, nodes):
	if Visited[i]:
		return
	Visited[i] = True
	nodes.append(i)
	for j in chars[i]:
		dfs(j, nodes)
Visited = [False for i in range(n)]
Graph = []
C = 0
for i in range(n):
	if not Visited[i]:
		nodes = []
		nodes.append(i)
		tmp = 10000000000
		while len(nodes) > 0:
			u = nodes.pop()
			if Visited[u] == False:
				Visited[u] = True
				tmp = min(tmp, c[u])
				for j in chars[u]:
					nodes.append(j)
		C += tmp
		Graph.append(nodes)
print(C)


from collections import defaultdict

def dfs(root, graph, C, seen):
	m = C[root - 1]
	stack = [root]
	while len(stack) > 0:
		x = stack.pop()
		seen[x] = True
		m = min(m, C[x - 1])
		for y in graph[x]:
			if not seen[y]:
				stack.append(y)
	return m
(n, m) = map(int, input().split())
C = [int(c) for c in input().split()]
graph = defaultdict(list)
for _ in range(m):
	(x, y) = map(int, input().split())
	graph[x].append(y)
	graph[y].append(x)
ans = 0
seen = [False] * (n + 1)
for i in range(1, n + 1):
	if not seen[i]:
		ans += dfs(i, graph, C, seen)
print(ans)


from collections import deque
(n, m) = list(map(int, input().split()))
c = list(map(int, input().split()))
graph = {}
for i in range(n):
	graph[i] = []
for _ in range(m):
	(x, y) = list(map(int, input().split()))
	graph[x - 1].append(y - 1)
	graph[y - 1].append(x - 1)
visited = [False] * n
q = deque()
ret = 0
for i in range(n):
	if visited[i]:
		continue
	visited[i] = True
	minVal = c[i]
	q.append(i)
	while q:
		r = q.popleft()
		minVal = min(minVal, c[r])
		for n in graph[r]:
			if visited[n]:
				continue
			q.append(n)
			visited[n] = True
	ret += minVal
print(ret)


(n, m) = map(int, input().split())
c = list(map(int, input().split()))
graph = [set() for i in range(n)]
color = [None] * n
cnt = 0
for i in range(m):
	(v, u) = map(int, input().split())
	v -= 1
	u -= 1
	graph[v].add(u)
	graph[u].add(v)
for i in range(n):
	if color[i] is None:
		cnt += 1
		q = [i]
		while q:
			tmp = q.pop(0)
			color[tmp] = cnt
			for j in graph[tmp]:
				if color[j] is None:
					q.append(j)
comp = [set() for i in range(cnt + 1)]
for u in range(n):
	comp[color[u]].add(u)
summ = 0
for k in range(1, cnt + 1):
	minn = float('INF')
	for u in comp[k]:
		minn = min(minn, c[u])
	summ += minn
print(summ)


(n, m) = map(int, input().split())
ls = list(map(int, input().split()))
frnd = {i: [] for i in range(1, n + 1)}
for j in range(m):
	(x, y) = map(int, input().split())
	frnd[x].append(y)
	frnd[y].append(x)
sm = 0
for k in range(1, len(frnd) + 1):
	if ls[k - 1] == -1:
		continue
	elif len(frnd[k]) == 0:
		sm = sm + ls[k - 1]
	else:
		stack = [k]
		mm = ls[k - 1]
		ls[k - 1] = -1
		while stack:
			x = stack.pop()
			for neighbour in frnd[x]:
				if ls[neighbour - 1] != -1:
					stack.append(neighbour)
					mm = min(ls[neighbour - 1], mm)
					ls[neighbour - 1] = -1
		sm += mm
print(sm)


import sys
from collections import defaultdict as d
(n, k) = map(int, input().split())
adj = d(list)
ans = 0
c = list(map(int, input().split()))
c.insert(0, 0)
used = [False] * (n + 1)
minn = sys.maxsize

def dfs(s):
	global minn, used
	stack = []
	if not used[i]:
		stack.append(s)
	while stack:
		x = stack.pop()
		if not used[x]:
			used[x] = True
			if c[x] < minn:
				minn = c[x]
		for node in adj[x]:
			if not used[node]:
				stack.append(node)
	return
for i in range(k):
	(x, y) = map(int, input().split())
	adj[x].append(y)
	adj[y].append(x)
for i in range(1, n + 1):
	if not used[i]:
		minn = sys.maxsize
		dfs(i)
		ans += minn
print(ans)


from collections import defaultdict
(x, y) = map(int, input().split())
a = list(map(int, input().split()))
c = [0] * x
if y == 0:
	print(sum(a))
else:
	d = defaultdict(list)
	for __ in range(y):
		(p, q) = map(int, input().split())
		d[p - 1].append(q - 1)
		d[q - 1].append(p - 1)
	lol = 0
	while 0 in c:
		i = c.index(0)
		q = []
		yo = 100000000001
		q.append(i)
		c[i] = 1
		while len(q) > 0:
			po = q.pop()
			yo = min(yo, a[po])
			for j in d[po]:
				if c[j] != 1:
					q.append(j)
					yo = min(yo, a[j])
					c[j] = 1
		lol += yo
	print(lol)


from collections import deque

def solve():
	nm = input().split()
	n = int(nm[0])
	m = int(nm[1])
	c = list(map(int, input().split()))
	friends = dict()
	for i in range(n):
		friends[i] = list()
	for i in range(m):
		uv = input().split()
		u = int(uv[0])
		v = int(uv[1])
		friends[u - 1].append(v - 1)
		friends[v - 1].append(u - 1)
	characters = [(i, c[i]) for i in range(n)]
	characters = sorted(characters, key=lambda character: character[1])
	heard = [False for _ in range(n)]
	bribe = 0
	for character in characters:
		index = character[0]
		if not heard[index]:
			heard[index] = True
			cost = character[1]
			bribe += cost
			q = deque()
			q.append(index)
			while q:
				cur = q.popleft()
				heard[cur] = True
				for friend in friends.get(cur):
					if not heard[friend]:
						heard[friend] = True
						q.append(friend)
	print(bribe)
solve()


(siz, n) = map(int, input().split())
p = [0] + list(map(int, input().split()))
t = [x for x in range(siz + 1)]

def get(ind):
	while ind != t[ind]:
		t[ind] = t[t[ind]]
		ind = t[ind]
	return ind
for x in range(n):
	(c, d) = map(lambda x: get(int(x)), input().split())
	if p[c] < p[d]:
		(c, d) = (d, c)
	t[c] = d
print(sum([p[i] if i == t[i] else 0 for i in range(1, siz + 1)]))


import sys
input = lambda : sys.stdin.readline().rstrip('\r\n')
ip = [line for line in sys.stdin.read().split('\n')]
it = 0
(n, m) = map(int, ip[it].split())
it += 1
cost = [int(x) for x in ip[it].split()]
it += 1
g = [[] for _ in range(n)]
vis = [False] * n

def dfs(src):
	st = [src]
	ct = float('inf')
	while st:
		node = st.pop()
		vis[node] = True
		ct = min(ct, cost[node])
		for nbr in g[node]:
			if not vis[nbr]:
				st.append(nbr)
	return ct
for _ in range(m):
	(u, v) = map(int, ip[it].split())
	it += 1
	u -= 1
	v -= 1
	g[u].append(v)
	g[v].append(u)
ans = 0
for i in range(n):
	if not vis[i]:
		ans += dfs(i)
print(ans)


def dfs(i, vis, a):
	stk = [i]
	vis[i] = 1
	while stk:
		v = stk.pop()
		a.append(c[v])
		nb = list(filter(lambda x: vis[x] == 0, adj[v]))
		for w in nb:
			vis[w] = 1
		stk.extend(nb)
(n, m) = map(int, input().split())
c = list(map(int, input().split()))
adj = [[] for _ in range(n)]
vis = [0] * n
for _ in range(m):
	(x, y) = map(int, input().split())
	adj[x - 1].append(y - 1)
	adj[y - 1].append(x - 1)
s = 0
for i in range(n):
	if not vis[i]:
		a = []
		dfs(i, vis, a)
		s += min([j for j in a])
print(s)


from collections import defaultdict

def dfs(source):
	s = [source]
	minGold = float('inf')
	while s:
		node = s.pop()
		visited[node] = True
		minGold = min(minGold, gold[node])
		for nbr in g[node]:
			if not visited[nbr]:
				s.append(nbr)
	return minGold
(n, m) = list(map(int, input().split()))
gold = list(map(int, input().split()))
g = defaultdict(list)
visited = [False] * n
for _ in range(m):
	(a, b) = map(int, input().split())
	g[a - 1].append(b - 1)
	g[b - 1].append(a - 1)
ans = 0
for i in range(n):
	if not visited[i]:
		ans += dfs(i)
print(ans)


class Vertice:

	def __init__(self, gold):
		self.gold = gold
		self.visited = 0
		self.adj = []

	def addAdj(self, vertex):
		self.adj.append(vertex)
(n, m) = [int(i) for i in input().split()]
vertices = [Vertice(int(i)) for i in input().split()]
for i in range(m):
	(a, b) = [int(i) - 1 for i in input().split()]
	vertices[a].addAdj(vertices[b])
	vertices[b].addAdj(vertices[a])
cost = 0
F = []
for i in vertices:
	if i.visited == 0:
		i.visited = 1
		F.append(i)
		valor = [i.gold]
		while F != []:
			v = F[0]
			del F[0]
			for w in v.adj:
				if w.visited == 0:
					w.visited = 1
					valor.append(w.gold)
					F.append(w)
		cost += min(valor)
print(cost)


class UnionFind:

	def __init__(self, n, cost):
		self.table = [i for i in range(n)]
		self.rank = [1 for _ in range(n)]
		self.cost = cost[:]
		self.cost.insert(0, 0)

	def unite(self, x, y):
		x = self.find(x)
		y = self.find(y)
		if x == y:
			return False
		if self.rank[x] > self.rank[y]:
			self.table[y] = x
		else:
			self.table[x] = y
			if self.rank[x] == self.rank[y]:
				self.rank[y] += 1
		if self.cost[x] > self.cost[y]:
			self.cost[x] = self.cost[y]
		else:
			self.cost[y] = self.cost[x]

	def find(self, x):
		if self.table[x] == x:
			return x
		self.table[x] = self.find(self.table[x])
		return self.table[x]

	def SameGragh(self, x, y):
		if self.find(x) == self.find(y):
			return True
		else:
			return False

	def sumCost(self):
		sumval = 0
		for i in range(1, n + 1):
			if self.table[i] == i:
				sumval += self.cost[i]
		return sumval
(n, m) = map(int, input().split(' '))
cost = list(map(int, input().split(' ')))
group = UnionFind(n + 1, cost)
used = [0] * (n + 1)
for i in range(m):
	(a, b) = map(int, input().split(' '))
	if not group.SameGragh(a, b):
		group.unite(a, b)
print(group.sumCost())


l = lambda : map(int, input().split())
t = lambda : int(input())
ss = lambda : input()
(n, m) = l()
a = list(l())
d = {}
for i in range(m):
	(u, v) = l()
	if u - 1 not in d.keys():
		d[u - 1] = []
	if v - 1 not in d.keys():
		d[v - 1] = []
	d[u - 1].append(v - 1)
	d[v - 1].append(u - 1)
s = 0
v = [0] * n
for i in d.keys():
	if v[i] == 0:
		q = [i]
		t = 50 ** 9
		while len(q):
			x = q.pop()
			t = min(a[x], t)
			for j in d[x]:
				if v[j] == 0:
					q.append(j)
					v[j] = 1
		s += t
for i in range(n):
	if not v[i]:
		s += a[i]
print(s)