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 C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<int(n);++i)
#define rep1(i,n) for(int i=1;i<=int(n);++i)
#define all(a) a.begin(),a.end()
#define lb(s,x) lower_bound(all(s),x)-s.begin()
#define debug(x) cout<<#x<<": "<<x<<endl
#define INF 1e9
typedef long long ll;
typedef priority_queue<ll, vector<ll>, greater<ll> > PQ;

struct UnionFind{
    vector<int> par;
    vector<int> sizes;

    UnionFind(){}
    UnionFind(int n):par(n),sizes(n,1){ rep(i,n)par[i]=i; }

    //find root of x
    int find(int x){
        if(x!=par[x]) par[x] = find(par[x]);
        return par[x];
    }

    void unite(int x,int y){
        x = find(x);
        y = find(y);
        if(x == y) return;
        if(sizes[x] < sizes[y]) swap(x, y);
        par[y] = x;
        sizes[x] += sizes[y];
    }

    bool same(int x,int y){ return find(x) == find(y); }
    int size(int x){ return sizes[find(x)]; }
};


vector<ll> a;
vector<PQ> pq;
priority_queue<pair<ll,int>, vector<pair<ll,int>>, greater<pair<ll,int>>> choose;
UnionFind uf;


// pqのサイズがどちらかが0ならimpossible
void merge(int x,int y){
    x = uf.find(x);
    y = uf.find(y);
    if(pq[x].size() < pq[y].size()){
        swap(x,y);
    }

    // pq[x].size() >= pq[y].size()
    // merge x <- y
    while(pq[y].size()){
        pq[x].push(pq[y].top());
        pq[y].pop();
    }

    uf.unite(x,y);
    int z = uf.find(x);
    // ufのルートに合わせる
    if(z != x){
        pq[z].swap(pq[x]);
    }
}

int main(void){
    int n,m;
    cin>>n>>m;
    a.resize(n);
    // g.resize(n);
    uf = UnionFind(n);
    pq.resize(n);
    rep(i,n) {
        cin>>a[i];
        pq[i].push(a[i]);
    }
    rep(_,m){
        int x,y;
        cin>>x>>y;
        merge(x,y);
    }

    rep(i,n){
        if(pq[i].size()){
            choose.push({pq[i].top(), i});
        }
    }

    ll res = 0;

    while(choose.size()>=2){
        int x = choose.top().second; choose.pop();
        int y = choose.top().second; choose.pop();
        res += pq[x].top() + pq[y].top();
        pq[x].pop(); pq[y].pop();
        merge(x,y);
        int z = uf.find(x);
        if(pq[z].size()==0){
            if(uf.size(z) != n){
                cout<<"Impossible"<<endl;
                return 0;
            }
        }
        choose.push({pq[z].top(),z});
    }
    cout<<res<<endl;

    return 0;
}