Language: C++

#include<bits/stdc++.h>
using namespace std;
#define rep(i,j,n) for(int i=(int)(j);i<(int)(n);i++)
#define REP(i,j,n) for(int i=(int)(j);i<=(int)(n);i++)
#define MOD 1000000007
#define int long long
#define ALL(a) (a).begin(),(a).end()
#define vi vector<int>
#define vii vector<vi>
#define pii pair<int,int>
#define priq priority_queue<int>
#define disup(A,key) distance(A.begin(),upper_bound(ALL(A),(int)(key)))
#define dislow(A,key) distance(A.begin(),lower_bound(ALL(A),(int)(key)))
#define tii tuple<int,int,int>
#define Priq priority_queue<int,vi,greater<int>>
#define pb push_back
#define mp make_pair
#define INF (1ll<<60)
struct Union_Find{
  vi par,A;
  vii C;
  int N;
  Union_Find(int n):par(n),C(n),A(n){
    N=n;
    rep(i,0,N){
      par[i]=i;
      cin>>A[i];
    }
  }
  int root(int X){
    if(par[X]==X) return X;
    return par[X]=root(par[X]);
  }
  void insert(int X,int Y){
    int RX=root(X),RY=root(Y);
    if(RX==RY) return;
    par[RX]=RY;
  }
  void answer(){
    vi B;
    int ans=0;
    bool flag=0;
    set<int> S;
    rep(i,0,N){
      C[root(i)].pb(A[i]);
      S.insert(root(i));
    }
    while(S.size()>1){
      vector<pii> D;
      for(auto i:S){
        if(C[i].size()==0){
          cout<<"Impossible"<<endl;
          goto XYZ;
        }
        int X=C[i].size();
        sort(ALL(C[i]),greater<int>());
        ans+=C[i][X-1];
        C[i].pop_back();
        if(C[i].size()>0){
          D.pb(mp(C[i][X-2],i));
        }
        else D.pb(INF);
      }
      sort(ALL(D));
      int P=D.size();
      rep(i,0,P/2){
        S.erase(D[i].second);
        rep(j,0,C[D[i].second].size()){
          C[D[i+P/2].second].pb(C[D[i].second][j]);
        }
      }
    }
    cout<<ans<<endl;
    XYZ:;
  }
};
signed main(){
  int N,M; cin>>N>>M;
  Union_Find tree(N);
  rep(i,0,M){
    int X,Y; cin>>X>>Y;
    tree.insert(X,Y);
  }
  tree.answer();
  }



Compiler output:
a.cc: In member function 'void Union_Find::answer()':
a.cc:62:18: error: no matching function for call to 'std::vector<std::pair<long long int, long long int> >::push_back(long long int)'
   62 |         else D.pb(INF);
      |              ~~~~^~~~~
In file included from /usr/include/c++/14/vector:66,
                 from /usr/include/c++/14/functional:64,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:53,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_vector.h:1283:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::pair<long long int, long long int>; _Alloc = std::allocator<std::pair<long long int, long long int> >; value_type = std::pair<long long int, long long int>]'
 1283 |       push_back(const value_type& __x)
      |       ^~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1283:35: note:   no known conversion for argument 1 from 'long long int' to 'const std::vector<std::pair<long long int, long long int> >::value_type&' {aka 'const std::pair<long long int, long long int>&'}
 1283 |       push_back(const value_type& __x)
      |                 ~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_vector.h:1300:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(value_type&&) [with _Tp = std::pair<long long int, long long int>; _Alloc = std::allocator<std::pair<long long int, long long int> >; value_type = std::pair<long long int, long long int>]'
 1300 |       push_back(value_type&& __x)
      |       ^~~~~~~~~
/usr/include/c++/14/bits/stl_vector.h:1300:30: note:   no known conversion for argument 1 from 'long long int' to 'std::vector<std::pair<long long int, long long int> >::value_type&&' {aka 'std::pair<long long int, long long int>&&'}
 1300 |       push_back(value_type&& __x)
      |                 ~~~~~~~~~~~~~^~~