Language: C++

#include <bits/stdc++.h>
#define MAX 100001
#define M 1000000007
#define ll long long
#define ld long double

// #define left (2*i+1)
// #define right (2*i+2)
// #define mid ((l+r)>>1)

using namespace std;

int n, m;
ll a[MAX];
vector<int> e[MAX];
int p[MAX], rank[MAX];

int getRoot(int x) {
	if (p[x]==x) return x;
	return getRoot(p[x]);
}

int union_(int x, int y) {
	int rx = getRoot(x);
	int ry = getRoot(y);
	if (rx==ry) return 0;
	if (rank[rx]>rank[ry]) {
		p[ry] = rx;
		rank[rx]++;
	}
	else {
		p[rx] = ry;
		rank[ry]++;
	}
	return 0;
}

vector<int> component[MAX];
int done[MAX];

int isConnected() {
	for (int i=0;i<n;i++) {
		if (component[i].size()==n) return 1;
	}
	return 0;
}

int main() {
	cin >> n >> m;
	for (int i=0;i<n;i++) p[i] = i;
	for (int i=0;i<n;i++) cin >> a[i];
	for (int i=0;i<m;i++) {
		int u, v;
		cin >> u >> v;
		union_(u, v);
	}
	for (int i=0;i<n;i++) {
		component[getRoot(i)].push_back(i);
	}
	if (isConnected()) {
		cout << 0;
		return 0;
	}
	ll sum = 0;
	int comp = 0;
	for (int i=0;i<n;i++) {
		if (component[i].size()!=0) {
			comp++;
			int idx = -1;
			ll mn = INT_MAX;
			for (int j=0;j<component[i].size();j++) {
				if (a[component[i][j]]<mn) {
					mn = a[component[i][j]];
					idx = component[i][j];
				}
			}
			done[idx] = 1;
			sum+=mn;
		}
	}
	vector<ll> v;
	for (int i=0;i<n;i++) {
		if (component[i].size()!=0) {
			for (int j=0;j<component[i].size();j++) {
				if (!done[component[i][j]]) v.push_back(a[component[i][j]]);
			}
		}
	}
	sort(v.begin(), v.end());
	int left = (comp-1)*2-comp;

	if (v.size()<left) {
		cout << "Impossible";
		return 0;
	}
	for (int i=0;i<left;i++) {
		sum+=v[i];
	}
	cout << sum;
	return 0;
}

Compiler output:
a.cc: In function 'int union_(int, int)':
a.cc:27:13: error: reference to 'rank' is ambiguous
   27 |         if (rank[rx]>rank[ry]) {
      |             ^~~~
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:16:13: note:                 'int rank [100001]'
   16 | int p[MAX], rank[MAX];
      |             ^~~~
a.cc:27:22: error: reference to 'rank' is ambiguous
   27 |         if (rank[rx]>rank[ry]) {
      |                      ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:16:13: note:                 'int rank [100001]'
   16 | int p[MAX], rank[MAX];
      |             ^~~~
a.cc:29:17: error: reference to 'rank' is ambiguous
   29 |                 rank[rx]++;
      |                 ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:16:13: note:                 'int rank [100001]'
   16 | int p[MAX], rank[MAX];
      |             ^~~~
a.cc:33:17: error: reference to 'rank' is ambiguous
   33 |                 rank[ry]++;
      |                 ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:16:13: note:                 'int rank [100001]'
   16 | int p[MAX], rank[MAX];
      |             ^~~~