Problem:
Score : 600 points

Problem Statement

You are given a forest with N vertices and M edges. The vertices are numbered 0 through N-1.
The edges are given in the format (x_i,y_i), which means that Vertex x_i and y_i are connected by an edge.

Each vertex i has a value a_i.
You want to add edges in the given forest so that the forest becomes connected.
To add an edge, you choose two different vertices i and j, then span an edge between i and j.
This operation costs a_i + a_j dollars, and afterward neither Vertex i nor j can be selected again.

Find the minimum total cost required to make the forest connected, or print Impossible if it is impossible.

Constraints

1 ≤ N ≤ 100,000

0 ≤ M ≤ N-1

1 ≤ a_i ≤ 10^9

0 ≤ x_i,y_i ≤ N-1

The given graph is a forest.

All input values are integers.

Input

Input is given from Standard Input in the following format:

N M
a_0 a_1 .. a_{N-1}
x_1 y_1
x_2 y_2
:
x_M y_M

Output

Print the minimum total cost required to make the forest connected, or print Impossible if it is impossible.

Sample Input 1

7 5
1 2 3 4 5 6 7
3 0
4 0
1 2
1 3
5 6

Sample Output 1

7

If we connect vertices 0 and 5, the graph becomes connected, for the cost of 1 + 6 = 7 dollars.

Sample Input 2

5 0
3 1 4 1 5

Sample Output 2

Impossible

We can't make the graph connected.

Sample Input 3

1 0
5

Sample Output 3

0

The graph is already connected, so we do not need to add any edges.

Solution in Ruby (2.3.3) (judge verdict: Time Limit Exceeded)

require 'pp'
# IS_DEBUG = true
IS_DEBUG = false
def dputs str
	if(IS_DEBUG)
		puts str
	end
end
def dpp str
	if(IS_DEBUG)
		pp str
	end
end


INT_MAX = 1000000001

N,M=gets.chomp.split(' ').map{|n| n.to_i}
A=Array.new(100000) # 0始まり100000-1まで
A=gets.chomp.split(' ').map{|n| n.to_i}
# ツリー情報
T=Array.new(100000).map{|n| nil} # 0始まり100000-1まで
# 得点情報
C=Array.new(100000).map{|n| nil} # 0始まり100000-1まで

ret_flg = false

if(M==0)
	if(N==1)
		puts "0"
	else
		puts "Impossible"
	end
	ret_flg = true
end

def add(x,y)
	i=0
	while(i<100000)
		tr = T[i]
		tr_c = C[i]
		if(tr.nil?)
			T[i] = [x,y]
			break
		end
		if(tr.any?{|i| i==x})
			tr << y
			merge(y)
			break
		end
		if(tr.any?{|i| i==y})
			tr << x
			merge(x)
			break
		end
		i+=1
	end
end
def merge(x)
	merge_i = nil
	i=0
	while(i<100000)
		tr = T[i]
		if(tr.nil?)
			break
		end
		if(tr.any?{|n| n==x})
			if(merge_i.nil?)
				merge_i = i
			else
				T[merge_i] += T[i].reject{|i| i==x}
				T.delete_at(i)
			end
		end
		i+=1
	end
end
def make_c
	i=0
	c_i=0
	while(i<100000)
		tr = T[i]
		if(tr.nil?)
			break
		end
		tr.each{|j|
			if(C[i].nil?)
				C[i] = [A[j]]
			else
				C[i] << A[j]
			end
		}
		i+=1
	end
end

def count_t
	i=0
	while(i<100000)
		tr = T[i]
		if(tr.nil?)
			break
		end
		i+=1
	end
	return i
end

if(!ret_flg)
	# 木の接点の得点リストを構成
	V = Array.new(N){|i| i}
	1.upto(M){|i|
		x,y=gets.chomp.split(' ').map{|n| n.to_i}
		add(x,y)
		V.delete(x)
		V.delete(y)
	}
	# 残りの点だけの木を追加

	make_c

	tind = T.index(nil)
	V.each{|i|
		T[tind] = [i]
		C[tind] = [A[i]]
		tind += 1
	}

	# i=0
	# while(i<100000)
	# 	if(C[i].nil?)
	# 		break
	# 	end
	# 	C[i].sort!
	# 	i+=1
	# end

	# Tを要素数の多さ順にソート
	i=0
	while(i<100000)
		if(C[i].nil?)
			break
		end
		i+=1
	end
	i-=1
	t = T[0..i]
	c = C[0..i]
	t.sort!{|i,j| j.size <=> i.size}
	c.sort!{|i,j| j.size <=> i.size}
	T[0..i] = t
	C[0..i] = c

	if(IS_DEBUG)
		puts "T is "
		i=0
		while(i<100000)
			if(T[i].nil?)
				break
			end
			pp T[i]
			i+=1
		end
		puts "C is "
		i=0
		while(i<100000)
			if(C[i].nil?)
				break
			end
			pp C[i]
			i+=1
		end
	end

	if((count_t-1)*2 > N)
		puts "Impossible"
	else
		#各treeを付け加えていく．
		sum = 0
		i = 1
		while(i<100000)
			if(C[i].nil?)
				break
			end

			# find min
			min=INT_MAX
			min_ti = nil
			min_i = nil

			j=0
			# いまのtreeに付け加える最小値
			#i-1まで
			while(j<i)
				tmin = C[j].min
				if(!tmin.nil? && min > tmin)
					min=tmin
					min_ti = j
					min_i = C[j].index(tmin)
				end
				j+=1
			end
			dputs "tmin #{tmin}"
			dputs "min_ti #{min_ti}"
			dputs "min_i #{min_i}"
			#選択したので削除
			T[min_ti].delete_at(min_i)
			C[min_ti].delete_at(min_i)
			sum += min

			# いまのtreeの最小値
			min = C[i].min
			sum += min
			min_i=C[i].index(min)
			T[i].delete_at(min_i)
			C[i].delete_at(min_i)

			i+=1
		end

		if(IS_DEBUG)
			puts "T is "
			i=0
			while(i<100000)
				if(T[i].nil?)
					break
				end
				pp T[i]
				i+=1
			end
			puts "C is "
			i=0
			while(i<100000)
				if(C[i].nil?)
					break
				end
				pp C[i]
				i+=1
			end
		end

		puts sum
	end
end