stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#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");

}

//

// Faster C++ solution for the same problem (measured speedup: 22.333736394460097x):
#include <cstdio>

#include <cstdlib>

#include <algorithm>



const int maxn = 1e5 + 20;



int n, m, A[maxn], F[maxn];

std::pair<int,int> M[maxn];

bool used[maxn];



int find(int x)

{

	return F[x] == x ? x : (F[x] = find(F[x]));

}

template<class TAT>void checkmin(TAT &x,const TAT &y)

{

	if(y < x) x = y;

}

int main()

{

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

	for(int i = 0; i < n; i++) scanf("%d", &A[i]);

	

	for(int i = 0; i < n; i++) F[i] = i;

	

	for(int i = 0, x, y; i < m; i++)

	{

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

		F[find(x)] = find(y);

	}

	

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

		M[i] = std::make_pair(A[i], i);

	

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

		checkmin(M[find(i)], M[i]);

	

	long long ans = 0;

	int cnt = 0;

	

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

		if(find(i) == i)

		{

			ans += M[i].first, cnt++;

			used[M[i].second] = true;

		}



	if(cnt == 1)

	{

		printf("0\n");

	}

	else if(n < (cnt - 1) * 2)

	{

		printf("Impossible\n");

	}

	else

	{

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

			M[i] = std::make_pair(A[i], i);

		std::sort(M, M + n);

		

		int rem = (cnt - 1) * 2 - cnt;

		

		for(int i = 0; i < n && rem; i++)

		{

			if(used[M[i].second]) continue;

			ans += M[i].first, rem--;

		}

		

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

	}

	

	return 0;

}
// Slower C++ solution:
#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;

}


// Faster C++ solution for the same problem (measured speedup: 18.55121817924971x):
#include <cstdio>

#include <cstdlib>

#include <algorithm>



const int maxn = 1e5 + 20;



int n, m, A[maxn], F[maxn];

std::pair<int,int> M[maxn];

bool used[maxn];



int find(int x)

{

	return F[x] == x ? x : (F[x] = find(F[x]));

}

template<class TAT>void checkmin(TAT &x,const TAT &y)

{

	if(y < x) x = y;

}

int main()

{

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

	for(int i = 0; i < n; i++) scanf("%d", &A[i]);

	

	for(int i = 0; i < n; i++) F[i] = i;

	

	for(int i = 0, x, y; i < m; i++)

	{

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

		F[find(x)] = find(y);

	}

	

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

		M[i] = std::make_pair(A[i], i);

	

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

		checkmin(M[find(i)], M[i]);

	

	long long ans = 0;

	int cnt = 0;

	

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

		if(find(i) == i)

		{

			ans += M[i].first, cnt++;

			used[M[i].second] = true;

		}



	if(cnt == 1)

	{

		printf("0\n");

	}

	else if(n < (cnt - 1) * 2)

	{

		printf("Impossible\n");

	}

	else

	{

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

			M[i] = std::make_pair(A[i], i);

		std::sort(M, M + n);

		

		int rem = (cnt - 1) * 2 - cnt;

		

		for(int i = 0; i < n && rem; i++)

		{

			if(used[M[i].second]) continue;

			ans += M[i].first, rem--;

		}

		

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

	}

	

	return 0;

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 33.038575144811865x):
#include<cstdio>

#include<algorithm>

#include<cstring>

#define MN 200005

using namespace std;

typedef long long ll;

ll ans;

bool used[MN];

int n,m,fa[MN],id[MN],a[MN],sz[MN];

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

bool cmp(int x,int y){return a[x]<a[y];}

int main(){

	scanf("%d%d",&n,&m);int tt=(n-1-m)*2;int x,y;

	for(int i=1;i<=n;i++)scanf("%d",&a[i]),fa[i]=id[i]=i;sort(id+1,id+n+1,cmp);

	for(int i=1;i<=m;i++)scanf("%d%d",&x,&y),fa[getf(++x)]=getf(++y);

	

	for(int i=1;i<=n&&tt;i++)if(!used[id[i]]){

		if(sz[getf(id[i])])continue;

		sz[getf(id[i])]++,used[id[i]]=1,--tt,ans+=a[id[i]];

	}

	for(int i=1;i<=n&&tt;i++)if(!used[id[i]]){

		if(sz[getf(id[i])]+1>n-1-m)continue;

		sz[getf(id[i])]++,--tt,ans+=a[id[i]];

	}if(tt)puts("Impossible");else printf("%lld\n",ans);

}
// Slower C++ solution:
#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;

}


// Faster C++ solution for the same problem (measured speedup: 18.49243105310761x):
#include<cstdio>

#include<algorithm>

#include<cstring>

#define MN 200005

using namespace std;

typedef long long ll;

ll ans;

bool used[MN];

int n,m,fa[MN],id[MN],a[MN],sz[MN];

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

bool cmp(int x,int y){return a[x]<a[y];}

int main(){

	scanf("%d%d",&n,&m);int tt=(n-1-m)*2;int x,y;

	for(int i=1;i<=n;i++)scanf("%d",&a[i]),fa[i]=id[i]=i;sort(id+1,id+n+1,cmp);

	for(int i=1;i<=m;i++)scanf("%d%d",&x,&y),fa[getf(++x)]=getf(++y);

	

	for(int i=1;i<=n&&tt;i++)if(!used[id[i]]){

		if(sz[getf(id[i])])continue;

		sz[getf(id[i])]++,used[id[i]]=1,--tt,ans+=a[id[i]];

	}

	for(int i=1;i<=n&&tt;i++)if(!used[id[i]]){

		if(sz[getf(id[i])]+1>n-1-m)continue;

		sz[getf(id[i])]++,--tt,ans+=a[id[i]];

	}if(tt)puts("Impossible");else printf("%lld\n",ans);

}
// Slower C++ solution:
#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");

}

