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.392342833814375x):
#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 <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.599898830976212x):
#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<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;

}

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

}
// 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.687187116971206x):
#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;

}
// 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.449203413280216x):
#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;

}
// 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.64712930763443x):
#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;

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

}

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

}
// 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: 1.4036297923725387x):
#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;

}
// 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.686201916247444x):
#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;

}