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)];
	}
};

using pq = priority_queue < ll, vl, greater<ll>>;
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];
	if (n == 2) {
		if(m==0)cout << a[0] + a[1] << endl;
		else cout << 0 << endl;
		return 0;
	}
	else if (n == 1) {
		cout << 0 << endl;
		return 0;
	}
	UnionFind uf(n);
	REP(i, m) {
		int x, y;
		cin >> x >> y;
		uf.unite(x, y);
	}
	map<int, pq> mp;
	REP(i, n) {
		mp[uf.root(i)].push(a[i]);
	}
	function<bool(pq,pq) > cmp([](pq &a, pq &b) {return a.top() > b.top(); });
	priority_queue<pq, vector<pq>, function<bool(pq,pq)> >  q(cmp);
	vl p;
	for (auto a : mp) {
		if (a.second.size() == 1)p.push_back(a.second.top());
		else q.push(a.second);
	}
	

	ll ans = 0;
	if (2 * (q.size()+p.size() - 1) > n) {
		cout << "Impossible" << endl;
		return 0;
	}
	while (q.size() >= 2) {
		pq a = q.top(); q.pop();
		pq b = q.top(); q.pop();
		ans += a.top() + b.top();
		a.pop();
		b.pop();
		if (a.size() > b.size()) {
			while (!b.empty()) {
				a.push(b.top());
				b.pop();
			}
			
			q.push(a);
		}
		else {
			while (!a.empty()) {
				b.push(a.top());
				a.pop();
			}

			q.push(b);
		}
	}
	if (q.size() == 1) {
		pq tmp = q.top();
		REP(i, p.size()) {
			ans += p[p.size() - 1 - i] + tmp.top();
			tmp.pop();
		}
	}
	cout << ans << endl;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:92:81: error: no matching function for call to 'std::function<bool(std::priority_queue<long long int, std::vector<long long int>, std::greater<long long int> >, std::priority_queue<long long int, std::vector<long long int>, std::greater<long long int> >)>::function(main()::<lambda(pq&, pq&)>)'
   92 |         function<bool(pq,pq) > cmp([](pq &a, pq &b) {return a.top() > b.top(); });
      |                                                                                 ^
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::priority_queue<long long int, std::vector<long long int, std::allocator<long long int> >, std::greater<long long int> >, std::priority_queue<long long int, std::vector<long long int, std::allocator<long long int> >, std::greater<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::priority_queue<long long int, std::vector<long long int>, std::greater<long long int> >, std::priority_queue<long long int, std::vector<long long int>, std::greater<long long int> >)>::_Callable<main()::<lambda(pq&, pq&)>, main()::<lambda(pq&, pq&)>, std::__invoke_result<main()::<lambda(pq&, pq&)>&, std::priority_queue<long long int, std::vector<long long int, std::allocator<long long int> >, std::greater<long long int> >, std::priority_queue<long long int, std::vector<long long int, std::allocator<long long int> >, std::greater<long long int> > > >; _Tp = void; _Res = bool; _ArgTypes = {std::priority_queue<long long int, std::vector<long long int, std::allocator<long long int> >, std::greater<long long int> >, std::priority_queue<long long int, std::vector<long long int, std::allocator<long long int> >, std::greater<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::priority_queue<long long int, std::vector<long long int, std::allocator<long long int> >, std::greater<long long int> >, std::priority_queue<long long int, std::vector<long long int, std::allocator<long long int> >, std::greater<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(pq&, pq&)>' to 'std::function<bool(std::priority_queue<long long int, std::vector<long long int>, std::greater<long long int> >, std::priority_queue<long long int, std::vector<long long int>, std::greater<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::priority_queue<long long int, std::vector<long long int, std::allocator<long long int> >, std::greater<long long int> >, std::priority_queue<long long int, std::vector<long long int, std::allocator<long long int> >, std::greater<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(pq&, pq&)>' to 'const std::function<bool(std::priority_queue<long long int, std::vector<long long int>, std::greater<long long int> >, std::priority_queue<long long int, std::vector<long long int>, std::greater<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::priority_queue<long long int, std::vector<long long int, std::allocator<long long int> >, std::greater<long long int> >, std::priority_queue<long long int, std::vector<long long int, std::allocator<long long int> >, std::greater<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(pq&, pq&)>' 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::priority_queue<long long int, std::vector<long long int, std::allocator<long long int> >, std::greater<long long int> >, std::priority_queue<long long int, std::vector<long long int, std::allocator<long long int> >, std::greater<long long int> >}]'
  368 |       function() noexcept
      |       ^~~~~~~~
/usr/include/c++/14/bits/std_function.h:368:7: note:   candidate expects 0 arguments, 1 provided