stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include <iostream>

#include <cassert>

#include <algorithm>

#include <vector>

#include <queue>

#include <set>

using namespace std;

int N,M;

int T[100005];

int A[100005];

long long rez;

multiset<int> G[100005];

int fi(int nod){

    if(T[nod] <= 0){

        return nod;

    }

    return T[nod] = fi(T[nod]);

}

void u(int x,int y,bool baga){

    x = fi(x);

    y = fi(y);

    if(baga){

        rez += *G[x].begin();G[x].erase(G[x].begin());

        rez += *G[y].begin();G[y].erase(G[y].begin());

        T[x]++;

        T[y]++;

    }

    if(T[x] < T[y]){

        for(auto it:G[y]){

            G[x].insert(it);

        }

        T[x] += T[y];

        T[y] = x;

    }

    else{

        for(auto it:G[x]){

            G[y].insert(it);

        }

        T[y] += T[x];

        T[x] = y;

    }

}

class cmp{

    public:

    bool operator() (int a,int b){

        return T[a] > T[b];

    }

};

priority_queue<int,vector<int>,cmp> H;

int main(){

    cin >> N >> M;

    for(int i = 1;i <= N;i++){

        cin >> A[i];

        T[i] = -1;

        G[i].insert(A[i]);

    }

    for(int i = 1;i <= M;i++){

        int x,y;

        cin >> x >> y;

        x++;

        y++;

        u(x,y,0);

    }

    for(int i = 1;i <= N;i++){

        if(T[i] <= 0){

            H.push(i);

        }

    }

    rez = 0;

    while(M < N - 1){

        if(H.size() < 2){

            cout << "Impossible";

            return 0;

        }

        int a = H.top();H.pop();

        int b = H.top();H.pop();

        if(T[b] == 0){

            cout << "Impossible";

            return 0;

        }

        u(a,b,1);

        H.push(fi(a));

        M++;

    }

    cout << rez;

    return 0;

}


// Optimized solution to the same problem (faster):
#include <cstdio>

#include <vector>

#include <algorithm>



using namespace std;



const int NMAX = 1e5;



int n,m;

vector<int> graph[NMAX + 5];

int val[NMAX + 5];

bool viz[NMAX + 5];



int dfs(int nod){

	viz[nod] = 1;

	int ans = nod;

	for(auto it:graph[nod]){

		if(!viz[it]){

			int tmp = dfs(it);

			if(val[tmp] < val[ans]){

				ans = tmp;

			}

		}

	}

	return ans;

}



