Language: C++

#include <vector>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>

#define REP(i,n)for (int i=0;i<(n);i++)
#define PB push_back
#define MP make_pair
#define ALL(a) (a).begin(),(a).end()
#define ll long long
using namespace std;
typedef pair<int,int> P;
int n,m;
int x[100100],y[100100];
int par[100100];
int rank[100100];

void init(int n){
	for(int i=0;i<n;i++){
		par[i]=i;
		rank[i]=0;
	}
}

int find(int x){
	if(par[x]==x){
		return x;
	}else{
		return par[x]=find(par[x]);
	}
}

void unite(int x,int 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]++;
	}
}

bool same(int x,int y){
	return find(x)==find(y);
}

int main(){
	cin>>n>>m;
	init(n);
	vector<P>a(n);
	REP(i,n)cin>>a[i].first;
	REP(i,n)a[i].second=i;
	REP(i,m){
		cin>>x[i]>>y[i];
		unite(x[i],y[i]);
	}
	int ans=0;
	sort(ALL(a));
	bool used[n];
	REP(i,n)used[i]=false;
	REP(i,n){
		if(used[i])continue;
		for(int j=i+1;j<n;j++){
			if(used[j])continue;
			if(!same(a[i].second,a[j].second)){
				used[a[i].second]=used[a[j].second]=true;
				ans+=a[i].first+a[j].first;
				unite(a[i].second,a[j].second);
			}
		}
	}
	REP(i,n){
		for(int j=i+1;j<n;j++){
			if(!same(i,j)){
				cout<<"Impossible"<<endl;
				return 0;
			}
		}
	}
	cout<<ans<<endl;
}


Compiler output:
a.cc: In function 'void init(int)':
a.cc:39:17: error: reference to 'rank' is ambiguous
   39 |                 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/vector:62,
                 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:34:5: note:                 'int rank [100100]'
   34 | int rank[100100];
      |     ^~~~
a.cc: In function 'void unite(int, int)':
a.cc:55:12: error: reference to 'rank' is ambiguous
   55 |         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:34:5: note:                 'int rank [100100]'
   34 | int rank[100100];
      |     ^~~~
a.cc:55:20: error: reference to 'rank' is ambiguous
   55 |         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:34:5: note:                 'int rank [100100]'
   34 | int rank[100100];
      |     ^~~~
a.cc:59:20: error: reference to 'rank' is ambiguous
   59 |                 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:34:5: note:                 'int rank [100100]'
   34 | int rank[100100];
      |     ^~~~
a.cc:59:29: error: reference to 'rank' is ambiguous
   59 |                 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:34:5: note:                 'int rank [100100]'
   34 | int rank[100100];
      |     ^~~~
a.cc:59:37: error: reference to 'rank' is ambiguous
   59 |                 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:34:5: note:                 'int rank [100100]'
   34 | int rank[100100];
      |     ^~~~