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"

#define ALL(g) (g).begin(),(g).end()
#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i,n) REP(i,0,n)
#define RREP(i, x, n) for(int i = x; i >= n; i--)
#define rrep(i, n) RREP(i,n,0)
#define pb push_back

using namespace std;

using ll = long long;
using P = pair<int,int>;
using Pl = pair<ll,int>;

const int mod=1e9+7,INF=1<<30;
const double EPS=1e-12,PI=3.1415926535897932384626;
const ll lmod = 1e9+7,LINF=1LL<<60;
const int MAX_N = 100005;

struct UnionFindTree{
  int par[MAX_N],rank[MAX_N],size[MAX_N];
  UnionFindTree(int N){
    fill(rank,rank+N,0);
    fill(size,size+N,1);
    rep(i,N) par[i]=i;
  }
  int find(int x){
    if(par[x]==x) return x;
    else  return par[x]=find(par[x]);
  }
  void unite(int x,int y){
    x=find(x);
    y=find(y);
    if(x==y)  return ;
    if(rank[x]<rank[y]){
      par[x]=y;
    }else{
      par[y]=x;
      if(rank[x]==rank[y])  ++rank[x];
    }
    size[x]=size[y]=size[x]+size[y];
  }
  bool same(int x,int y){
    return find(x)==find(y);
  }
};

priority_queue<Pl,vector<Pl>,greater<Pl>> p;
priority_queue<ll,vector<ll>,greater<ll>> q;
ll a[MAX_N];
bool used[MAX_N];
vector<ll> argcost[MAX_N];

int main(){
  int N,M; cin >> N >> M;
  UnionFindTree uft(N);
  rep(i,N) scanf("%lld",a+i);
  rep(i,M){
    int x,y; scanf("%d%d",&x,&y);
    uft.unite(x,y);
  }
  if(N/2+M<N-1){
    cout << "Impossible" << endl;
    return 0;
  }
  int first = uft.find(0);
  fill(used,used+N,false);
  rep(i,N){
    int j = uft.find(i);
    if(j==first) q.push(a[i]);
    else{
      p.push(Pl(a[i],j));
      argcost[j].pb(i);
    }
  }
  ll ans = 0LL;
  while(!q.empty()&&!p.empty()){
    Pl pl = p.top(); p.pop();
    if(uft.same(pl.second,first)) continue;
    used[pl.second] = true;
    ll ql = q.top(); q.pop();
    ans += (pl.first + ql);
    uft.unite(first,pl.second);
    // cout << first << " - " << pl.second << " -> " << pl.first+ql << endl;
    for(auto ac:argcost[pl.second]) if(!used[ac]) q.push(a[ac]);
  }
  bool ok = true;
  rep(i,N) ok &= (uft.same(first,i));
  if(ok) cout << ans << endl;
  else cout << "Impossible" << endl;
  return 0;
}