int main(){

	

	scanf("%d %d",&n,&m);

	

	for(int i = 0;i < n;i++){

		scanf("%d",&val[i]);

	}

	

	for(int i = 0;i < m;i++){

		int x,y;

		scanf("%d %d",&x,&y);

		graph[x].push_back(y);

		graph[y].push_back(x);

	}

	

	if(2 * (n - m - 1) > n){

		printf("Impossible");

		return 0;

	}

	

	if(m == n - 1){

		printf("0");

		return 0;

	}

	

	long long ans = 0;

	

	for(int i = 0;i < n;i++){

		if(!viz[i]){

			int tmp = dfs(i);

			ans += val[tmp];

			val[tmp] = 1 << 30;

		}

	}

	

	sort(val,val + n);

	

	for(int i = 0;i < n - m - 2;i++){

		ans += val[i];

	}

	

	printf("%lld",ans);



	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>



typedef long long LL;



const int MAXN = 1e5 + 5;



int n, m, w[MAXN];



std::multiset<int> st[MAXN];

std::set<std::pair<int, int> > spec;



int fa[MAXN], part;



int Find(int x) {

    return fa[x] == x ? x : fa[x] = Find(fa[x]);

}



void merge(int u, int v) {

    if (Find(u) == Find(v)) return;

    if (st[fa[u]].size() > st[fa[v]].size()) std::swap(u, v);

    --part;

    for (int i: st[fa[u]]) {

        st[fa[v]].insert(i);

    }

    st[fa[u]].clear();

    if (!st[fa[v]].empty()) spec.insert(std::make_pair(st[fa[v]].size(), fa[v]));

    fa[fa[u]] = fa[v];

}



int main() {

    scanf("%d%d", &n, &m); part = n;

    for (int i = 0; i < n; ++i) {

        fa[i] = i;

        scanf("%d", w + i);

        st[i].insert(w[i]);

        spec.insert(std::make_pair(1, i));

    }

    

    for (int i = 1; i <= m; ++i) {

        static int u, v;

        

        scanf("%d%d", &u, &v);

        spec.erase(spec.find(std::make_pair(st[Find(u)].size(), Find(u))));

        spec.erase(spec.find(std::make_pair(st[Find(v)].size(), Find(v))));

        merge(u, v);

    }

    

    LL ans = 0;

    

    while (part > 1) {

        if (spec.size() < 2) {

            puts("Impossible");

            return 0;

        }

        auto x = *--spec.end();

        spec.erase(--spec.end());

        

        auto y = *--spec.end();

        spec.erase(--spec.end());

        

        int u = x.second, v = y.second;

        ans += *st[Find(u)].begin() + *st[Find(v)].begin();

        

        st[Find(u)].erase(st[Find(u)].begin());

        st[Find(v)].erase(st[Find(v)].begin());

        

        merge(u, v);

    }

    

    printf("%lld\n", ans);

    

    return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>

#define F(i,a,b) for(int i=(a);i<=(b);++i)

using namespace std;

typedef long long ll;



int n,m;

int a[100001],b[100001],t;

int fa[100001];

int ff(int x){return fa[x]?fa[x]=ff(fa[x]):x;}

ll Sum;



int main(){

	int x,y;

	scanf("%d%d",&n,&m);

	if(m==n-1) return puts("0"),0;

	F(i,1,n) scanf("%d",a+i), Sum+=a[i];

	F(i,1,m){

		scanf("%d%d",&x,&y);

		x=ff(x+1); y=ff(y+1);

		Sum-=max(a[x],a[y]);

		b[++t]=max(a[x],a[y]);

		if(a[x]>a[y]) fa[x]=y;

		else fa[y]=x;

	}

	if(t<n-m-2) return puts("Impossible"),0;

	sort(b+1,b+t+1);

	F(i,1,n-m-2) Sum+=b[i];

	printf("%lld",Sum);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<cmath>

#include<cstdio>

#include<algorithm>

#include<queue>

using namespace std;

typedef long long LL;

int n,m;

LL a[100000+10];

int par[100000+10];

priority_queue<LL> mini[100000+10];



int fpar(int x){

	if (par[x]==-1)return x;

	par[x]=fpar(par[x]);

	return par[x];

}

priority_queue <LL > pq1;

LL ans=0;

int main(){

	scanf("%d%d",&n,&m);

	for (int f1=0;f1<n;f1++)scanf("%lld",&a[f1]);

	for (int f1=0;f1<n;f1++){par[f1]=-1;}

	for (int f1=0;f1<m;f1++){

		int i,j;

		scanf("%d%d",&i,&j);

		i=fpar(i);

		j=fpar(j);

		par[j]=i;

	}

	for (int f1=0;f1<n;f1++){

		int j=fpar(f1);

		mini[j].push(-a[f1]);

	}

	if (n==1+m){

		printf("0\n");

		return 0;

	}

	for (int f1=0;f1<n;f1++){

		if (par[f1]==-1){

			ans-=mini[f1].top();

			mini[f1].pop();

			while (!mini[f1].empty()){

				pq1.push(mini[f1].top());

				mini[f1].pop();

			}	

		}

	}

	

	if (int(pq1.size())>=n-m-2){

		for (int f1=1;f1<=n-m-2;f1++){

			ans-=pq1.top();

			pq1.pop();

		}

		printf("%lld\n",ans);

	} else printf("Impossible\n");

}

//

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>

#define F(i,a,b) for(int i=(a);i<=(b);++i)

using namespace std;

typedef long long ll;



int n,m;

int a[100001],b[100001],t;

int fa[100001];

int ff(int x){return fa[x]?fa[x]=ff(fa[x]):x;}

ll Sum;



int main(){

	int x,y;

	scanf("%d%d",&n,&m);

	if(m==n-1) return puts("0"),0;

	F(i,1,n) scanf("%d",a+i), Sum+=a[i];

	F(i,1,m){

		scanf("%d%d",&x,&y);

		x=ff(x+1); y=ff(y+1);

		Sum-=max(a[x],a[y]);

		b[++t]=max(a[x],a[y]);

		if(a[x]>a[y]) fa[x]=y;

		else fa[y]=x;

	}

	if(t<n-m-2) return puts("Impossible"),0;

	sort(b+1,b+t+1);

	F(i,1,n-m-2) Sum+=b[i];

	printf("%lld",Sum);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <algorithm>

#include <iostream>

#include <cstdlib>

#include <cstdio>

#include <string>

#include <vector>

using namespace std;

const int MAXN = 100000;



int n, m;

int fa[MAXN+5];

int val[MAXN+5];

vector <int> blk[MAXN+5];

vector <int> ansEdge;

long long int ans;



int SetFind(int x)

{

    if(fa[x] != x)

        return fa[x] = SetFind(fa[x]);

    return fa[x];

}



void SetInit(int n)

{

    for(int i = 1; i <= n; i++)

        fa[i] = i;

}



int main()

{

    int blkcnt;



    cin >> n >> m ;

    SetInit(n);

    blkcnt = n;



    for(int i = 1; i <= n; i++)

    {

        cin >> val[i] ;

        //blk[i].push_back(val[i]);

    }





    for(int i = 1; i <= m; i++)

    {

        int u, v;

        cin >> u >> v;

        u++, v++;

        if(SetFind(u) != SetFind(v))

            fa[SetFind(u)] = SetFind(v), blkcnt--;

    }



    for(int i = 1; i <= n; i++)

        blk[SetFind(i)].push_back(val[i]);

/*

    for(int i = 1; i <= n; i++)

    {

        if(!blk[i].empty())

            cout << blk[i].size() << endl ;

    }

*/

    if(blkcnt <= 1)

    {

        cout << 0 << endl ;

        return 0;

    }



    blkcnt -= 2;



    for(int i = 1; i <= n; i++)

        if(!blk[i].empty())

        {

            sort(blk[i].begin(), blk[i].end());

            ans += blk[i][0];

            for(int k = 1; k < blk[i].size(); k++)

                ansEdge.push_back(blk[i][k]);

        }



    if(ansEdge.size() < blkcnt)

    {

        cout << "Impossible" << endl;

        return 0;

    }



    sort(ansEdge.begin(), ansEdge.end());



    for(int i = 0; i < blkcnt; i++)

        ans += ansEdge[i] ;



    cout << ans << endl ;

    return 0;

}


// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>

#define F(i,a,b) for(int i=(a);i<=(b);++i)

using namespace std;

typedef long long ll;



int n,m;

int a[100001],b[100001],t;

int fa[100001];

int ff(int x){return fa[x]?fa[x]=ff(fa[x]):x;}

ll Sum;



int main(){

	int x,y;

	scanf("%d%d",&n,&m);

	if(m==n-1) return puts("0"),0;

	F(i,1,n) scanf("%d",a+i), Sum+=a[i];

	F(i,1,m){

		scanf("%d%d",&x,&y);

		x=ff(x+1); y=ff(y+1);

		Sum-=max(a[x],a[y]);

		b[++t]=max(a[x],a[y]);

		if(a[x]>a[y]) fa[x]=y;

		else fa[y]=x;

	}

	if(t<n-m-2) return puts("Impossible"),0;

	sort(b+1,b+t+1);

	F(i,1,n-m-2) Sum+=b[i];

	printf("%lld",Sum);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>



using namespace std;



#define SZ(x) ((int)x.size())



const int N = 100005;



int n, m;

int a[N];

int fa[N];



vector<int> val[N];



int getfa(int x) {

	return fa[x] == x ? x : fa[x] = getfa(fa[x]);

}



void merge(int x, int y) {

	x = getfa(x);

	y = getfa(y);

	if(SZ(val[x]) < SZ(val[y])) {

		swap(x, y);

	}

	for(auto v: val[y]) {

		val[x].push_back(v);

	}

	val[y].clear();

	fa[y] = x;

}



void input()

{

	scanf("%d%d", &n, &m);

	for(int i = 1; i <= n; i++) {

		scanf("%d", &a[i]);



		val[i].push_back(a[i]);

		fa[i] = i;

	}

	for(int i = 1; i <= m; i++) {

		int x, y;

		scanf("%d%d", &x, &y);

		x++, y++;

		merge(x, y);

	}

}



void solve()

{

	long long ans = 0;



	int cnt = (n - 1 - m) * 2;



	if(cnt == 0) {

		puts("0");

		return;

	}



	vector<int> vec;

	for(int i = 1; i <= n; i++) {

		if(fa[i] == i) {

			sort(val[i].begin(), val[i].end());

			ans += val[i][0];

			cnt--;

			for(int j = 1; j < SZ(val[i]); j++) {

				vec.push_back(val[i][j]);

			}

		}

	}



	if(SZ(vec) < cnt) {

		puts("Impossible");

		return;

	}



	sort(vec.begin(), vec.end());

	for(int i = 0; i < cnt; i++) {

		ans += vec[i];

	}

	cout << ans << endl;

}



int main()

{

	input();

	solve();



	return 0;

}


// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>

#define F(i,a,b) for(int i=(a);i<=(b);++i)

using namespace std;

typedef long long ll;



int n,m;

int a[100001],b[100001],t;

int fa[100001];

int ff(int x){return fa[x]?fa[x]=ff(fa[x]):x;}

ll Sum;



int main(){

	int x,y;

	scanf("%d%d",&n,&m);

	if(m==n-1) return puts("0"),0;

	F(i,1,n) scanf("%d",a+i), Sum+=a[i];

	F(i,1,m){

		scanf("%d%d",&x,&y);

		x=ff(x+1); y=ff(y+1);

		Sum-=max(a[x],a[y]);

		b[++t]=max(a[x],a[y]);

		if(a[x]>a[y]) fa[x]=y;

		else fa[y]=x;

	}

	if(t<n-m-2) return puts("Impossible"),0;

	sort(b+1,b+t+1);

	F(i,1,n-m-2) Sum+=b[i];

	printf("%lld",Sum);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<cstdio>

#include<cctype>

#include<cstring>

#include<algorithm>

#include<vector>

using std::vector;

#define max(a,b) (a>b?a:b)

#define min(a,b) (a<b?a:b)

#define reg register

typedef long long ll;

inline int read(){

    reg int x=0,f=1;reg char ch=getchar();

    for(;!isdigit(ch);f=ch=='-'?-1:1,ch=getchar());

    for(;isdigit(ch);x=x*10+(ch^48),ch=getchar());

    return x*f;

}

const int MN=1e5+5;

int n,m;

class UFset{

	public:

		int par[MN];

		inline int find(int x){return x==par[x]?x:par[x]=find(par[x]);}

		inline void init(int x){for(reg int i=0;i<x;i++)par[i]=i;}

		inline void unite(int x,int y){x=find(x);y=find(y);par[y]=x;}

		inline bool isfa(int x){return find(x)==x;}

}S;

int v[MN],cnt,id[MN],a[MN];

vector<int>pts[MN];

int main(){

	n=read();m=read();S.init(n);if(n==m+1)return 0*puts("0");

	for(reg int i=0;i<n;i++)v[i]=read();

	for(reg int i=1;i<=m;i++)S.unite(read(),read());

	for(reg int i=0;i<n;i++)if(S.isfa(i))id[i]=++cnt,pts[cnt].push_back(v[i]);

	for(reg int i=0;i<n;i++)if(!S.isfa(i))pts[id[S.find(i)]].push_back(v[i]);

	if(n<((cnt-1)<<1))return 0*puts("Impossible");

	for(reg int i=1;i<=cnt;i++)std::sort(pts[i].begin(),pts[i].end());

	reg ll ans=0;reg int tot=0;

	for(reg int i=1;i<=cnt;i++)ans+=pts[i][0];

	for(reg int i=1;i<=cnt;i++)for(reg int j=1;j<pts[i].size();j++)a[++tot]=pts[i][j];

	std::sort(a+1,a+1+tot);

	for(reg int i=1;i<=cnt-2;i++)ans+=a[i];

	printf("%lld\n",ans);

    return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>

#define F(i,a,b) for(int i=(a);i<=(b);++i)

using namespace std;

typedef long long ll;



int n,m;

int a[100001],b[100001],t;

int fa[100001];

int ff(int x){return fa[x]?fa[x]=ff(fa[x]):x;}

ll Sum;



int main(){

	int x,y;

	scanf("%d%d",&n,&m);

	if(m==n-1) return puts("0"),0;

	F(i,1,n) scanf("%d",a+i), Sum+=a[i];

	F(i,1,m){

		scanf("%d%d",&x,&y);

		x=ff(x+1); y=ff(y+1);

		Sum-=max(a[x],a[y]);

		b[++t]=max(a[x],a[y]);

		if(a[x]>a[y]) fa[x]=y;

		else fa[y]=x;

	}

	if(t<n-m-2) return puts("Impossible"),0;

	sort(b+1,b+t+1);

	F(i,1,n-m-2) Sum+=b[i];

	printf("%lld",Sum);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<cstdio>

#include<cstring>

#include<algorithm>

#include<queue>

#define ll long long

#define MN 200005

using namespace std;

inline ll in(){

    ll x=0;bool f=0;char c;

    for (;(c=getchar())<'0'||c>'9';f=c=='-');

    for (x=c-'0';(c=getchar())>='0'&&c<='9';x=(x<<3)+(x<<1)+c-'0');

    return f?-x:x;

}

priority_queue<ll,vector<ll>,greater<ll> >q;

struct edge{

	int to,nxt;

}e[MN<<1];

ll a[MN],mn[MN],res;

int num[MN],h[MN];

int n,m,x,y,cnt,tot,ct;

bool vis[MN],tmp[MN];

inline void ins(int x,int y){

	e[++cnt].to=y;e[cnt].nxt=h[x];h[x]=cnt;

}

inline void dfs(int u){

	mn[tot]=min(mn[tot],a[u]);

	for (int i=h[u];i;i=e[i].nxt){

		int v=e[i].to;

		if (vis[v]) continue;

		vis[v]=1;num[v]=tot;dfs(v);

	}

}

int main()

{

	n=in();m=in();

	memset(mn,0x3f,sizeof(mn));

	for (int i=1;i<=n;++i) a[i]=in();

	for (int i=1;i<=m;++i){

		x=in()+1;y=in()+1;

		ins(x,y);ins(y,x);

	}

	for (int i=1;i<=n;++i)

	if (!vis[i]){

		vis[i]=1;num[i]=(++tot);dfs(i);

	}

	if (tot==1) {printf("0");return 0;}

	for (int i=1;i<=tot;++i) res+=1ll*mn[i];

	for (int i=1;i<=n;++i){

		if (!tmp[num[i]]&&a[i]==mn[num[i]]) {tmp[num[i]]=1;continue;}

		else q.push(a[i]);

	}

	while (!q.empty()){

		if (ct==tot-2) break;

		ll x=q.top();q.pop();++ct;res+=1ll*x;

	} 

	if (ct==tot-2) printf("%lld",res);else printf("Impossible");

	return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>

using namespace std;

typedef long long ll;

int p[100100];

ll ans;

int n, m;

pair<ll, int> a[100100];

bool group_used[100100];

bool used[100100];

int root(int x) { return x == p[x] ? x : p[x] = root(p[x]); }

int main()

{

	scanf("%d%d", &n, &m);

	for (int i = 1; i <= n; i++)

	{

		ll cost;

		scanf("%lld", &cost);

		a[i] = make_pair(cost, i);

	}

	for (int i = 1; i <= n; i++) p[i] = i;

	for (int i = 1; i <= m; i++)

	{

		int u, v;

		scanf("%d%d", &u, &v);

		u++; v++;

		p[root(u)] = root(v);

	}

	int g = 0;

	for (int i = 1; i <= n; i++) if (p[i] == i) g++;

	// need to group g supernodes into a tree. need to have:

	//   sum of degree = 2(g-1) <= number of nodes = n

	if (2 * (g - 1) > n)

	{

		printf("Impossible\n");

		return 0;

	}

	// select smallest cost from each group first

	// then select smallest from rest

	sort(&a[1], &a[n + 1]);

	int dsum = 0;

	for (int i = 1; i <= n && dsum < 2 * (g - 1); i++)

	{

		int x = a[i].second;

		int r = root(x);

		if (group_used[r] || used[x]) continue;

		group_used[r] = used[x] = true;

		ans += a[i].first;

		dsum++;

	}

	for (int i = 1; i <= n && dsum < 2 * (g - 1); i++)

	{

		int x = a[i].second;

		if (used[x]) continue;

		ans += a[i].first;

		dsum++;

	}

	printf("%lld\n", ans);

	return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

#define rep(i,a,b) for (int i=(a); i<=(b); i++)

#define per(i,a,b) for (int i=(a); i>=(b); i--)

using namespace std;



const int maxn = 100005;

int a[maxn], vis[maxn], n, m, x, y, mn, cur;

priority_queue<int, vector<int>, greater<int> > Q;

vector<int> e[maxn];

long long ans;



void dfs(int u) {

	int sz = e[u].size();

	if (a[u] < mn) mn = a[u], cur = u;

	vis[u] = 1;

	if (!sz) return;

	rep (i, 0, sz - 1)

		if (!vis[e[u][i]]) dfs(e[u][i]);

}



int main() {

	scanf("%d%d", &n, &m);

	if (n < 2 * (n - m - 1)) 

		return puts("Impossible"), 0;

	if (n - 1 == m)

		return puts("0"), 0;

	rep (i, 1, n) scanf("%d", &a[i]);

	rep (i, 1, m) {

		scanf("%d%d", &x, &y); x++; y++;

		e[x].push_back(y); e[y].push_back(x);

	}

	rep (i, 1, n)

		if (!vis[i]) {

			mn = 2e9;

			dfs(i);

			ans += mn;

			a[cur] = 0;

		}

	rep (i, 1, n)

		if (a[i]) Q.push(a[i]);

	m = 2 * (n - m - 1) - (n - m);

	while (m-- > 0) {

		ans += Q.top();

		Q.pop();

	}

	cout << ans << endl;

	return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>

using namespace std;

typedef long long ll;

int p[100100];

ll ans;

int n, m;

pair<ll, int> a[100100];

bool group_used[100100];

bool used[100100];

int root(int x) { return x == p[x] ? x : p[x] = root(p[x]); }

int main()

{

	scanf("%d%d", &n, &m);

	for (int i = 1; i <= n; i++)

	{

		ll cost;

		scanf("%lld", &cost);

		a[i] = make_pair(cost, i);

	}

	for (int i = 1; i <= n; i++) p[i] = i;

	for (int i = 1; i <= m; i++)

	{

		int u, v;

		scanf("%d%d", &u, &v);

		u++; v++;

		p[root(u)] = root(v);

	}

	int g = 0;

	for (int i = 1; i <= n; i++) if (p[i] == i) g++;

	// need to group g supernodes into a tree. need to have:

	//   sum of degree = 2(g-1) <= number of nodes = n

	if (2 * (g - 1) > n)

	{

		printf("Impossible\n");

		return 0;

	}

	// select smallest cost from each group first

	// then select smallest from rest

	sort(&a[1], &a[n + 1]);

	int dsum = 0;

	for (int i = 1; i <= n && dsum < 2 * (g - 1); i++)

	{

		int x = a[i].second;

		int r = root(x);

		if (group_used[r] || used[x]) continue;

		group_used[r] = used[x] = true;

		ans += a[i].first;

		dsum++;

	}

	for (int i = 1; i <= n && dsum < 2 * (g - 1); i++)

	{

		int x = a[i].second;

		if (used[x]) continue;

		ans += a[i].first;

		dsum++;

	}

	printf("%lld\n", ans);

	return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

#include<algorithm>

#include<cstdio>

#include<cstdlib>

#include<cmath>

#include<cstring>

#include<queue>

#include<vector>

#include<map>

using namespace std;

#define llg long long

#define maxn 200010

#define yyj(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);



inline llg getint()

{

    llg w=0,q=0; char c=getchar();

    while((c<'0' || c>'9') && c!='-') c=getchar();

    if (c=='-')  q=1, c=getchar(); while (c>='0' && c<='9') w=w*10+c-'0', c=getchar();

    return q ? -w : w;

}



struct node{llg val,fa;} a[maxn];



bool cmp(const node &a,const node&b){return a.val<b.val;}



llg n,m,fa[maxn],ans,c[maxn],needb,xian[maxn],chuxian[maxn],bj[maxn];



llg find(llg x) {if (fa[x]!=x) fa[x]=find(fa[x]); return fa[x];}



int main()

{

	cin>>n>>m;

	for (llg i=1;i<=n;i++) c[i]=getint(),fa[i]=i;

	for (llg i=1;i<=m;i++)

	{

		llg x=getint(),y=getint();

		x++,y++;

		if (find(x)!=find(y)) fa[find(x)]=find(y);

	}

	for (llg i=1;i<=n;i++) a[i].val=c[i],a[i].fa=find(i);

	sort(a+1,a+n+1,cmp);

	needb=n-1-m;

	llg cs=0;

	for (llg i=1;i<=n && cs<needb*2;i++)

		if (!chuxian[a[i].fa])

		{

			cs++;

			bj[i]=1;

			chuxian[a[i].fa]=1;

			ans+=a[i].val;

		}

	for (llg i=1;i<=n && cs<needb*2;i++)

	{

		if (xian[a[i].fa]<needb && !bj[i])

		{

			xian[a[i].fa]++;

			ans+=a[i].val;

			cs++;

		}

	}

	if (cs<needb*2) puts("Impossible"); else 

	cout<<ans;

	return 0;

} 


// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>

using namespace std;

typedef long long ll;

int p[100100];

ll ans;

int n, m;

pair<ll, int> a[100100];

bool group_used[100100];

bool used[100100];

int root(int x) { return x == p[x] ? x : p[x] = root(p[x]); }

int main()

{

	scanf("%d%d", &n, &m);

	for (int i = 1; i <= n; i++)

	{

		ll cost;

		scanf("%lld", &cost);

		a[i] = make_pair(cost, i);

	}

	for (int i = 1; i <= n; i++) p[i] = i;

	for (int i = 1; i <= m; i++)

	{

		int u, v;

		scanf("%d%d", &u, &v);

		u++; v++;

		p[root(u)] = root(v);

	}

	int g = 0;

	for (int i = 1; i <= n; i++) if (p[i] == i) g++;

	// need to group g supernodes into a tree. need to have:

	//   sum of degree = 2(g-1) <= number of nodes = n

	if (2 * (g - 1) > n)

	{

		printf("Impossible\n");

		return 0;

	}

	// select smallest cost from each group first

	// then select smallest from rest

	sort(&a[1], &a[n + 1]);

	int dsum = 0;

	for (int i = 1; i <= n && dsum < 2 * (g - 1); i++)

	{

		int x = a[i].second;

		int r = root(x);

		if (group_used[r] || used[x]) continue;

		group_used[r] = used[x] = true;

		ans += a[i].first;

		dsum++;

	}

	for (int i = 1; i <= n && dsum < 2 * (g - 1); i++)

	{

		int x = a[i].second;

		if (used[x]) continue;

		ans += a[i].first;

		dsum++;

	}

	printf("%lld\n", ans);

	return 0;

}


// Fastest known solution for this problem: