stablegradients/pie-gem5-pairs huggingface dataset:
// 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.462244339586245x):
#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;

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

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

}

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

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

}

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

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

} 


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

}
// Slower C++ solution:
#include <bits/stdc++.h>





using namespace std;

typedef long long LL;

const int maxn = 100005;

int a[maxn], fa[maxn], mi[maxn], id[maxn], vis[maxn];

int n, m;



void Init()

{

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

    {

        fa[i] = i;

        mi[i] = a[i];

        id[i] = i;

    }

}



int Find(int x)

{

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

}



void Merge(int x, int y)

{

    int fx = Find(x), fy = Find(y);

    fa[fx] = fy;

    if(mi[fy] > mi[fx])

    {

        mi[fy] = mi[fx];

        id[fy] = id[fx];

    }

}



int main()

{

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

    multiset<int> s;

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

    {

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

        s.insert(a[i]);

    }

    Init();

//    multiset<int>::iterator it;

//    for(it = s.begin(); it != s.end(); it++)

//    {

//        printf("%d ", *it);

//    }

//    printf("\n");

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

    {

        int u, v;

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

        Merge(u + 1, v + 1);

    }

    int num = 0;

    LL ans = 0;

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

    {

//        printf("%d %d %d\n", i, Find(i), mi[fa[i]]);

        if(Find(i) == i)

        {

            num++;

            ans += mi[fa[i]];

            multiset<int>::iterator it = s.find(mi[fa[i]]);

            s.erase(it);

        }

    }

    if(n < 2 * num - 2)

        printf("Impossible\n");

    else if(num == 1)

        printf("0\n");

    else

    {

        int cnt = 0;

        multiset<int>::iterator it;

        for(it = s.begin(); it != s.end() && cnt < num - 2; it++)

        {

            cnt++;

            ans += *it;

        }

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

    }

    return 0;

}


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

}
// Slower C++ solution:
#include <bits/stdc++.h>

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

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

#define pq priority_queue

#define P pair<int,int>

#define P2 pair<int,P>

#define P3 pair<int,P2>

typedef long long ll; typedef long double ld;

using namespace std;

const int INF=1e9, MOD=1e9+7, around[]={0,1,1,-1,-1,0,-1,1,0,0};

const ll LINF=1e18;

const ld PI=abs(acos(-1));

set<int> st;

vector<ll> vec[100010], pos[100010];

vector<ll> g[100010];



void dfs(int p, int q){

	if(st.count(p)) return ; st.insert(p);

	vec[q].push_back(p);

	

	for(auto e:g[p]) dfs(e, q);

}



int main(){

	ll n, m, a[100010];

	cin >> n >> m;

	REP(i,0,n) cin >> a[i];

	REP(i,0,m){

		int x, y; cin >> x >> y;

		g[x].push_back(y);

		g[y].push_back(x);

	}

	

	pq<P2, vector<P2>, greater<P2>> qu;

	ll cou = 0, count = 0, li[100010]; ll s = 0, ma = 0;

	REP(i,0,n) if(!st.count(i)) dfs(i, cou++);

	REP(i,0,cou) REP(j,0,vec[i].size()) pos[i].push_back(a[vec[i][j]]);

	REP(i,0,cou) sort(pos[i].begin(), pos[i].end());

	REP(i,0,cou) s += pos[i][0];

	REP(i,0,cou) REP(j,1,pos[i].size()) qu.push({pos[i][j], {i, j}});

	

	int c = cou - 2;

	while(c and !qu.empty()){

		s += qu.top().first;

		ma = max<int>(ma, qu.top().first);

		qu.pop();

		c--;

	}

	if(cou == 1) cout << 0 << endl;

	else if(c) cout << "Impossible" << endl;

	else cout << s << endl;

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.452551387368814x):
#include <bits/stdc++.h>

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

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

#define pq priority_queue

#define P pair<int,int>

#define P2 pair<int,P>

#define P3 pair<int,P2>

typedef long long ll; typedef long double ld;

using namespace std;

const int INF=1e9, MOD=1e9+7, around[]={0,1,1,-1,-1,0,-1,1,0,0};

const ll LINF=1e18;

const ld PI=abs(acos(-1));



int num[100010];

vector<int> g[100010];

vector<int> vec[100010];

set<int> st;



void dfs(int p, int q){

	if(st.count(p)) return ; st.insert(p);

	vec[q].push_back(num[p]);

	

	for(auto e:g[p]) dfs(e, q);

}



