Language: C++

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

struct data {
  long long id, cost;
};
bool operator<(const data &l, const data &r) {
  if(l.cost != r.cost) return l.cost < r.cost;
  return l.id < r.id;
}

struct Unionfind {
  // tree number
  vector<int> par;
  // tree rank
  vector<int> treerank;
  // constructor
  Unionfind(int n = 1) { stree(n + 1); }
  // make and initialization
  void stree(int n = 1) {
    par.resize(n);
    treerank.resize(n);
    for(int i = 0; i < n; ++i) {
      par[i] = i;
      treerank[i] = 0;
    }
  }
  // search root
  int root(int x) {
    if(par[x] == x) return x;
    return par[x] = root(par[x]);
  }
  // is same?
  bool issame(int x, int y) { return root(x) == root(y); }

  // add
  // already added, return 0
  bool uni(int x, int y) {
    x = root(x);
    y = root(y);
    if(x == y) return 0;
    if(treerank[x] < treerank[y])
      par[x] = y;
    else if(treerank[y] < treerank[x])
      par[y] = x;
    else {
      par[y] = x;
      ++treerank[x];
    }
    return 1;
  }
};

long long n, m, ans = -1;
bool ch[100005][2] = {0};
vector<data> v;
Unionfind uf;

void solve();

int main() {
  cin >> n >> m;
  uf = Unionfind(n + 1);
  v.resize(n);
  for(int i = 0; i < n; ++i) {
    cin >> v[i].cost;
    v[i].id = i;
  }
  sort(v.begin(), v.end());
  for(int i = 0; i < m; ++i) {
    int x, y;
    cin >> x >> y;
    uf.uni(x, y);
  }
  solve();
  if(ans == -1)
    cout << "Impossible" << endl;
  else
    cout << ans << endl;
  return 0;
}

void solve() {
  long long now = 0, cnt = 0;
  for(int i = 0; i < n; ++i)
    if(!ch[uf.root(v[i].id)][1]) {
      now += v[i].cost;
      ++cnt;
      ch[i][0] = 1;
      ch[uf.root(v[i].id)][1] = 1;
    }
  if(cnt == 1) now = 0;
  if(n < (cnt - 1) * 2) return;
  cnt = (cnt - 1) * 2 - cnt;
  for(int i = 0; i < n; ++i)
    if(!ch[i][0] && cnt > 0) {
      now += v[i].cost;
      if(--cnt == 0) break;
    }
  ans = now;
}


Compiler output:
a.cc:7:22: error: reference to 'data' is ambiguous
    7 | bool operator<(const data &l, const data &r) {
      |                      ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 from a.cc:1:
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:4:8: note:                 'struct data'
    4 | struct data {
      |        ^~~~
a.cc:7:37: error: reference to 'data' is ambiguous
    7 | bool operator<(const data &l, const data &r) {
      |                                     ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:4:8: note:                 'struct data'
    4 | struct data {
      |        ^~~~
a.cc:7:6: error: 'bool operator<(const int&, const int&)' must have an argument of class or enumerated type
    7 | bool operator<(const data &l, const data &r) {
      |      ^~~~~~~~
a.cc:56:12: error: template argument 1 is invalid
   56 | vector<data> v;
      |            ^
a.cc:56:12: error: template argument 2 is invalid
a.cc: In function 'int main()':
a.cc:64:5: error: request for member 'resize' in 'v', which is of non-class type 'int'
   64 |   v.resize(n);
      |     ^~~~~~
a.cc:66:13: error: invalid types 'int[int]' for array subscript
   66 |     cin >> v[i].cost;
      |             ^
a.cc:67:6: error: invalid types 'int[int]' for array subscript
   67 |     v[i].id = i;
      |      ^
a.cc:69:10: error: request for member 'begin' in 'v', which is of non-class type 'int'
   69 |   sort(v.begin(), v.end());
      |          ^~~~~
a.cc:69:21: error: request for member 'end' in 'v', which is of non-class type 'int'
   69 |   sort(v.begin(), v.end());
      |                     ^~~
a.cc: In function 'void solve()':
a.cc:86:21: error: invalid types 'int[int]' for array subscript
   86 |     if(!ch[uf.root(v[i].id)][1]) {
      |                     ^
a.cc:87:15: error: invalid types 'int[int]' for array subscript
   87 |       now += v[i].cost;
      |               ^
a.cc:90:19: error: invalid types 'int[int]' for array subscript
   90 |       ch[uf.root(v[i].id)][1] = 1;
      |                   ^
a.cc:97:15: error: invalid types 'int[int]' for array subscript
   97 |       now += v[i].cost;
      |               ^