//

// Faster C++ solution for the same problem (measured speedup: 22.215239977998237x):
#include<cstdio>

#include<algorithm>

#include<cstring>

#define MN 200005

using namespace std;

typedef long long ll;

ll ans;

bool used[MN];

int n,m,fa[MN],id[MN],a[MN],sz[MN];

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

bool cmp(int x,int y){return a[x]<a[y];}

int main(){

	scanf("%d%d",&n,&m);int tt=(n-1-m)*2;int x,y;

	for(int i=1;i<=n;i++)scanf("%d",&a[i]),fa[i]=id[i]=i;sort(id+1,id+n+1,cmp);

	for(int i=1;i<=m;i++)scanf("%d%d",&x,&y),fa[getf(++x)]=getf(++y);

	

	for(int i=1;i<=n&&tt;i++)if(!used[id[i]]){

		if(sz[getf(id[i])])continue;

		sz[getf(id[i])]++,used[id[i]]=1,--tt,ans+=a[id[i]];

	}

	for(int i=1;i<=n&&tt;i++)if(!used[id[i]]){

		if(sz[getf(id[i])]+1>n-1-m)continue;

		sz[getf(id[i])]++,--tt,ans+=a[id[i]];

	}if(tt)puts("Impossible");else printf("%lld\n",ans);

}
// Slower C++ solution:
#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;

}


// Faster C++ solution for the same problem (measured speedup: 18.45279072240069x):
#include<cstdio>

#include<algorithm>

#include<cstring>

#define MN 200005

using namespace std;

typedef long long ll;

ll ans;

bool used[MN];

int n,m,fa[MN],id[MN],a[MN],sz[MN];

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

bool cmp(int x,int y){return a[x]<a[y];}

int main(){

	scanf("%d%d",&n,&m);int tt=(n-1-m)*2;int x,y;

	for(int i=1;i<=n;i++)scanf("%d",&a[i]),fa[i]=id[i]=i;sort(id+1,id+n+1,cmp);

	for(int i=1;i<=m;i++)scanf("%d%d",&x,&y),fa[getf(++x)]=getf(++y);

	

	for(int i=1;i<=n&&tt;i++)if(!used[id[i]]){

		if(sz[getf(id[i])])continue;

		sz[getf(id[i])]++,used[id[i]]=1,--tt,ans+=a[id[i]];

	}

	for(int i=1;i<=n&&tt;i++)if(!used[id[i]]){

		if(sz[getf(id[i])]+1>n-1-m)continue;

		sz[getf(id[i])]++,--tt,ans+=a[id[i]];

	}if(tt)puts("Impossible");else printf("%lld\n",ans);

}
// Slower C++ solution:
#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");

}

//

// Faster C++ solution for the same problem (measured speedup: 1.234674762748071x):
#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;

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 33.30196307202027x):
#include<cstdio>

#include<cstring>

#include<algorithm>

int n,m;

int a[100001];

int fa[100001];

int us[100001],val[100001],num[100001],re[100001],p,cnt;

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

long long ans=0;

int main(){

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

	for(int i=1;i<=n;i++) scanf("%d",&a[i]);

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

	for(int i=1,x,y;i<=m;i++) scanf("%d%d",&x,&y),x++,y++,fa[gf(y)]=gf(x);

	for(int i=1;i<=n;i++) if(fa[i]==i) cnt++;

	if(n<cnt*2-2) return !puts("Impossible");

	if(cnt==1) return !puts("0");

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

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

		int x=gf(i);

		if(a[i]<val[x]) val[x]=a[i],num[x]=i;

	}

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

		if(val[i]<2147483647) ans+=1ll*val[i];

		if(num[i]) us[num[i]]=1;

	}

	for(int i=1;i<=n;i++) if(!us[i]) re[++p]=a[i];

	std::sort(re+1,re+p+1);

	for(int i=1;i<=cnt-2;i++) ans+=1ll*re[i];

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

	return 0;

}
// Slower C++ solution:
#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;

}


// Faster C++ solution for the same problem (measured speedup: 18.63985517968612x):
#include<cstdio>

#include<cstring>

#include<algorithm>

int n,m;

int a[100001];

int fa[100001];

int us[100001],val[100001],num[100001],re[100001],p,cnt;

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

long long ans=0;

int main(){

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

	for(int i=1;i<=n;i++) scanf("%d",&a[i]);

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

	for(int i=1,x,y;i<=m;i++) scanf("%d%d",&x,&y),x++,y++,fa[gf(y)]=gf(x);

	for(int i=1;i<=n;i++) if(fa[i]==i) cnt++;

	if(n<cnt*2-2) return !puts("Impossible");

	if(cnt==1) return !puts("0");

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

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

		int x=gf(i);

		if(a[i]<val[x]) val[x]=a[i],num[x]=i;

	}

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

		if(val[i]<2147483647) ans+=1ll*val[i];

		if(num[i]) us[num[i]]=1;

	}

	for(int i=1;i<=n;i++) if(!us[i]) re[++p]=a[i];

	std::sort(re+1,re+p+1);

	for(int i=1;i<=cnt-2;i++) ans+=1ll*re[i];

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

	return 0;

}