int main(){

	int n,m; cin >> n >> m;

	REP(i,0,n) cin >> num[i];

	REP(i,0,m){

		int a,b; cin >> a >> b;

		g[a].push_back(b);

		g[b].push_back(a);

	}

	

	int cou = 0; ll s = 0;

	pq<int, vector<int>, greater<int>> qu;

	REP(i,0,n) if(!st.count(i)) dfs(i, cou++);

	REP(i,0,cou) sort(vec[i].begin(), vec[i].end());

	REP(i,0,cou) s += vec[i][0];

	REP(i,0,cou) REP(j,1,vec[i].size()) qu.push(vec[i][j]);

	

	int count = cou - 2;

	while(count and !qu.empty()){

		s += qu.top(); qu.pop();

		count--;

	}

	

	if(cou == 1) cout << 0 << endl;

	else if(count) cout << "Impossible" << endl;

	else cout << s << endl;

	return 0;

}
// Slower C++ solution:
//左手拈着花，右手舞着剑 

#include<iostream>

#include<cstdio>

#include<cstring>

#include<algorithm>

#include<cstring>

#include<map>

#include<vector>

#include<queue>

#define ll long long

#define pi pair<int,int>

#define mp(a,b) make_pair(a,b)

using namespace std;

const int N=100005;priority_queue

<pi,vector<pi>,greater<pi> >q[N];

int val[N],fa[N],rk[N],n,m,k;vector<pi>v;

void take(){

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

	fa[i]=i,rk[i]=0;

} int get(int x){

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

} void merge(int x,int y){

	x=get(x);y=get(y);

	if(x==y) return ;

	if(rk[x]<rk[y]) fa[x]=y;

	else{

		fa[y]=x;if(rk[x]==rk[y]) rk[x]++;

	}

} int main(){

	ll ans=0;scanf("%d%d",&n,&m);take();

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

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

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

		int fx=get(x),fy=get(y);

		if(fx!=fy) merge(fx,fy);

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

	q[get(i)].push(mp(val[i],i));

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

	if(!q[i].empty()) v.push_back(q[i].top()),

	val[q[i].top().second]=2e9,

	ans+=q[i].top().first;

	if(v.size()==1) puts("0");

	else if(n<(v.size()-1)*2)

	puts("Impossible");else{

		sort(val,val+n);

		n=(v.size()-1)*2-v.size();

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

		cout<<ans<<endl; 

	} return 0;

}

// Faster C++ solution for the same problem (measured speedup: 4.3663864288058765x):
#pragma GCC optimize(2)

#include<bits/stdc++.h>

#define N 100005

#define int long long

using namespace std;



#define v1 first

#define v2 second

#define pb push_back

#define mid ((l+r)>>1)

#define vec vector<int>

#define pr pair<int,int>

#define tp tuple<int,int,int>

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

#define all(x) x.begin(),x.end()

#define mem(a,x) memset(a,x,sizeof a)

#define For(x) for(int y,i=head[x];(y=e[i].to);i=e[i].nxt)



inline bool rd(int &X)

{

    X=0;int w=0;char ch=0;

    while(!isdigit(ch))w|=ch=='-',ch=getchar();

    while( isdigit(ch))X=(X<<3)+(X<<1)+(ch^48),ch=getchar();

    X=w?-X:X; return 1;

}



vec now;

int n,m,tot,ans;

int a[N],vis[N],u[N];

int head[N],cnt,mn[N];

struct nd{int nxt,to;}e[N<<1];

#define For(x) for(int y,i=head[x];(y=e[i].to);i=e[i].nxt)



void add(int x,int y){

    e[++cnt]=(nd){head[x],y};head[x]=cnt;

    e[++cnt]=(nd){head[y],x};head[y]=cnt;

}

void dfs(int x){

    if(a[x]<a[mn[tot]]) mn[tot]=x;

    For(x) if(!vis[y])

        vis[y]=1,dfs(y);

}

signed main()

{

    rd(n);rd(m); a[0]=2e9;



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

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

        rd(x),rd(y),add(x+1,y+1);

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

        if(!vis[i]) ++tot,dfs(i);

    if(tot==1) return cout<<0,0;

    if(n<2*(tot-1)) return cout<<"Impossible",0;

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

        u[mn[i]]=1,ans+=a[mn[i]];

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

        if(!u[i]) now.pb(a[i]);

    sort(all(now));

    for(int i=1;i<=2*(tot-1)-tot;i++)

        ans+=now[i-1];

    cout<<ans;

}