Language: C++

#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <utility>
#include <queue>
#include <set>
#include <map>

using namespace std;
typedef  long long ll;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define  MP make_pair
#define  PB push_back
#define inf  1000000007
#define rep(i,n) for(int i=0;i<(int)(n);++i)


int p[100002];
int r[100002];

void init(int n){
    for (int i = 0; i < n; ++i) {
        p[i]=i;
        r[i]=0;
    }
}


int find(int x){
	if(x != p[x]){
		p[x] = find(p[x]);
    }
   	return p[x];
}
     
void union_set(int x,int y){
	x = find(x);
	y = find(y);
	if(x==y) return;
	if(r[x] < r[y]){
		p[x] = y;	
	}else{
		p[y] = x;
		if(r[x] == r[y])r[x]++;
	}
	return ;
}

int main(){
	int n,m;
	cin >> n >> m;
	vector<ll>a(n);
	for(int i=0;i<n;i++){
		cin >> a[i];
	}
	if(n==1){
		cout << 0 << endl;
		return 0;
	}
	if(n==2&&m==1){
		cout << 0 << endl;
		return 0;
	}
	init(n);
	for(int i=0;i<m;i++){#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <utility>
#include <queue>
#include <set>
#include <map>

using namespace std;
typedef  long long ll;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define  MP make_pair
#define  PB push_back
#define inf  1000000007
#define rep(i,n) for(int i=0;i<(int)(n);++i)


int p[100002];
int r[100002];

void init(int n){
    for (int i = 0; i < n; ++i) {
        p[i]=i;
        r[i]=0;
    }
}


int find(int x){
	if(x != p[x]){
		p[x] = find(p[x]);
    }
   	return p[x];
}
     
void union_set(int x,int y){
	x = find(x);
	y = find(y);
	if(x==y) return;
	if(r[x] < r[y]){
		p[x] = y;	
	}else{
		p[y] = x;
		if(r[x] == r[y])r[x]++;
	}
	return ;
}

int main(){
	int n,m;
	cin >> n >> m;
	vector<ll>a(n);
	for(int i=0;i<n;i++){
		cin >> a[i];
	}
	if(n==1){
		cout << 0 << endl;
		return 0;
	}
	if(n==2&&m==1){
		cout << 0 << endl;
		return 0;
	}
	init(n);
	for(int i=0;i<m;i++){
		int x,y;
		cin >> x >> y;
		union_set(x,y);
	}
	vector<ll>v(n,inf);
	vector<ll>w;
	set<int> st;
	for(int i=0;i<n;i++){
		int t = find(i);
		st.insert(t);
		if(v[t]!=inf){
			if(v[t]>a[i]){
				w.push_back(v[t]);
				v[t] = a[i];
			}else{
				w.push_back(a[i]);
			}
		}else{
			v[t] = a[i];
		}
	}
	ll ans = 0 ;
	for(int i=0;i<n;i++){
		if(v[i]!=inf){
			ans += v[i];
		}
	}
	int cnt = st.size();
	if(st.size()==1){
		cout << 0 << endl;
		return 0;
	}
	if(2*(cnt-1)>n){
		cout << "Impossible" << endl;
	}else{
		sort(w.begin(),w.end());
		for(int i=0;i<cnt-2;i++){
			ans += w[i];
		}
		cout << ans << endl;
	}
	return 0;
}
		int x,y;
		cin >> x >> y;
		union_set(x,y);
	}
	vector<ll>v(n,inf);
	vector<ll>w;
	set<int> st;
	for(int i=0;i<n;i++){
		int t = find(i);
		st.insert(t);
		if(v[t]!=inf){
			if(v[t]>a[i]){
				w.push_back(v[t]);
				v[t] = a[i];
			}else{
				w.push_back(a[i]);
			}
		}else{
			v[t] = a[i];
		}
	}
	ll ans = 0 ;
	for(int i=0;i<n;i++){
		if(v[i]!=inf){
			ans += v[i];
		}
	}
	int cnt = st.size();
	if(2*(cnt-1)>n){
		cout << "Impossible" << endl;
	}else{
		sort(w.begin(),w.end());
		for(int i=0;i<cnt-2;i++){
			ans += w[i];
		}
		cout << ans << endl;
	}
	return 0;
}

Compiler output:
a.cc:69:30: error: stray '#' in program
   69 |         for(int i=0;i<m;i++){#include <iostream>
      |                              ^
a.cc: In function 'int main()':
a.cc:69:31: error: 'include' was not declared in this scope
   69 |         for(int i=0;i<m;i++){#include <iostream>
      |                               ^~~~~~~
a.cc:69:48: error: expected primary-expression before '>' token
   69 |         for(int i=0;i<m;i++){#include <iostream>
      |                                                ^
a.cc:79:1: error: expected primary-expression before 'using'
   79 | using namespace std;
      | ^~~~~
a.cc:93:17: error: a function-definition is not allowed here before '{' token
   93 | void init(int n){
      |                 ^
a.cc:101:16: error: a function-definition is not allowed here before '{' token
  101 | int find(int x){
      |                ^
a.cc:108:28: error: a function-definition is not allowed here before '{' token
  108 | void union_set(int x,int y){
      |                            ^
a.cc:121:9: warning: empty parentheses were disambiguated as a function declaration [-Wvexing-parse]
  121 | int main(){
      |         ^~
a.cc:121:9: note: remove parentheses to default-initialize a variable
  121 | int main(){
      |         ^~
      |         --
a.cc:121:9: note: or replace parentheses with braces to value-initialize a variable
a.cc:121:11: error: a function-definition is not allowed here before '{' token
  121 | int main(){
      |           ^