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 Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.Collections;
import java.util.TreeSet;
import java.util.ArrayList;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        D solver = new D();
        solver.solve(1, in, out);
        out.close();
    }

    static class D {
        ArrayList<Integer>[] e;
        ArrayList<Q> qs;
        boolean[] vis;
        int[] a;

        void dfs(int x, Q q) {
            vis[x] = true;
            q.v.add(a[x]);
            for (int y : e[x]) {
                if (!vis[y]) {
                    dfs(y, q);
                }
            }
        }

        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int n = in.nextInt();
            int m = in.nextInt();
            a = new int[n];
            for (int i = 0; i < n; i++) {
                a[i] = in.nextInt();
            }
            e = new ArrayList[n];
            for (int i = 0; i < n; i++) {
                e[i] = new ArrayList<>();
            }

            for (int i = 0; i < m; i++) {
                int u = in.nextInt();
                int v = in.nextInt();
                e[u].add(v);
                e[v].add(u);
            }

            int cc = 0;
            qs = new ArrayList<>();
            vis = new boolean[n];
            for (int i = 0; i < n; i++) {
                if (!vis[i]) {
                    Q q = new Q(cc);
                    dfs(i, q);
                    cc++;
                    qs.add(q);
                }
            }

            for (Q q : qs) {
                Collections.sort(q.v);
                if (q.v.size() > cc - 1) {
                    q.v = new ArrayList<>(q.v.subList(0, cc - 1));
                }
            }

            TreeSet<Q> s = new TreeSet<>(qs);
            int left = 2 * (n - 1 - m);
            long ans = 0;
            while (left > 0) {
                if (s.isEmpty()) {
                    out.println("Impossible");
                    return;
                }
                Q q1 = s.pollFirst();
                ans = ans + q1.v.get(q1.p++);
                if (q1.p < q1.v.size()) {
                    s.add(q1);
                }

                left -= 1;
            }
            out.println(ans);
        }

        class Q implements Comparable<Q> {
            int id;
            ArrayList<Integer> v;
            int p;

            public Q(int id) {
                this.id = id;
                v = new ArrayList<>();
                p = 0;
            }


            public int compareTo(Q o) {
                if (this == o) return 0;
                int t = Integer.compare(v.get(p), o.v.get(o.p));
                if (t == 0) {
                    return id - o.id;
                }
                return t;
            }

        }

    }
}