Language: C++

#include <vector>
#include <iostream>
#include <queue>
#include <map>
#include <functional>

#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]);
	}
	auto  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:97:70: error: no matching function for call to 'std::priority_queue<std::priority_queue<long long int, std::vector<long long int>, std::greater<long long int> >, std::vector<std::priority_queue<long long int, std::vector<long long int>, std::greater<long long int> > >, 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> >)> >::priority_queue(main()::<lambda(pq&, pq&)>&)'
   97 |         priority_queue<pq, vector<pq>, function<bool(pq,pq)> >  q(cmp);
      |                                                                      ^
In file included from /usr/include/c++/14/queue:66,
                 from a.cc:3:
/usr/include/c++/14/bits/stl_queue.h:691:9: note: candidate: 'template<class _InputIterator, class _Alloc, class _Requires> std::priority_queue<_Tp, _Sequence, _Compare>::priority_queue(_InputIterator, _InputIterator, const _Compare&, _Sequence&&, const _Alloc&) [with _Alloc = _InputIterator; _Requires = _Alloc; _Tp = std::priority_queue<long long int, std::vector<long long int>, std::greater<long long int> >; _Sequence = std::vector<std::priority_queue<long long int, std::vector<long long int>, std::greater<long long int> > >; _Compare = 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> >)>]'
  691 |         priority_queue(_InputIterator __first, _InputIterator __last,
      |         ^~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_queue.h:691:9: note:   candidate expects 5 arguments, 1 provided
/usr/include/c++/14/bits/stl_queue.h:679:9: note: candidate: 'template<class _InputIterator, class _Alloc, class, class _Requires> std::priority_queue<_Tp, _Sequence, _Compare>::priority_queue(_InputIterator, _InputIterator, const _Compare&, const _Sequence&, const _Alloc&) [with _Alloc = _InputIterator; <template-parameter-2-3> = _Alloc; _Requires = <template-parameter-1-3>; _Tp = std::priority_queue<long long int, std::vector<long long int>, std::greater<long long int> >; _Sequence = std::vector<std::priority_queue<long long int, std::vector<long long int>, std::greater<long long int> > >; _Compare = 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> >)>]'
  679 |         priority_queue(_InputIterator __first, _InputIterator __last,
      |         ^~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_queue.h:679:9: note:   candidate expects 5 arguments, 1 provided
/usr/include/c++/14/bits/stl_queue.h:671:9: note: candidate: 'template<class _InputIterator, class _Alloc, class, class _Requires> std::priority_queue<_Tp, _Sequence, _Compare>::priority_queue(_InputIterator, _InputIterator, const _Compare&, const _Alloc&) [with _Alloc = _InputIterator; <template-parameter-2-3> = _Alloc; _Requires = <template-parameter-1-3>; _Tp = std::priority_queue<long long int, std::vector<long long int>, std::greater<long long int> >; _Sequence = std::vector<std::priority_queue<long long int, std::vector<long long int>, std::greater<long long int> > >; _Compare = 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> >)>]'
  671 |         priority_queue(_InputIterator __first, _InputIterator __last,
      |         ^~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_queue.h:671:9: note:   candidate expects 4 arguments, 1 provided
/usr/include/c++/14/bits/stl_queue.h:663:9: note: candidate: 'template<class _InputIterator, class _Alloc, class, class _Requires> std::priority_queue<_Tp, _Sequence, _Compare>::priority_queue(_InputIterator, _InputIterator, const _Alloc&) [with _Alloc = _InputIterator; <template-parameter-2-3> = _Alloc; _Requires = <template-parameter-1-3>; _Tp = std::priority_queue<long long int, std::vector<long long int>, std::greater<long long int> >; _Sequence = std::vector<std::priority_queue<long long int, std::vector<long long int>, std::greater<long long int> > >; _Compare = 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> >)>]'
  663 |         priority_queue(_InputIterator __first, _InputIterator __last,
      |         ^~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_queue.h:663:9: note:   candidate expects 3 arguments, 1 provided
/usr/include/c++/14/bits/stl_queue.h:649:9: note: candidate: 'template<class _InputIterator, class> std::priority_queue<_Tp, _Sequence, _Compare>::priority_queue(_InputIterator, _InputIterator, const _Compare&, _Sequence&&) [with <template-parameter-2-2> = _InputIterator; _Tp = std::priority_queue<long long int, std::vector<long long int>, std::greater<long long int> >; _Sequence = std::vector<std::priority_queue<long long int, std::vector<long long int>, std::greater<long long int> > >; _Compare = 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> >)>]'
  649 |         priority_queue(_InputIterator __first, _InputIterator __last,
      |         ^~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_queue.h:649:9: note:   candidate expects 4 arguments, 1 provided
/usr/include/c++/14/bits/stl_queue.h:638:9: note: candidate: 'template<class _InputIterator, class> std::priority_queue<_Tp, _Sequence, _Compare>::priority_queue(_InputIterator, _InputIterator, const _Compare&, const _Sequence&) [with <template-parameter-2-2> = _InputIterator; _Tp = std::priority_queue<long long int, std::vector<long long int>, std::greater<long long int> >; _Sequence = std::vector<std::priority_queue<long long int, std::vector<long long int>, std::greater<long long int> > >; _Compare = 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> >)>]'
  638 |         priority_queue(_InputIterator __first, _InputIterator __last,
      |         ^~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_queue.h:638:9: note:   candidate expects 4 arguments, 1 provided
/usr/include/c++/14/bits/stl_queue.h:629:9: note: candidate: 'template<class _InputIterator, class> std::priority_queue<_Tp, _Sequence, _Compare>::priority_queue(_InputIterator, _InputIterator, const _Compare&) [with <template-parameter-2-2> = _InputIterator; _Tp = std::priority_queue<long long int, std::vector<long long int>, std::greater<long long int> >; _Sequence = std::vector<std::priority_queue<long long int, std::vector<long long int>, std::greater<long long int> > >; _Compare = 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> >)>]'
  629 |         priority_queue(_InputIterator __first, _InputIterator __last,
      |         ^~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_queue.h:629:9: note:   candidate expects 2 arguments, 1 provided
/usr/include/c++/14/bits/stl_queue.h:594:9: note: candidate: 'template<class _Alloc, class _Requires> std::priority_queue<_Tp, _Sequence, _Compare>::priority_queue(std::priority_queue<_Tp, _Sequence, _Compare>&&, const _Alloc&) [with _Requires = _Alloc; _Tp = std::priority_queue<long long int, std::vector<long long int>, std::greater<long long int> >; _Sequence = std::vector<std::priority_queue<long long int, std::vector<long long int>, std::greater<long long int> > >; _Compare = 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> >)>]'
  594 |         priority_queue(priority_queue&& __q, const _Alloc& __a)
      |         ^~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_queue.h:594:9: note:   candidate expects 2 arguments, 1 provided
/usr/include/c++/14/bits/stl_queue.h:590:9: note: candidate: 'template<class _Alloc, class _Requires> std::priority_queue<_Tp, _Sequence, _Compare>::priority_queue(const std::priority_queue<_Tp, _Sequence, _Compare>&, const _Alloc&) [with _Requires = _Alloc; _Tp = std::priority_queue<long long int, std::vector<long long int>, std::greater<long long int> >; _Sequence = std::vector<std::priority_queue<long long int, std::vector<long long int>, std::greater<long long int> > >; _Compare = 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> >)>]'
  590 |         priority_queue(const priority_queue& __q, const _Alloc& __a)
      |         ^~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_queue.h:590:9: note:   candidate expects 2 arguments, 1 provided
/usr/include/c++/14/bits/stl_queue.h:585:9: note: candidate: 'template<class _Alloc, class _Requires> std::priority_queue<_Tp, _Sequence, _Compare>::priority_queue(const _Compare&, _Sequence&&, const _Alloc&) [with _Requires = _Alloc; _Tp = std::priority_queue<long long int, std::vector<long long int>, std::greater<long long int> >; _Sequence = std::vector<std::priority_queue<long long int, std::vector<long long int>, std::greater<long long int> > >; _Compare = 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> >)>]'
  585 |         priority_queue(const _Compare& __x, _Sequence&& __c, const _Alloc& __a)
      |         ^~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_queue.h:585:9: note:   candidate expects 3 arguments, 1 provided
/usr/include/c++/14/bits/stl_queue.h: