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 Haskell (GHC 7.10.3) (judge verdict: Accepted)

import qualified Data.ByteString.Char8 as BC
import qualified Data.IntMap as M
import Data.Maybe (fromJust)
import Data.List
import Data.Array.IO
import Control.Monad

type Arr = IOUArray Int Int
type ArrB = IOUArray Int Bool
type Edge = (Int, Int)
type Graph = M.IntMap [Int]

main = do
  [n,m] <- map read . words <$> getLine :: IO [Int]
  a <- getIntListBC
  edge <- if m > 0
            then map (\[x,y] -> (x,y)) <$> getInt2DListBC
            else return [] :: IO [(Int, Int)]
  parent <- newArray (-1, n-1) 0 :: IO Arr
  mark <- newArray (1, n) False :: IO ArrB
  let weight = sort $ zip a [0..n-1]
  let graph = foldl' makeGraph M.empty edge
  foldM (solve graph) parent [0..n-1]
  l <- length . group . sort . tail <$> getElems parent
  weight' <- select parent mark l weight
  single <- length . filter (==1) . map length . group . sort . tail <$> getElems parent
  ans1 <- if l > 1 then readArray parent (-1) else return 0
  let ans2 = sum . map fst . take (l-2) $ weight'
  if single > (n - single) - (l - single - 1)*2
    then putStrLn "Impossible"
    else print $ ans1 + ans2

solve :: Graph -> Arr -> Int -> IO Arr
solve graph arr i = dfs graph i arr i

--DFS : 前pre 現在i
dfs :: Graph -> Int -> Arr -> Int -> IO Arr
dfs graph p mark i = do
  n <- readArray mark i
  if n /= 0
    then return mark
    else do
      writeArray mark i $ p+1
      let ne = M.lookup i graph
      if ne /= Nothing
        then return =<< foldM (dfs graph p) mark $ fromJust ne
        else return mark

--重み無し隣接リスト
makeGraph :: Graph -> Edge -> Graph
makeGraph graph (x, y) =
  let nX = M.lookup x graph
      nY = M.lookup y graph
      vX = if nX == Nothing
             then M.insert x [y] graph
             else M.insert x (y : (fromJust nX)) graph
  in if nY == Nothing
       then M.insert y [x] vX
       else M.insert y (x : (fromJust nY)) vX

select :: Arr -> ArrB -> Int -> [(Int, Int)] -> IO [(Int, Int)]
select _      _    _ []          = return []
select _      _    0 s           = return s
select parent mark n ((w, v):ws) = do
  p <- readArray parent v
  used <- readArray mark p
  if used
    then do
      l <- select parent mark n ws
      return $ (w, v):l
    else do
      writeArray mark p True
      s <- readArray parent (-1)
      writeArray parent (-1) $ s + w
      select parent mark (n-1) ws

bsToInt :: BC.ByteString -> Int
bsToInt = fst . fromJust . BC.readInt

getIntListBC :: IO [Int]
getIntListBC = map bsToInt . BC.words <$> BC.getLine

getInt2DListBC :: IO [[Int]]
getInt2DListBC = map (map bsToInt . BC.words) . BC.lines <$> BC.getContents