Language: C++

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;

#define pb push_back
#define rep(i, a, n) for(int i = (a); i < (n); i++)
#define dep(i, a, n) for(int i = (a); i >= (n); i--)
#define mod (ll)(1e9+7)
#define int ll

__attribute__((constructor))
void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

struct T {int cost, i;};


bool comp(T& t1, T& t2) {
  return t1.cost < t2.cost;
}

T t[100001];
int par[100001], rank[100001];

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 s[100001];

signed main() {
  int v, e;
  cin >> v >> e;
  if((e + 1) * 2 < v) {
    cout << "Impossible";
    return 0;
  }
  for(int i = 0; i < v; i++) par[i] = i;
  rep(i, 0, v) {
    int a;
    cin >> a;
    t[i] = {a, i};
  }
  rep(i, 0, e) {
    int a, b;
    cin >> a >> b;
    unite(a, b);
  }
  sort(t, t + v, comp);
  int ans = 0;

  rep(i, 0, v) {
    if(s[t[i].i]) continue;
    rep(j, i + 1, v) {

      if(s[t[j].i]) continue;
      if(!same(t[i].i, t[j].i)) {
        unite(t[i].i, t[j].i);
        ans += t[i].cost + t[j].cost;
        s[i] = 1, s[j] = 1;
        break;
      }
    }
  }
  cout << ans << endl;
}


Compiler output:
a.cc: In function 'void unite(ll, ll)':
a.cc:39:6: error: reference to 'rank' is ambiguous
   39 |   if(rank[x] < rank[y]) {
      |      ^~~~
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:28:18: note:                 'll rank [100001]'
   28 | int par[100001], rank[100001];
      |                  ^~~~
a.cc:39:16: error: reference to 'rank' is ambiguous
   39 |   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:28:18: note:                 'll rank [100001]'
   28 | int par[100001], rank[100001];
      |                  ^~~~
a.cc:43:8: error: reference to 'rank' is ambiguous
   43 |     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:28:18: note:                 'll rank [100001]'
   28 | int par[100001], rank[100001];
      |                  ^~~~
a.cc:43:19: error: reference to 'rank' is ambiguous
   43 |     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:28:18: note:                 'll rank [100001]'
   28 | int par[100001], rank[100001];
      |                  ^~~~
a.cc:43:28: error: reference to 'rank' is ambiguous
   43 |     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:28:18: note:                 'll rank [100001]'
   28 | int par[100001], rank[100001];
      |                  ^~~~