Language: C++

#include <bits/stdc++.h>

using namespace std;
typedef priority_queue<int, vector<int>, greater<int> > pq;

int n,m;
int a[100005];
int par[100005];
int rank[100005];
pq g[100005];
void init(int n) {
  for(int i = 0; i < n; i++) {
    par[i] = i;
    rank[i] = 0;
  }
}

int find(int x) {
  return x == par[x] ? x : par[x] = find(par[x]);
}

void Union(int x, int y) {
  int a = find(x);
  int b = find(y);
  if(a == b) return;

   if(rank[a] > rank[b]) {
    par[b] = a;
  }
   else {
     par[a] = b;
     if(rank[a] == rank[b]) rank[b]++;
   }
}

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

int main() {
  cin >> n >> m;
  int group = n;
  init(n);
  for(int i = 0; i < n; i++) cin >> a[i];
  for(int i = 0; i < m; i++) {
    int x,y;
    cin >> x >> y;
    if(find(x) != find(y)) {
      Union(x,y);
      group--;
    }
  }
  /*  for(int i = 0; i < n; i++) {
    cout << par[i] << endl;
    }*/
  
  if(group == 1) {
    cout << "0" << endl;
    return 0;
  }

  int ans = 0;
  for(int i = 0; i < n; i++) g[find(i)].push(a[i]);
  pq rest;

  for(int i = 0; i < n; i++) {
  
    if(find(i)  == i) {
      ans += g[i].top(); g[i].pop();
    }
    while(!g[i].empty()) {
      rest.push(g[i].top());
      //  cout << g[i].top() << endl;
      g[i].pop();
      
    }
  }
  int ne = (group - 1) * 2 - group;

  while(ne > 0) {
    if(rest.empty()) {
      cout << "Impossible" << endl;
      return 0;
    }
    ans += rest.top();
    rest.pop();
    ne--;
  }
  cout << ans << endl;
}


Compiler output:
a.cc: In function 'void init(int)':
a.cc:14:5: error: reference to 'rank' is ambiguous
   14 |     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:9:5: note:                 'int rank [100005]'
    9 | int rank[100005];
      |     ^~~~
a.cc: In function 'void Union(int, int)':
a.cc:27:7: error: reference to 'rank' is ambiguous
   27 |    if(rank[a] > rank[b]) {
      |       ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:9:5: note:                 'int rank [100005]'
    9 | int rank[100005];
      |     ^~~~
a.cc:27:17: error: reference to 'rank' is ambiguous
   27 |    if(rank[a] > rank[b]) {
      |                 ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:9:5: note:                 'int rank [100005]'
    9 | int rank[100005];
      |     ^~~~
a.cc:32:9: error: reference to 'rank' is ambiguous
   32 |      if(rank[a] == rank[b]) rank[b]++;
      |         ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:9:5: note:                 'int rank [100005]'
    9 | int rank[100005];
      |     ^~~~
a.cc:32:20: error: reference to 'rank' is ambiguous
   32 |      if(rank[a] == rank[b]) rank[b]++;
      |                    ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:9:5: note:                 'int rank [100005]'
    9 | int rank[100005];
      |     ^~~~
a.cc:32:29: error: reference to 'rank' is ambiguous
   32 |      if(rank[a] == rank[b]) rank[b]++;
      |                             ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:9:5: note:                 'int rank [100005]'
    9 | int rank[100005];
      |     ^~~~