Language: C++

#include "bits/stdc++.h"

#define REP(i,n) for(ll i=0;i<ll(n);++i)
#define RREP(i,n) for(ll i=ll(n)-1;i>=0;--i)
#define FOR(i,m,n) for(ll i=m;i<ll(n);++i)
#define RFOR(i,m,n) for(ll i=ll(n)-1;i>=ll(m);--i)
#define ALL(v) (v).begin(),(v).end()
#define UNIQUE(v) v.erase(unique(ALL(v)),v.end());
#define DUMP(v) REP(aa, (v).size()) { cout << v[aa]; if (aa != v.size() - 1)cout << " "; else cout << endl; }
#define INF 1000000001ll
#define MOD 1000000007ll
#define EPS 1e-9

const int dx[8] = { 1,1,0,-1,-1,-1,0,1 };
const int dy[8] = { 0,1,1,1,0,-1,-1,-1 };


using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
ll max(ll a, int b) { return max(a, ll(b)); }
ll max(int a, ll b) { return max(ll(a), b); }
ll min(ll a, int b) { return min(a, ll(b)); }
ll min(int a, ll b) { return min(ll(a), b); }
struct UnionFind {
	vector<int> par;
	vector<int> sizes;

	UnionFind(int n) {
		par.resize(n);
		sizes.resize(n);
		REP(i, n) {
			par[i] = i;
			sizes[i] = 1;
		}
	}

	int root(int a) {
		return par[a] == a ? a : par[a] = root(par[a]);
	}

	bool same(int x, int y) {
		return root(x) == root(y);
	}

	void unite(int x, int y) {
		x = root(x);
		y = root(y);
		if (x == y)return;
		if (sizes[x] < sizes[y]) swap(x, y);

		par[y] = x;
		sizes[x] += sizes[y];
	}
	int size(int x) {
		return sizes[root(x)];
	}
};

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n, m;
	cin >> n >> m;
	vi a(n);
	REP(i, n)cin >> a[i];
	UnionFind uf(n);
	REP(i, m) {
		int x, y;
		cin >> x >> y;
		uf.unite(x, y);
	}
	map<int, vl> mp;
	REP(i, n) {
		mp[uf.root(i)].push_back(a[i]);
	}
	function<bool(vl, vl)> cmp([](vl &a, vl &b) {return a.back() > b.back(); });
	priority_queue<vl, vvl, function<bool(vl, vl)> >  q(cmp);
	for (auto a : mp) {
		sort(ALL(a.second), greater<ll>());
		q.push(a.second);
	}
	ll ans = 0;
	if (2 * (q.size() - 1) > n) {
		cout << "Impossible" << endl;
		return 0;
	}
	while (q.size() >= 2) {
		vl a = q.top(); q.pop();
		vl b = q.top(); q.pop();
		ans += a.back() + b.back();
		a.pop_back();
		b.pop_back();
		if (a.size() > b.size()) {
			REP(i, b.size())a.push_back(b[i]);
			sort(ALL(a),greater<ll>());
			q.push(a);
		}
		else {
			REP(i, a.size())b.push_back(a[i]);
			sort(ALL(b),greater<ll>());
			q.push(b);
		}
	}
	cout << ans << endl;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:82:83: error: no matching function for call to 'std::function<bool(std::vector<long long int>, std::vector<long long int>)>::function(main()::<lambda(vl&, vl&)>)'
   82 |         function<bool(vl, vl)> cmp([](vl &a, vl &b) {return a.back() > b.back(); });
      |                                                                                   ^
In file included from /usr/include/c++/14/functional:59,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:53,
                 from a.cc:1:
/usr/include/c++/14/bits/std_function.h:435:9: note: candidate: 'template<class _Functor, class _Constraints> std::function<_Res(_ArgTypes ...)>::function(_Functor&&) [with _Constraints = _Functor; _Res = bool; _ArgTypes = {std::vector<long long int, std::allocator<long long int> >, std::vector<long long int, std::allocator<long long int> >}]'
  435 |         function(_Functor&& __f)
      |         ^~~~~~~~
/usr/include/c++/14/bits/std_function.h:435:9: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/14/bits/stl_pair.h:60,
                 from /usr/include/c++/14/bits/stl_algobase.h:64,
                 from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51:
