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.AbstractCollection;
import java.util.Vector;
import java.util.PriorityQueue;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.Comparator;
import java.util.Stack;
import java.io.InputStream;

/**
 * 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;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskD solver = new TaskD();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskD {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt(), m = in.nextInt();
            int a[] = new int[n];
            for (int i = 0; i < n; i++) {
                a[i] = in.nextInt();
            }
            if (n == 1) {
                out.println(0);
                return;
            }
            Stack<Integer> gp[] = (Stack<Integer>[]) new Stack[n];
            for (int i = 0; i < n; i++) {
                gp[i] = new Stack<Integer>();
            }
            int b[] = new int[n];
            for (int i = 0; i < m; i++) {
                int l = in.nextInt(), r = in.nextInt();
                gp[l].push(r);
                gp[r].push(l);
                b[l] = 1;
                b[r] = 1;
            }
            int ct = 0;
            for (int i = 0; i < n; i++) {
                if (b[i] == 1) {
                    ct++;
                }
            }
            if (ct != n) {
                out.println("Impossible");
                return;
            }
            PriorityQueue<tp> minp = new PriorityQueue<tp>(new tp());
            Stack<Integer> s = new Stack<Integer>();
            boolean v[] = new boolean[n];
            int ccom = 0;
            for (int i = 0; i < n; i++) {
                if (!v[i]) {
                    v[i] = true;
                    s.add(i);
                    minp.add(new tp(a[i], ccom));
                    while (!s.isEmpty()) {
                        int curr = s.peek();
                        if (gp[curr].isEmpty()) {
                            s.pop();
                        } else {
                            int next = gp[curr].pop();
                            if (!v[next]) {
                                v[next] = true;
                                s.push(next);
                                minp.add(new tp(a[next], ccom));
                            }
                        }
                    }
                    ccom++;
                }
            }
            long ans = 0;
            boolean us[] = new boolean[n];
            ct = 0;
            PriorityQueue<tp> minn = new PriorityQueue<tp>(new tp());
            while (ct != ccom - 1) {
                tp curr = null, next = null;
                if (minn.isEmpty()) {
                    curr = minp.poll();
                } else {
                    curr = minn.poll();
                }
                if (minp.isEmpty()) {
                    break;
                }
                while (!minp.isEmpty()) {
                    next = minp.poll();
                    if (curr.c != next.c) {
                        break;
                    }
                    minn.add(next);
                }
                if (!us[(int) curr.c] || !us[(int) next.c]) {
                    ans += curr.v + next.v;
                    us[(int) curr.c] = true;
                    us[(int) next.c] = true;
                    ct++;
                }
            }
            if (ct == ccom - 1) {
                out.println(ans);
            } else {
                out.println("Impossible");
            }
        }

        class tp implements Comparator<tp> {
            long v;
            long c;

            tp() {
            }

            tp(int val, int com) {
                v = val;
                c = com;
            }


            public int compare(tp t1, tp t2) {
                return (int) (t1.v - t2.v);
            }

        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputReader.SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1)
                throw new InputMismatchException();

            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }

                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = read();

            while (isSpaceChar(c))
                c = read();

            int sgn = 1;

            if (c == '-') {
                sgn = -1;
                c = read();
            }

            int res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            while (!isSpaceChar(c));

            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            if (filter != null)
                return filter.isSpaceChar(c);
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }
}