Language: C++

#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef pair <int,int> P;
 
struct UF
{
	int par[SIZE],rank[SIZE];
	
	void init(int n)
	{
		for(int i=0;i<n;i++)
		{
			par[i]=i;
			rank[i]=0;
		}
	}
	int find(int x)
	{
		if(par[x]==x) return x;
		return par[x]=find(par[x]);
	}
	void unite(int x,int y)
	{
		x=find(x);
		y=find(y);
		if(x==y) return;
		if(rank[x]<rank[y]) par[x]=y;
		else
		{
			par[y]=x;
			if(rank[x]==rank[y]) rank[x]++;
		}
	}
	bool same(int x,int y)
	{
		return find(x)==find(y);
	}
};
UF uf;
int A[SIZE];
vector <int> vec[SIZE];
 
int main()
{
	int n,m;
	scanf("%d %d",&n,&m);
	uf.init(n+2);
	for(int i=0;i<n;i++)
	{
		scanf("%d",&A[i]);
	}
	for(int i=0;i<m;i++)
	{
		int x,y;
		scanf("%d %d",&x,&y);
		uf.unite(x,y);
	}
	int cnt=0;
	for(int i=0;i<n;i++)
	{
		if(uf.find(i)==i) cnt++;
		int p=uf.find(i);
		vec[p].push_back(A[i]);
	}
	if((cnt-1)*2>n)
	{
		puts("Impossible");
		return 0;
	}
	if(cnt==1)
	{
		puts("0");
		return 0;
	}
	ll ret=0;
	vector <int> vt;
	for(int i=0;i<n;i++)
	{
		if(uf.find(i)==i)
		{
			sort(vec[i].begin(),vec[i].end());
			ret+=vec[i][0];
			for(int j=1;j<vec[i].size();j++) vt.push_back(vec[i][j]);
		}
	}
	sort(vt.begin(),vt.end());
	for(int i=0;i<cnt-2;i++) ret+=vt[i];
	printf("%lld\n",ret);
	return 0;
}

Compiler output:
a.cc:9:17: error: 'SIZE' was not declared in this scope
    9 |         int par[SIZE],rank[SIZE];
      |                 ^~~~
a.cc:9:28: error: 'SIZE' was not declared in this scope
    9 |         int par[SIZE],rank[SIZE];
      |                            ^~~~
a.cc: In member function 'void UF::init(int)':
a.cc:15:25: error: 'par' was not declared in this scope; did you mean '__pstl::execution::v1::par'?
   15 |                         par[i]=i;
      |                         ^~~
      |                         __pstl::execution::v1::par
In file included from /usr/include/c++/14/pstl/glue_algorithm_defs.h:15,
                 from /usr/include/c++/14/algorithm:86,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/pstl/execution_defs.h:43:45: note: '__pstl::execution::v1::par' declared here
   43 | _GLIBCXX17_INLINE constexpr parallel_policy par{};
      |                                             ^~~
a.cc:16:30: error: redeclaration of 'auto i'
   16 |                         rank[i]=0;
      |                              ^
a.cc:13:25: note: 'int i' previously declared here
   13 |                 for(int i=0;i<n;i++)
      |                         ^
a.cc:16:33: error: class template argument deduction failed:
   16 |                         rank[i]=0;
      |                                 ^
a.cc:16:33: error: no matching function for call to 'rank(int)'
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:
/usr/include/c++/14/type_traits:1437:12: note: candidate: 'template<class> rank()-> std::rank< <template-parameter-1-1> >'
 1437 |     struct rank
      |            ^~~~
/usr/include/c++/14/type_traits:1437:12: note:   candidate expects 0 arguments, 1 provided
/usr/include/c++/14/type_traits:1437:12: note: candidate: 'template<class> rank(std::rank< <template-parameter-1-1> >)-> std::rank< <template-parameter-1-1> >'
/usr/include/c++/14/type_traits:1437:12: note:   template argument deduction/substitution failed:
a.cc:16:33: note:   mismatched types 'std::rank< <template-parameter-1-1> >' and 'int'
   16 |                         rank[i]=0;
      |                                 ^
a.cc: In member function 'int UF::find(int)':
a.cc:21:20: error: 'par' was not declared in this scope; did you mean '__pstl::execution::v1::par'?
   21 |                 if(par[x]==x) return x;
      |                    ^~~
      |                    __pstl::execution::v1::par
/usr/include/c++/14/pstl/execution_defs.h:43:45: note: '__pstl::execution::v1::par' declared here
   43 | _GLIBCXX17_INLINE constexpr parallel_policy par{};
      |                                             ^~~
a.cc:22:24: error: 'par' was not declared in this scope; did you mean '__pstl::execution::v1::par'?
   22 |                 return par[x]=find(par[x]);
      |                        ^~~
      |                        __pstl::execution::v1::par
/usr/include/c++/14/pstl/execution_defs.h:43:45: note: '__pstl::execution::v1::par' declared here
   43 | _GLIBCXX17_INLINE constexpr parallel_policy par{};
      |                                             ^~~
a.cc: In member function 'void UF::unite(int, int)':
a.cc:29:24: error: expected unqualified-id before '[' token
   29 |                 if(rank[x]<rank[y]) par[x]=y;
      |                        ^
a.cc:29:37: error: 'par' was not declared in this scope; did you mean '__pstl::execution::v1::par'?
   29 |                 if(rank[x]<rank[y]) par[x]=y;
      |                                     ^~~
      |                                     __pstl::execution::v1::par
/usr/include/c++/14/pstl/execution_defs.h:43:45: note: '__pstl::execution::v1::par' declared here
   43 | _GLIBCXX17_INLINE constexpr parallel_policy par{};
      |                                             ^~~
a.cc:32:25: error: 'par' was not declared in this scope; did you mean '__pstl::execution::v1::par'?
   32 |                         par[y]=x;
      |                         ^~~
      |                         __pstl::execution::v1::par
/usr/include/c++/14/pstl/execution_defs.h:43:45: note: '__pstl::execution::v1::par' declared here
   43 | _GLIBCXX17_INLINE constexpr parallel_policy par{};
      |                                             ^~~
a.cc:33:32: error: expected unqualified-id before '[' token
   33 |                         if(rank[x]==rank[y]) rank[x]++;
      |                                ^
a.cc:33:53: error: expected initializer before '++' token
   33 |                         if(rank[x]==rank[y]) rank[x]++;
      |                                                     ^~
a.cc:33:53: error: expected ';' before '++' token
a.cc: At global scope:
a.cc:42:7: error: 'SIZE' was not declared in this scope
   42 | int A[SIZE];
      |       ^~~~
a.cc:43:18: error: 'SIZE' was not declared in this scope
   43 | vector <int> vec[SIZE];
      |                  ^~~~
a.cc: In function 'int main()':
a.cc:52:29: error: 'A' was not declared in this scope
   52 |                 scanf("%d",&A[i]);
      |                             ^
a.cc:65:17: error: 'vec' was not declared in this scope
   65 |                 vec[p].push_back(A[i]);
      |                 ^~~
a.cc:65:34: error: 'A' was not declared in this scope
   65 |                 vec[p].push_back(A[i]);
      |                                  ^
a.cc:83:30: error: 'vec' was not declared in this scope
   83 |                         sort(vec[i].begin(),vec[i].end());
      |                              ^~~