You are given a forest with N vertices and M edges. Each vertex has a value. You want to add edges to connect the forest. Adding an edge between vertices i and j costs ai + aj dollars. Find the minimum total cost to connect the forest, or print "Impossible" if it's not possible. Constraints: 1 ≤ N ≤ 100,000, 0 ≤ M ≤ N-1, 1 ≤ ai ≤ 10^9, 0 ≤ xi, yi ≤ N-1. Input format: N M, followed by ai values, followed by M pairs of xi and yi. Output the minimum total cost or "Impossible". Example input and output are provided.

# Buggy version:
from heapq import heappush, heappop
from collections import defaultdict
n,m = map(int,input().split())
a = list(map(int,input().split()))
# edg = [[] for i in range(n)]
rank = [1] * n
par = [i for i in range(n)]
def root(x):
  if x == par[x]:
    return x
  par[x] = root(par[x])
  return par[x]
def union(a,b):
  a = root(a)
  b = root(b)
  if rank[a] < rank[b]:a,b = b,a
  if a == b: return False
  par[b] = a
  rank[a] += rank[b]
  return True
for _ in range(m):
  x,y = map(int, input().split())
  union(x,y)
d = defaultdict(list)
q = []
for i in range(n):
  ai = a[i]
  heappush(q,(ai,i))
  ri = root(i)
  heappush(d[ri],(ai,i))

ans = 0
tmp = 0
c = [0] * n

for i in d.keys():
  tmp += 1
  t,p = heappop(d[i])
  c[p] = 1
  ans += t
if 2*tmp > n:
  print("Impossible")
else:
  for _ in range(tmp):
    p,i = heappop(q)
    while c[i]:
      p,i = heappop(q)
    ans += p
  print(ans)

# Fixed version:
from heapq import heappush, heappop
from collections import defaultdict
n,m = map(int,input().split())
a = list(map(int,input().split()))
# edg = [[] for i in range(n)]
rank = [1] * n
par = [i for i in range(n)]
def root(x):
  if x == par[x]:
    return x
  par[x] = root(par[x])
  return par[x]
def union(a,b):
  a = root(a)
  b = root(b)
  if rank[a] < rank[b]:a,b = b,a
  if a == b: return False
  par[b] = a
  rank[a] += rank[b]
  return True
for _ in range(m):
  x,y = map(int, input().split())
  union(x,y)
d = defaultdict(list)
q = []
for i in range(n):
  ai = a[i]
  heappush(q,(ai,i))
  ri = root(i)
  heappush(d[ri],(ai,i))
if len(d.keys()) == 1:
  print(0)
else:
  ans = 0
  tmp = 0
  c = [0] * n

  for i in d.keys():
    tmp += 1
    t,p = heappop(d[i])
    c[p] = 1
    ans += t
  if 2*tmp-2 > n:
    print("Impossible")
  else:
    for _ in range(tmp-2):
      p,i = heappop(q)
      while c[i]:
        p,i = heappop(q)
      ans += p
    print(ans)

# Buggy version:
import heapq
from collections import deque


n, m = map(int, input().split())
a = list(map(int, input().split()))
info = [list(map(int, input().split())) for i in range(m)]
tree = [[] for i in range(n)]

for b, c in info:
    tree[b].append(c)
    tree[c].append(b)

visited = [False] * n
group = []
for i in range(n):
    if visited[i]:
        continue
    q = deque([i])
    visited[i] = True
    tmp = [a[i]]
    while q:
        pos = q.pop()
        for next_pos in tree[pos]:
            if visited[next_pos]:
                continue
            visited[next_pos] = True
            q.append(next_pos)
            tmp.append(a[next_pos])
    group.append(tmp)

for i in range(len(group)):
    group[i] = sorted(group[i])

ans = 0
cnt = len(group)*2 - 2

if cnt == 0:
    print(0)
    exit()
    
for i in range(len(group)):
    ans += group[i][0]
    cnt -= 1
    group[i][0] = 10**9 + 7

