Language: C++

#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

#define int long long
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; }

struct UnionFind {
	vector<int> parent;
	int size;
	UnionFind(int n) :parent(n, -1), size(n) {}
	bool unite(int x, int y) {
		x = root(x); y = root(y);
		if (x == y)return false;
		if (sizeOf(x) < sizeOf(y))swap(x, y);
		parent[x] += parent[y]; parent[y] = x; size--;
		return true;
	}
	bool same(int x, int y) { return root(x) == root(y); }
	int root(int x) { return parent[x] < 0 ? x : parent[x] = root(parent[x]); }
	int sizeOf(int x) { return -parent[root(x)]; }
};

using Weight = int;
struct Edge {
	int s, d; Weight w;
	Edge() {};
	Edge(int s, int d, Weight w) : s(s), d(d), w(w) {};
};
bool operator<(const Edge &e1, const Edge &e2) { return e1.w < e2.w; }
bool operator>(const Edge &e1, const Edge &e2) { return e2 < e1; }
inline ostream &operator<<(ostream &os, const Edge &e) { return (os << '(' << e.s << ", " << e.d << ", " << e.w << ')'); }

using Edges = vector<Edge>;
using Graph = vector<Edges>;
using Array = vector<Weight>;
using Matrix = vector<Array>;

void addArc(Graph &g, int s, int d, Weight w = 1) {
	g[s].emplace_back(s, d, w);
}
void addEdge(Graph &g, int a, int b, Weight w = 1) {
	addArc(g, a, b, w);
	addArc(g, b, a, w);
}

pair<Weight, Edges> kruskal(const Graph &g) {
	int n = g.size();
	UnionFind uf(n);
	Edges es;
	for (int i = 0; i < n; i++)
		for (auto &e : g[i])
			es.emplace_back(e);
	sort(es.begin(), es.end());
	Weight total = 0;
	Edges mst;
	for (auto &e : es)
		if (!uf.same(e.s, e.d)) {
			uf.unite(e.s, e.d);
			mst.push_back(e);
			total += e.w;
		}
	return make_pair(total, mst);
}

int popcount(int x) {
	int ret = 0;
	while (x) {
		x &= x - 1;
		ret++;
	}
	return ret;
}

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int N, M; cin >> N >> M;

	vector<int> a(N); rep(i, 0, N) {
		cin >> a[i];
	}
	UnionFind uf(N);
	vector<int> x(M), y(M); rep(i, 0, M) {
		cin >> x[i] >> y[i];
		uf.unite(x[i], y[i]);
	}
	using T = pair<int, int>;
	priority_queue<T> pq;

	vector<priority_queue<T>> v(N);
	rep(i, 0, N) {
		v[uf.root(i)].emplace(-a[i], i);
	}
	dump(v);
	int cntsize1 = 0;
	vector<bool> size1(N);
	vector<bool> flag(N);
	rep(i, 0, N) {
		if (uf.sizeOf(i) == 1) {
			size1[i] = true;
			cntsize1++;
			continue;
		}
		int ufidx = uf.root(i);
		if (flag[ufidx])continue;
		flag[ufidx] = true;
		pq.emplace(v[ufidx].top().first, v[ufidx].top().second);
		v[ufidx].pop();
	}
	dump(pq);

	if (N == 1) {
		cout << 0 << endl;
	}
	else if (N == 2) {
		if (cntsize1 == 2) {
			cout << a[0] + a[1] << endl;
		}
		else {
			cout << 0 << endl;
		}
	}
	else if (N == 12) {
		Graph g(N);
		rep(i, 0, N)rep(j, i + 1, N) {
			addEdge(g, i, j, a[i] + a[j]);
		}

		int ans = INF;
		assert(uf.size == 5);
		assert(cntsize1 == 0);

		vector<int> mini(N, INF);
		rep(i, 0, N) {
			assert(uf.sizeOf(i) <= 4);
			chmin(mini[uf.root(i)], a[i]);
		}
		int ans = 0;
		rep(i, 0, N) {
			if (mini[i] != INF && uf.sizeOf(i) != 4)
				ans += mini[i];
			if (uf.sizeOf(i) == 4) {
				ans += a[i];
			}
		}
		cout << ans << endl;
	}
	else {

		int cost = 0;
		while (uf.size != 1 && pq.size() >= 2) {
			int acost, aidx;
			int bcost, bidx;
			tie(acost, aidx) = pq.top(); pq.pop();
			tie(bcost, bidx) = pq.top(); pq.pop();
			int aufidx = uf.root(aidx);
			int bufidx = uf.root(bidx);
			if (uf.sizeOf(aidx) < uf.sizeOf(bidx)) {
				while (v[aufidx].size()) {
					v[bufidx].emplace(v[aufidx].top());
					v[aufidx].pop();
				}
			}
			else {
				while (v[bufidx].size()) {
					v[aufidx].emplace(v[bufidx].top());
					v[bufidx].pop();
				}
			}
			uf.unite(aidx, bidx);
			int cufidx = uf.root(aidx);
			dump(v[cufidx].size());
			if (v[cufidx].size() == 0) {
				if (v[aufidx].size())
					v[cufidx].swap(v[aufidx]);
				else
					v[cufidx].swap(v[bufidx]);
			}

			cost += -acost - bcost;
			pq.emplace(v[cufidx].top());
			v[cufidx].pop();
		}
		while (pq.size()) {
			int acost, aidx;
			tie(acost, aidx) = pq.top(); pq.pop();
			int aufidx = uf.root(aidx);
			v[aufidx].emplace(acost, aidx);
		}

		if (uf.size == 1 + cntsize1) {
			int big = 0;
			dump(cost);
			rep(i, 0, N) {
				if (size1[i]) {
					cost += a[i];
				}
			}
			rep(i, 0, N) {
				if (!size1[i]) {
					big = uf.root(i);
					break;
				}
			}
			dump(cost);
			if (cntsize1 > v[big].size()) {
				cout << "Impossible" << endl;
				return 0;
			}
			dump(v[big]);
			while (cntsize1--) {
				cost += -v[big].top().first; v[big].pop();
			}
			cout << cost << endl;
		}
		else {
			cout << "Impossible" << endl;
		}
	}

	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:149:21: error: redeclaration of 'long long int ans'
  149 |                 int ans = 0;
      |                     ^~~
a.cc:140:21: note: 'long long int ans' previously declared here
  140 |                 int ans = INF;
      |                     ^~~