Language: C++

#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <string>
#include <queue>
#include <stack>
#include <math.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<ll, ll> P;
typedef vector<int> vi;
typedef vector<vi> vvi;

#define REP(i, n) for (ll i = 0; i < (n); ++i)
#define REPR(i, n) for (ll i = (n) - 1; i >= 0; --i)
#define FOR(i, n, m) for (ll i = (n); i < (m); ++i)
#define FORR(i, n, m) for (ll i = (m) - 1; i >= (n); --i) 
#define FORE(x, xs) for (auto &x: (xs))
#define ALL(v) v.begin(), v.end()
#define ZERO(a) memset(a,0,sizeof(a))

const int INF = 1000000009;
const int MOD = 1000000007;
const double EPS = 1e-14;
const double PI = acos(-1);

const int MAX = 2e5;

ll N, M;

vector<int> parent, rank;
vector<ii> cost;
map<int, vi> forest;

int getRoot (int n) {
  if (parent[n] == n) return n;
  int root = getRoot(parent[n]);
  parent[n] = root;
  return root;
}

bool isGroup (int m, int n) {
  return getRoot(m) == getRoot(n);
}

void unite (int m, int n) {
  m = getRoot(m), n = getRoot(n);
  int rankM = rank[m], rankN = rank[n];

  if (rankM > rankN) parent[n] = m;
  else {
    parent[m] = n;
    rank[n] = max(rank[n], rank[m]+1);
  }
}

int main() {
  cin >> N >> M;
  parent.resize(N+1), rank.resize(N+1), cost.resize(N);
  FOR (i, 1, N+1) {
    parent[i] = i;
    rank[i] = 1;
  }

  REP (i, N) {
    int c;
    cin >> c;
    cost[i] = make_pair(c, i+1);
  }
  sort(ALL(cost));

  REP (i, M) {
    int x, y;
    cin >> x >> y;
    x++; y++;
    unite(x, y);
  }

  int counts = 0;
  FOR (i, 1, N+1) 
    if (getRoot(i) == i) counts++;
  ll ans = 0;

  while (counts != 1) {
    int i = 0, root = getRoot(cost[i].second);
    int j = i+1;
    while (getRoot(cost[j].second) == root) j++;
    ans += cost[i].first + cost[j].first;
    cost.erase(cost.begin()+j), cost.erase(cost.begin()+i);
    if (cost.size() <= 1) {
      cout << "Impossible" << endl;
      return 0;
    }
    counts--;
  }

  cout << ans << endl;
  return 0;
}

Compiler output:
a.cc: In function 'void unite(int, int)':
a.cc:53:15: error: reference to 'rank' is ambiguous
   53 |   int rankM = rank[m], rankN = rank[n];
      |               ^~~~
In file included from /usr/include/c++/14/bits/move.h:37,
                 from /usr/include/c++/14/bits/exception_ptr.h:41,
                 from /usr/include/c++/14/exception:166,
                 from /usr/include/c++/14/ios:41,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 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:36:21: note:                 'std::vector<int> rank'
   36 | vector<int> parent, rank;
      |                     ^~~~
a.cc:55:15: error: 'rankN' was not declared in this scope; did you mean 'rankM'?
   55 |   if (rankM > rankN) parent[n] = m;
      |               ^~~~~
      |               rankM
a.cc:58:5: error: reference to 'rank' is ambiguous
   58 |     rank[n] = max(rank[n], rank[m]+1);
      |     ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:36:21: note:                 'std::vector<int> rank'
   36 | vector<int> parent, rank;
      |                     ^~~~
a.cc:58:19: error: reference to 'rank' is ambiguous
   58 |     rank[n] = max(rank[n], rank[m]+1);
      |                   ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:36:21: note:                 'std::vector<int> rank'
   36 | vector<int> parent, rank;
      |                     ^~~~
a.cc:58:28: error: reference to 'rank' is ambiguous
   58 |     rank[n] = max(rank[n], rank[m]+1);
      |                            ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:36:21: note:                 'std::vector<int> rank'
   36 | vector<int> parent, rank;
      |                     ^~~~
a.cc: In function 'int main()':
a.cc:64:23: error: reference to 'rank' is ambiguous
   64 |   parent.resize(N+1), rank.resize(N+1), cost.resize(N);
      |                       ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:36:21: note:                 'std::vector<int> rank'
   36 | vector<int> parent, rank;
      |                     ^~~~
a.cc:67:5: error: reference to 'rank' is ambiguous
   67 |     rank[i] = 1;
      |     ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:36:21: note:                 'std::vector<int> rank'
   36 | vector<int> parent, rank;
      |                     ^~~~