Language: C++

#include <bits/stdc++.h>
using namespace std;
#define Int int64_t
#define dump(x) cout << (x) << endl
#define fi first
#define se second
Int mod = 1e9+7;
Int INF = 1e18;
double EPS = 0.00000001;
//Int mod = 1e6+3;

#define MAX_V 200005

// Union-Find 
Int par[MAX_V];
Int sz[MAX_V];

void UFinit() {
    for (Int i = 0; i <= MAX_V; i++) {
        par[i] = i;
        sz[i] = 1;
    }
}

Int root(Int x) {
    if (par[x] == x) return x;
    return par[x] = root(par[x]);
}

void unite(Int x, Int y) {
    Int rx = root(x);
    Int ry = root(y);
    if (rx == ry) return;
    par[rx] = ry;
    sz[ry] += sz[rx];
    sz[rx] = sz[ry];
}

bool same(Int x, Int y) {
    Int rx = root(x);
    Int ry = root(y);
    return rx == ry;
}

int main() {
    Int n, m;
    cin >> n >> m;
    UFinit();
    vector<Int> a(n);
    for (Int i = 0; i < n; i++) cin >> a[i];
    for (Int i = 0; i < m; i++) {
        Int a, b;
        cin >> a >> b;
        unite(a, b);
    }
    map<Int, Int> mp;
    for (Int i = 0; i < n; i++) {
        mp[root(i)] = sz[root(i)];
    }
    if (mp.size() == 1) {
        dump(0);
        return 0;
    }
    Int cnt1 = 0;
    Int cnt2 = 0;
    map<Int, Int> mp2;
    for (auto v : mp) {
        mp2[v.se]++;
        if (v.se == 1) cnt1++;
        if (v.se >= 2) cnt2++;
    }
    Int hand1 = 0;
    Int hand2 = 0;
    for (auto v : mp2) {
        if (v.fi == 1) {
            hand1 += v.se;
        }
        if (v.fi >= 2) {
            hand2 += v.se * (v.fi - 2);
        }
    }
    hand2 += 2;
    if (hand1 > hand2) {
        dump("Impossible");
        return 0;
    }
    Int res = 0;
    vector<Int> v[100005];
    for (Int i = 0; i < n; i++) {
        if (sz[root(i)] == 1) {
            res += a[i];
        }
        else if (sz[root(i)] >= 2) {
            v[root(i)].emplace_back(a[i]);
        }
    }
    priority_queue<tuple<Int, Int, Int>, vector<tuple<Int, Int, Int>>, greater<tuple<Int, Int, Int>>> q;
    for (Int i = 0; i < n; i++) {
        if (v[i].empty()) continue;
        sort(v[i].begin(), v[i].end());
        q.push(make_tuple(v[i][0], i, (Int)0));
    }
    Int sum = 0;
    map<pair<Int, Int>, Int> mp3;
    while (sum < cnt2 - 1) {
        vector<tuple<Int, Int, Int>> tmp;
        tuple<Int, Int, Int> t = q.top();
        q.pop();
        tuple<Int, Int, Int> t2 = q.top();
        while (mp3[{get<1>(t), get<1>(t2)}] > 0 || mp3[{get<1>(t2), get<1>(t)}] > 0) {
            tmp.emplace_back(t2);
            q.pop();
            t2 = q.top();
        }
        sum++;
        mp3[{get<1>(t), get<1>(t2)}]++;
        res += get<0>(t) + get<0>(t2);
        if (get<2>(t) < (Int)v[get<1>(t)].size() - 1) {
            q.push(make_tuple(v[get<1>(t)][get<2>(t) + 1], get<1>(t), get<2>(t) + 1));
        }
        if (get<2>(t2) < (Int)v[get<1>(t2)].size() - 1) {
            q.push(make_tuple(v[get<1>(t2)][get<2>(t2) + 1], get<1>(t2), get<2>(t2) + 1));
        }
        for (Int i = 0; i < (Int)tmp.size(); i++) q.push(tmp[i]);
    }
    for (Int i = 0; i < cnt1; i++) {
        tuple<Int, Int, Int> t = q.top();
        q.pop();
        res += get<0>(t);
        if (get<2>(t) < (Int)v[get<1>(t)].size() - 1) {
            q.push(make_tuple(v[get<1>(t)][get<2>(t) + 1], get<1>(t), get<2>(t) + 1));
        }
    }
    dump(res);
    return 0;


Compiler output:
a.cc: In function 'int main()':
a.cc:135:14: error: expected '}' at end of input
  135 |     return 0;
      |              ^
a.cc:45:12: note: to match this '{'
   45 | int main() {
      |            ^