q = []
for i in range(len(group)):
    for num in group[i]:
        if num != 10**9 +7:
            heapq.heappush(q, num)
print(1)
exit()
for i in range(cnt):
    if not q:
        print("Impossible")
        exit()
    ans += heapq.heappush(q)
print(ans)
    

# Fixed version:
import heapq
from collections import deque


n, m = map(int, input().split())
a = list(map(int, input().split()))
info = [list(map(int, input().split())) for i in range(m)]
tree = [[] for i in range(n)]

for b, c in info:
    tree[b].append(c)
    tree[c].append(b)

visited = [False] * n
group = []
for i in range(n):
    if visited[i]:
        continue
    q = deque([i])
    visited[i] = True
    tmp = [a[i]]
    while q:
        pos = q.pop()
        for next_pos in tree[pos]:
            if visited[next_pos]:
                continue
            visited[next_pos] = True
            q.append(next_pos)
            tmp.append(a[next_pos])
    group.append(tmp)

for i in range(len(group)):
    group[i] = sorted(group[i])

ans = 0
cnt = len(group)*2 - 2

if cnt == 0:
    print(0)
    exit()
    
for i in range(len(group)):
    ans += group[i][0]
    cnt -= 1
    group[i][0] = 10**9 + 7

q = []
for i in range(len(group)):
    for num in group[i]:
        if num != 10**9 +7:
            heapq.heappush(q, num)

for i in range(cnt):
    if not q:
        print("Impossible")
        exit()
    ans += heapq.heappop(q)
print(ans)

# Buggy version:
import sys
sys.setrecursionlimit(10**6)

def input():
	return sys.stdin.readline()[:-1]

n, m = map(int, input().split())
a = list(map(int, input().split()))
adj = [[] for _ in range(n)]
for _ in range(m):
	x, y = map(int, input().split())
	adj[x].append(y)
	adj[y].append(x)

root = [-1 for _ in range(n)]
is_minimum = [False for _ in range(n)]

def dfs(x, r):
	global mi, mi_ind
	if a[x] < mi:
		mi = a[x]
		mi_ind = x
	for v in adj[x]:
		if root[v] < 0:
			root[v] = r
			dfs(v, r)
	return mi_ind

for i in range(n):
	if root[i] < 0:
		mi, mi_ind = 10**10, -1
		root[i] = i
		is_minimum[dfs(i, i)] = True

con_num = len(set(root))
#print(con_num)
if (con_num-1) * 2 > n:
	print("Impossible")
	sys.exit()
elif con_num == 0:
	print(0)
	sys.exit()
ans = sum([a[i] for i in range(n) if is_minimum[i]])
ans += sum(sorted([a[i] for i in range(n) if not is_minimum[i]])[:con_num-2])
print(ans)

# Fixed version:
import sys
sys.setrecursionlimit(10**6)

def input():
	return sys.stdin.readline()[:-1]

n, m = map(int, input().split())
a = list(map(int, input().split()))
adj = [[] for _ in range(n)]
for _ in range(m):
	x, y = map(int, input().split())
	adj[x].append(y)
	adj[y].append(x)

root = [-1 for _ in range(n)]
is_minimum = [False for _ in range(n)]

def dfs(x, r):
	global mi, mi_ind
	if a[x] < mi:
		mi = a[x]
		mi_ind = x
	for v in adj[x]:
		if root[v] < 0:
			root[v] = r
			dfs(v, r)
	return mi_ind

for i in range(n):
	if root[i] < 0:
		mi, mi_ind = 10**10, -1
		root[i] = i
		is_minimum[dfs(i, i)] = True

con_num = len(set(root))
#print(con_num)
if (con_num-1) * 2 > n:
	print("Impossible")
	sys.exit()
elif con_num == 1:
	print(0)
	sys.exit()
ans = sum([a[i] for i in range(n) if is_minimum[i]])
ans += sum(sorted([a[i] for i in range(n) if not is_minimum[i]])[:con_num-2])
print(ans)