atcoder#APC001D. Forest

Forest

Score : 600600 points

Problem Statement

You are given a forest with NN vertices and MM edges. The vertices are numbered 00 through N1N-1. The edges are given in the format (xi,yi)(x_i,y_i), which means that Vertex xix_i and yiy_i are connected by an edge.

Each vertex ii has a value aia_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 ii and jj, then span an edge between ii and jj. This operation costs ai+aja_i + a_j dollars, and afterward neither Vertex ii nor jj can be selected again.

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

Constraints

  • 1N100,0001 \leq N \leq 100,000
  • 0MN10 \leq M \leq N-1
  • 1ai1091 \leq a_i \leq 10^9
  • 0xi,yiN10 \leq x_i,y_i \leq N-1
  • The given graph is a forest.
  • All input values are integers.

Input

Input is given from Standard Input in the following format:

NN MM

a0a_0 a1a_1 .... aN1a_{N-1}

x1x_1 y1y_1

x2x_2 y2y_2

::

xMx_M yMy_M

Output

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

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

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

5 0
3 1 4 1 5
Impossible

We can't make the graph connected.

1 0
5
0

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