This page is still under construction.

Parts of this page are still being built. What you see may change.

Connect the Forest

Time limit2sMemory limit256 MB

Summary
Given a weighted forest, add disjoint vertex pairs (each vertex used at most once) so the graph becomes connected, minimizing the sum of the paired values, or report Impossible.
Level

Medium7 of 10

Topics
Greedy, Sorting, Union-find, Graph
Solved
No attempts yet

Problem

You are given a forest with NN vertices and MM edges. The vertices are numbered 00 through N−1N-1. Each edge is given as (xi,yi)(x_i, y_i), meaning that vertices xix_i and yiy_i are connected by an edge.

Each vertex ii is assigned a value aia_i. You want to add edges to the given forest so that it becomes a single connected graph. To add an edge, choose two different vertices ii and jj and place an edge between them. This operation costs ai+aja_i + a_j dollars, and afterward neither vertex ii nor jj can be chosen again.

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

Input

Input is given in the following format:

NN MM

a0a_0 a1a_1 …\ldots aN−1a_{N-1}

x1x_1 y1y_1

…\ldots

xMx_M yMy_M

Output

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

Constraints

1≤N≤100 0001 \le N \le 100\,000, 0≤M≤N−10 \le M \le N-1, 1≤ai≤1091 \le a_i \le 10^9, 0≤xi,yi≤N−10 \le x_i,y_i \le N-1. The given graph is a forest. All input values are integers.

Hint

In Sample 1, connecting vertices 00 and 55 makes the graph connected, and the cost is 1+6=71 + 6 = 7.

In Sample 2, the graph cannot be connected.

In Sample 3, the graph is connected whether or not we do anything.

Examples3

  1. Example 1

    Input
    7 5
    1 2 3 4 5 6 7
    3 0
    4 0
    1 2
    1 3
    5 6
    
    Expected output
    7
    
  2. Example 2

    Input
    5 0
    3 1 4 1 5
    
    Expected output
    Impossible
    
  3. Example 3

    Input
    1 0
    5
    
    Expected output
    0