Language: C++

#include<bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long long LL;
typedef pair<LL,LL> P;
const LL mod=1e9+7;
const LL LINF=1LL<<62;


const int M_N=200001;

struct UnionFind{
    int par[M_N];
    int rank[M_N];

    void init(int n){
        for(int i=0;i<n;i++){
            par[i]=i;
            rank[i]=0;
        }
    }

    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 main(){
    int n,m;cin >> n >> m;
    vector<LL> a(n);
    UnionFind uf;
    uf.init(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    for (int i = 0; i < m; i++) {
        int x,y;cin >> x >> y;
        uf.unite(x,y);
    }
    vector<P> v(n);
    set<LL> st;
    for (int i = 0; i < n; i++) {
        v[i] = make_pair(a[i],uf.find(i));
        st.insert(uf.find(i));
    }
    sort(ALL(v));
    int sz = st.size()-1;
    if(2*(st.size()-1)>n){
        puts("Impossible");
        return 0;
    }
    int l=1;
    vector<bool> used(n,false);
    LL ans = 0;
    for (int i = 0; i < n&& sz; i++) {
        if(used[i]) continue;
        for (int j = l; j < n; j++) {
            if(v[i].sc!=v[j].sc&&(!used[j])){
                ans+=v[i].fs+v[j].fs;
                used[j] = true;
                l = j+1;
                sz--;
                break;
            }
        }
    }
    if(sz){
        puts("Impossible");
        return 0;
    }
    cout << ans << endl;
    return 0;
}
#include<bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long long LL;
typedef pair<LL,LL> P;
const LL mod=1e9+7;
const LL LINF=1LL<<62;


const int M_N=200001;

struct UnionFind{
    int par[M_N];
    int rank[M_N];

    void init(int n){
        for(int i=0;i<n;i++){
            par[i]=i;
            rank[i]=0;
        }
    }

    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 main(){
    int n,m;cin >> n >> m;
    vector<LL> a(n);
    UnionFind uf;
    uf.init(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    for (int i = 0; i < m; i++) {
        int x,y;cin >> x >> y;
        uf.unite(x,y);
    }
    vector<P> v(n);
    set<LL> st;
    for (int i = 0; i < n; i++) {
        v[i] = make_pair(a[i],uf.find(i));
        st.insert(uf.find(i));
    }
    sort(ALL(v));
    int sz = st.size()-1;
    if(2*(st.size()-1)>n){
        puts("Impossible");
        return 0;
    }
    int l=1;
    vector<bool> used(n,false);
    LL ans = 0;
    for (int i = 0; i < n&& sz; i++) {
        if(used[i]) continue;
        for (int j = l; j < n; j++) {
            if(v[i].sc!=v[j].sc&&(!used[j])){
                ans+=v[i].fs+v[j].fs;
                used[j] = true;
                l = j+1;
                sz--;
                break;
            }
        }
    }
    if(sz){
        puts("Impossible");
        return 0;
    }
    cout << ans << endl;
    return 0;
}


Compiler output:
a.cc:114:10: error: redefinition of 'const LL mod'
  114 | const LL mod=1e9+7;
      |          ^~~
a.cc:12:10: note: 'const LL mod' previously defined here
   12 | const LL mod=1e9+7;
      |          ^~~
a.cc:115:10: error: redefinition of 'const LL LINF'
  115 | const LL LINF=1LL<<62;
      |          ^~~~
a.cc:13:10: note: 'const LL LINF' previously defined here
   13 | const LL LINF=1LL<<62;
      |          ^~~~
a.cc:118:11: error: redefinition of 'const int M_N'
  118 | const int M_N=200001;
      |           ^~~
a.cc:16:11: note: 'const int M_N' previously defined here
   16 | const int M_N=200001;
      |           ^~~
a.cc:120:8: error: redefinition of 'struct UnionFind'
  120 | struct UnionFind{
      |        ^~~~~~~~~
a.cc:18:8: note: previous definition of 'struct UnionFind'
   18 | struct UnionFind{
      |        ^~~~~~~~~
a.cc:159:5: error: redefinition of 'int main()'
  159 | int main(){
      |     ^~~~
a.cc:57:5: note: 'int main()' previously defined here
   57 | int main(){
      |     ^~~~