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: Accepted)

#include <bits/stdc++.h>
typedef long long ll;
const int INF = 1e9;
const int MOD = 1e9+7;
const ll LINF = 1e18;
using namespace std;
 
#define dump(x)  cout << #x << " = " << (x) << endl;
#define YES(n) cout << ((n) ? "YES" : "NO"  ) << endl
#define Yes(n) cout << ((n) ? "Yes" : "No"  ) << endl
#define POSSIBLE(n) cout << ((n) ? "POSSIBLE" : "IMPOSSIBLE"  ) << endl
#define Possible(n) cout << ((n) ? "Possible" : "Impossible"  ) << endl
#define possible(n) cout << ((n) ? "possible" : "impossible"  ) << endl
 
#define SANKOU(n,a,b) cout << ((n) ? (#a) : (#b) ) << endl
 
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) for(int i=0;i<(n);++i)
#define REPR(i,n) for(int i=n;i>=0;i--)
 
#define FOREACH(x,a) for(auto& (x) : (a) )
 
#define WFA(d,v) REP(k,v)REP(i,v)REP(j,v)d[i][j]=min(d[i][j],d[i][k]+d[k][j])
 
#define SCOUT(x) cout<<(x)<<" "
#define ENDL cout<<endl
 
#define VECCIN(x) for(auto&youso_: (x) )cin>>youso_
#define VECIN2(x,y) REP(i,x.size())cin>>x[i]>>y[i]
#define VECCOUT(x) if(1){for(auto tt=x.begin();tt!=x.end();tt++){if(tt!=x.begin())cout<<" ";cout<<(*tt);}cout<<endl;}
 
#define ALL(obj) (obj).begin(),(obj).end()
 
#define EXIST(n,x) (find(ALL(n),x)!=n.end())
#define UNIQUE(obj) sort(ALL( obj )); obj.erase(unique(ALL(obj)),obj.end())
#define EN(x) if(1){cout<<#x<<endl;return 0;}
#define COUT(x) cout<<(x)<<endl
void CINT(){}
template <class Head,class... Tail>
void CINT(Head&& head,Tail&&... tail){
    cin>>head;
    CINT(move(tail)...);
}
#define CIN(...) int __VA_ARGS__;CINT(__VA_ARGS__)
#define LCIN(...) ll __VA_ARGS__;CINT(__VA_ARGS__)
#define SCIN(...) string __VA_ARGS__;CINT(__VA_ARGS__)
 
template <class T = ll>
T IN(){T x;cin>>x;return (x);}
 
template <class Head>
void VT(Head head){}
template <class Head,class Seco,class... Tail>
void VT(Head&& head,Seco&& seco,Tail&&... tail){
    seco.resize(head);
    VT(head,move(tail)...);
}
void VT2(){}
template <class Head,class... Tail>
void VT2(Head&& head,Tail&&... tail){
  VECCIN(head);
  VT2(move(tail)...);
}
 
template <class Head>
void VT3(Head&& head){}
template <class Head,class Seco,class... Tail>
void VT3(Head&& head,Seco&& seco,Tail&&... tail){
  seco[head]=IN();
  VT3(head,move(tail)...);
}
 
#define VC1(n,...) V __VA_ARGS__;VT(n,__VA_ARGS__);VT2(__VA_ARGS__); //aaabbbccc
#define VC2(n,...) V __VA_ARGS__;VT(n,__VA_ARGS__);REP(i,n)VT3(i,__VA_ARGS__); //abcabcabc
 
//#include <boost/multiprecision/cpp_int.hpp>
//using namespace boost::multiprecision; // cpp_int
 
#define P pair<ll,ll>
#define V vector<ll>
#define M map<ll,ll>
#define S set<ll>
 
#define pb(a) push_back(a)
#define mp make_pair
class UnionFind{
public:
    V par,rank,num;
    vector<list<int>> child;
    int count;//今までに何回併合されたか
    UnionFind(int n);
    int Size();//今までに何回併合されたか
    int Find(int x);//xの親が何か
    bool Unite(int x,int y);//xとyの併合
    bool Same(int x,int y);//xとyが同じ集合に含まれるか
    int Count(int x);//xと同じ集合に含まれる要素の数
    list<int> Child(int x);//xと同じ集合に含まれる要素の列挙
};

UnionFind::UnionFind(int n){
    par.resize(n);num=V(n,1);rank=V(n,0);
    child.resize(n);
    count=0;
    REP(i,n){par[i]=i;child[i].pb(i);}
}

int UnionFind::Find(int x){
    return (par[x]==x?x:Find(par[x]));
}

bool UnionFind::Unite(int x,int y){
    x=Find(x);y=Find(y);
    
    if(x==y)return false;
    if(rank[x]<rank[y]){
      par[x]=y;
      num[y]+=num[x];
      child[y].splice(child[y].end(),move(child[x]));
      num[x]=0;
    }
    else{
      par[y]=x;
      num[x]+=num[y];
      child[x].splice(child[x].end(),move(child[y]));
      num[y]=0;
      if(rank[x]==rank[y])rank[x]++;
    }
    count++;
    return true;
}

bool UnionFind::Same(int x,int y){
    return (Find(x)==Find(y));
}

int UnionFind::Count(int x){
  return num[Find(x)];
}

int UnionFind::Size(){
    return par.size()-count;
}

list<int> UnionFind::Child(int x){
  return child[Find(x)];
}

int main(){
 
  CIN(n,m);
  V a(n);V x(m);V y(m);
  REP(i,n)scanf("%d",&a[i]);
  REP(i,m)scanf("%d%d",&x[i],&y[i]);
  UnionFind uni(n);
  REP(i,m)uni.Unite(x[i],y[i]);
 
    if(uni.Size()==1)EN(0);
  if((uni.Size()-1)*2>n)EN(Impossible);
  V flag(n,false);
  REP(i,n)
    if(uni.Find(i)==i){
        list<int> ch = uni.Child(i);
        int mi=MOD;int ind = 0;
        FOREACH(xx,ch)
            if(mi>a[xx]){
                mi=a[xx];
                ind=xx;
            }
        flag[ind]=true;
    }
    priority_queue<P,vector<P>,greater<P>> que;
    REP(i,n)if(!flag[i])que.push(mp(a[i],i));
    int t = (uni.Size()-1)*2-uni.Size();
    REP(i,t){
        flag[que.top().second]=true;
        que.pop();
    }
    ll ans = 0;
    REP(i,n)if(flag[i])ans+=a[i];
    COUT(ans);
 
 
  return 0;
}