/usr/include/c++/14/type_traits: In substitution of 'template<bool _Cond, class _Tp> using std::__enable_if_t = typename std::enable_if::type [with bool _Cond = false; _Tp = void]':
/usr/include/c++/14/bits/std_function.h:353:8:   required by substitution of 'template<class _Res, class ... _ArgTypes> template<class _Cond, class _Tp> using std::function<_Res(_ArgTypes ...)>::_Requires = std::__enable_if_t<((bool)_Cond::value), _Tp> [with _Cond = std::function<bool(std::vector<long long int>, std::vector<long long int>)>::_Callable<main()::<lambda(vl&, vl&)>, main()::<lambda(vl&, vl&)>, std::__invoke_result<main()::<lambda(vl&, vl&)>&, std::vector<long long int, std::allocator<long long int> >, std::vector<long long int, std::allocator<long long int> > > >; _Tp = void; _Res = bool; _ArgTypes = {std::vector<long long int, std::allocator<long long int> >, std::vector<long long int, std::allocator<long long int> >}]'
  353 |         using _Requires = __enable_if_t<_Cond::value, _Tp>;
      |               ^~~~~~~~~
/usr/include/c++/14/bits/std_function.h:434:9:   required from here
  434 |                typename _Constraints = _Requires<_Callable<_Functor>>>
      |                ^~~~~~~~
/usr/include/c++/14/type_traits:138:11: error: no type named 'type' in 'struct std::enable_if<false, void>'
  138 |     using __enable_if_t = typename enable_if<_Cond, _Tp>::type;
      |           ^~~~~~~~~~~~~
/usr/include/c++/14/bits/std_function.h:404:7: note: candidate: 'std::function<_Res(_ArgTypes ...)>::function(std::function<_Res(_ArgTypes ...)>&&) [with _Res = bool; _ArgTypes = {std::vector<long long int, std::allocator<long long int> >, std::vector<long long int, std::allocator<long long int> >}]'
  404 |       function(function&& __x) noexcept
      |       ^~~~~~~~
/usr/include/c++/14/bits/std_function.h:404:27: note:   no known conversion for argument 1 from 'main()::<lambda(vl&, vl&)>' to 'std::function<bool(std::vector<long long int>, std::vector<long long int>)>&&'
  404 |       function(function&& __x) noexcept
      |                ~~~~~~~~~~~^~~
/usr/include/c++/14/bits/std_function.h:386:7: note: candidate: 'std::function<_Res(_ArgTypes ...)>::function(const std::function<_Res(_ArgTypes ...)>&) [with _Res = bool; _ArgTypes = {std::vector<long long int, std::allocator<long long int> >, std::vector<long long int, std::allocator<long long int> >}]'
  386 |       function(const function& __x)
      |       ^~~~~~~~
/usr/include/c++/14/bits/std_function.h:386:32: note:   no known conversion for argument 1 from 'main()::<lambda(vl&, vl&)>' to 'const std::function<bool(std::vector<long long int>, std::vector<long long int>)>&'
  386 |       function(const function& __x)
      |                ~~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/std_function.h:375:7: note: candidate: 'std::function<_Res(_ArgTypes ...)>::function(std::nullptr_t) [with _Res = bool; _ArgTypes = {std::vector<long long int, std::allocator<long long int> >, std::vector<long long int, std::allocator<long long int> >}; std::nullptr_t = std::nullptr_t]'
  375 |       function(nullptr_t) noexcept
      |       ^~~~~~~~
/usr/include/c++/14/bits/std_function.h:375:16: note:   no known conversion for argument 1 from 'main()::<lambda(vl&, vl&)>' to 'std::nullptr_t'
  375 |       function(nullptr_t) noexcept
      |                ^~~~~~~~~
/usr/include/c++/14/bits/std_function.h:368:7: note: candidate: 'std::function<_Res(_ArgTypes ...)>::function() [with _Res = bool; _ArgTypes = {std::vector<long long int, std::allocator<long long int> >, std::vector<long long int, std::allocator<long long int> >}]'
  368 |       function() noexcept
      |       ^~~~~~~~
/usr/include/c++/14/bits/std_function.h:368:7: note:   candidate expects 0 arguments, 1 provided