Language: C++

#include <iostream>
#include <cstring>
#include <queue>

using namespace std;

const int N = 200005, inf = 3e9;
int n, m, prev[N], target[N], last[N], t, a[N], cnt, minn[N], f[N], ans;
priority_queue <int, vector<int>, greater<int> > q;
void add(int x, int y)
{
	t++;
	prev[t] = last[x];
	target[t] = y;
	last[x] = t;
}

bool vis[N];
void dfs1(int x, int fa)
{
	f[x] = fa;
	vis[x] = 1;
	minn[fa] = min(minn[fa], a[x]);
	for (int ptr = last[x]; ptr; ptr = prev[ptr])
	{
		int xx = target[ptr];
		if (!vis[xx])
			dfs1(xx, fa);
	}
}

void dfs2(int x, int fa)
{
	vis[x] = 1;
	if (a[x] != minn[fa]) q.push(a[x]);
	for (int ptr = last[x]; ptr; ptr = prev[ptr])
	{
		int xx = target[ptr];
		if (!vis[xx])
			dfs2(xx, fa);
	}
}

int main()
{
	cin >> n >> m;
	for (int i = 1; i <= n; i++) cin >> a[i];
	for (int i = 1; i <= m; i++)
	{
		int x, y;
		cin >> x >> y;
		x++, y++;
		add(x, y);
		add(y, x);
	}
	for (int i = 1; i <= n; i++) minn[i] = inf;
	for (int i = 1; i <= n; i++)
	{
		if (!f[i])
		{
			cnt++;
			dfs1(i, i);
			memset(vis, 0, sizeof(vis));
			dfs2(i, i);
			memset(vis, 0, sizeof(vis));
		}
	}
	if (cnt == 1)
	{
		cout << 0 << endl;
		return 0;
	}
	int now = 0;
	while (!q.empty() && now < cnt - 2)
	{
		now++;
		ans += q.top();
		q.pop();
	}
	if (now < cnt - 2)
	{
		cout << "Impossible" << endl;
		return 0;
	}
	for (int i = 1; i <= n; i++)
		if (f[i] == i) ans += minn[i];
	cout << ans << endl;
	return 0;
}


Compiler output:
a.cc:7:29: warning: overflow in conversion from 'double' to 'int' changes value from '3.0e+9' to '2147483647' [-Woverflow]
    7 | const int N = 200005, inf = 3e9;
      |                             ^~~
a.cc: In function 'void add(int, int)':
a.cc:13:9: error: reference to 'prev' is ambiguous
   13 |         prev[t] = last[x];
      |         ^~~~
In file included from /usr/include/c++/14/string:47,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:8:11: note:                 'int prev [200005]'
    8 | int n, m, prev[N], target[N], last[N], t, a[N], cnt, minn[N], f[N], ans;
      |           ^~~~
a.cc: In function 'void dfs1(int, int)':
a.cc:24:44: error: reference to 'prev' is ambiguous
   24 |         for (int ptr = last[x]; ptr; ptr = prev[ptr])
      |                                            ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:8:11: note:                 'int prev [200005]'
    8 | int n, m, prev[N], target[N], last[N], t, a[N], cnt, minn[N], f[N], ans;
      |           ^~~~
a.cc: In function 'void dfs2(int, int)':
a.cc:36:44: error: reference to 'prev' is ambiguous
   36 |         for (int ptr = last[x]; ptr; ptr = prev[ptr])
      |                                            ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:8:11: note:                 'int prev [200005]'
    8 | int n, m, prev[N], target[N], last[N], t, a[N], cnt, minn[N], f[N], ans;
      |           ^~~~