Language: C++

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto& (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define sar(a,n) cout<<#a<<":";rep(kbrni,n)cout<<" "<<a[kbrni];cout<<endl
#define svec(v) cout<<#v<<":";rep(kbrni,v.size())cout<<" "<<v[kbrni];cout<<endl
#define svecp(v) cout<<#v<<":";each(kbrni,v)cout<<" {"<<kbrni.first<<":"<<kbrni.second<<"}";cout<<endl
#define sset(s) cout<<#s<<":";each(kbrni,s)cout<<" "<<kbrni;cout<<endl
#define smap(m) cout<<#m<<":";each(kbrni,m)cout<<" {"<<kbrni.first<<":"<<kbrni.second<<"}";cout<<endl

using namespace std;

typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef pair<ll,int> pli;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

class UF {
private:
    int sz;
public:
    vector<int> kind;   //所属しているグループ番号
    vector<vector<int> > inc;   //グループのメンバー
    UF(){}
    UF(int node_size){ sz = node_size; kind.resize(sz),inc.resize(sz); rep(i,sz){ kind[i] = i; inc[i].pb(i); } }
    bool same(int a,int b){ return kind[a] == kind[b]; }
    void unite(int a,int b)
    { if(same(a,b)) return; if(inc[kind[a]].size() < inc[kind[b]].size()) swap(a,b);
        int ga = kind[a],gb = kind[b]; rep(i,inc[gb].size()) kind[inc[gb][i]] = ga;
        inc[ga].insert(inc[ga].end(),inc[gb].begin(),inc[gb].end()); inc[gb].clear(); }
};

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n,m;
    cin >> n >> m;
    vl a(n);
    rep(i,n){
        cin >> a[i];
    }
    UF uf(n);
    rep(i,m){
        int a,b;
        cin >> a >> b;
        uf.unite(a,b);
    }
    vvi& inc = uf.inc;
    vector<multiset<ll> > vec;
    ll ans = 0;
    int cnt = 0;
    rep(i,len(inc)){
        if(len(inc[i]) >= 2){
            vec.pb(set<ll>());
            rep(j,len(inc[i])){
                vec.back().insert(a[inc[i][j]]);
            }
        }else if(len(inc[i])){
            ans += a[inc[i][0]];
            cnt++;
        }
    }
    int sz = len(vec);
    if(sz == 0){
        if(cnt == 1){
            cout << "0\n";
            return 0;
        }else if(cnt == 2){
            cout << ans << "\n";
            return 0;
        }else{
            cout << "Impossible\n";
            return 0;
        }
    }
    priority_queue<pli,vector<pli>,greater<pli> > que;
    rep(i,sz){
        que.push(pli(*vec[i].begin(),i));
    }
    rep(i,sz-1){
        pli p = que.top();
        que.pop();
        pli q = que.top();
        que.pop();
        ans += p.fi + q.fi;
        vec[p.se].erase(vec[p.se].begin());
        vec[q.se].erase(vec[q.se].begin());
        if(len(vec[p.se]) < len(vec[q.se])){
            while(!vec[p.se].empty()){
                vec[q.se].insert(*vec[p.se].begin());
                vec[p.se].erase(vec[p.se].begin());
            }
            que.push(pli(*vec[q.se].begin(),q.se));
        }else{
            while(!vec[q.se].empty()){
                vec[p.se].insert(*vec[q.se].begin());
                vec[q.se].erase(vec[q.se].begin());
            }
            que.push(pli(*vec[p.se].begin(),p.se));
        }
    }
    int id = que.top().se;
    if(len(vec[id]) >= cnt){
        int lp = 0;
        each(it,vec[id]){
            if(lp == cnt){
                break;
            }
            ans += it;
            lp++;
        }
    }else{
        cout << "Impossible\n";
        return 0;
    }
    cout << ans << "\n";
    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:78:19: error: no matching function for call to 'std::vector<std::multiset<long long int> >::push_back(std::set<long long int>)'
   78 |             vec.pb(set<ll>());
      |             ~~~~~~^~~~~~~~~~~
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::multiset<long long int>; _Alloc = std::allocator<std::multiset<long long int> >; value_type = std::multiset<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 'std::set<long long int>' to 'const std::vector<std::multiset<long long int> >::value_type&' {aka 'const std::multiset<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::multiset<long long int>; _Alloc = std::allocator<std::multiset<long long int> >; value_type = std::multiset<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 'std::set<long long int>' to 'std::vector<std::multiset<long long int> >::value_type&&' {aka 'std::multiset<long long int>&&'}
 1300 |       push_back(value_type&& __x)
      |                 ~~~~~~~~~~~~~^~~