Language: C++

#include <bits/stdc++.h>
#define pb push_back
#define ll long long
#define mp make_pair
#define inf 1000000007
using namespace std;
ll par[114514];
ll rank[114514];
void init(int n){
	for(int i=0;i<n;i++){
		par[i] = i;
		rank[i]=0;
	}
}
ll find(ll x){
	if(par[x] == x){
		return x;
	}else{
		return par[x] = find(par[x]);
	}
}
void unite(ll x, ll y){
	x = find(x);
	y = find(y);
	if(x==y)return;
	if(rank[x]<rank[y]){
		par[x]=y;
	}else{
		par[y]=x;
		if(rank[x]==rank[y])rank[x]++;
	}
}
vector<ll> tre[100009];
int main(){
	int n,m;
	vector<ll> a;
	cin>>n>>m;
	if(n-1==m){
		cout<<0<<endl;
		return 0;
	}
	if( n < 2*(n-m-1) ){
		cout<<"Impossible"<<endl;
		return 0;
	}
	for(int i=0;i<n;i++){
		ll x;
		cin>>x;
		a.pb(x);
	}
	init(n);
	vector<ll> amari;
	for(int i=0;i<m;i++){
		ll x,y;
		cin>>x>>y;
		unite(x,y);
	}
	for(int i=0;i<n;i++){
		ll rt = find((ll)i);
		tre[rt].pb(a[i]);
	}
	ll cnt = 0,ans = 0;
	for(int i=0;i<n;i++){
		if(!tre[i].empty()){
			sort(tre[i].begin(),tre[i].end());
			cnt++;
			ans += tre[i][0];
			//cout<<tre[i][0]<<endl;
			for(int j=1;j<tre[i].size();i++){
				amari.pb(tre[i][j]);
			}
		}
	}
	sort(amari.begin(),amari.end());
	for(int i=cnt;i<2*(n-m-1);i++){
		ans += amari[i-cnt];
		//cout<<amari[i-cnt]<<endl;
	}
	cout<<ans<<endl;
	return 0;
}

Compiler output:
a.cc: In function 'void init(int)':
a.cc:12:17: error: reference to 'rank' is ambiguous
   12 |                 rank[i]=0;
      |                 ^~~~
In file included from /usr/include/c++/14/bits/stl_pair.h:60,
                 from /usr/include/c++/14/bits/stl_algobase.h:64,
                 from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:8:4: note:                 'long long int rank [114514]'
    8 | ll rank[114514];
      |    ^~~~
a.cc: In function 'void unite(long long int, long long int)':
a.cc:26:12: error: reference to 'rank' is ambiguous
   26 |         if(rank[x]<rank[y]){
      |            ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:8:4: note:                 'long long int rank [114514]'
    8 | ll rank[114514];
      |    ^~~~
a.cc:26:20: error: reference to 'rank' is ambiguous
   26 |         if(rank[x]<rank[y]){
      |                    ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:8:4: note:                 'long long int rank [114514]'
    8 | ll rank[114514];
      |    ^~~~
a.cc:30:20: error: reference to 'rank' is ambiguous
   30 |                 if(rank[x]==rank[y])rank[x]++;
      |                    ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:8:4: note:                 'long long int rank [114514]'
    8 | ll rank[114514];
      |    ^~~~
a.cc:30:29: error: reference to 'rank' is ambiguous
   30 |                 if(rank[x]==rank[y])rank[x]++;
      |                             ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:8:4: note:                 'long long int rank [114514]'
    8 | ll rank[114514];
      |    ^~~~
a.cc:30:37: error: reference to 'rank' is ambiguous
   30 |                 if(rank[x]==rank[y])rank[x]++;
      |                                     ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:8:4: note:                 'long long int rank [114514]'
    8 | ll rank[114514];
      |    ^~~~