Language: C++

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define pb push_back
#define mp make_pair
#define rep(i,n) for(int i = 0; i < (n); i++)
#define repr(i,b,e) for(int i = (b); i <= (e); i++)

#define pr(x) do{cerr << (#x) << " = " << (x) << endl;}while(0)
#define pri(x,i) do{cerr << (#x) << "[" << i << "] = " << (x[i]) << endl;}while(0)

namespace std {
	template<class S, class T> pair<S, T> operator + (const pair<S, T> &a, const pair<S, T> &b) {return mp<S, T>(a.first + b.first, a.second + b.second);}
	template<class S, class T> pair<S, T> operator - (const pair<S, T> &a, const pair<S, T> &b) {return mp<S, T>(a.first - b.first, a.second - b.second);}
	template<class S, class T> pair<S, T>& operator += (pair<S, T> &a, const pair<S, T> &other) {return a = a + other;}
	template<class S, class T> pair<S, T>& operator -= (pair<S, T> &a, const pair<S, T> &other) {return a = a - other;}
	template<class S, class T> ostream& operator << (ostream& ost, pair<S, T> p) {ost << "(" << p.first << ", " << p.second << ")";return ost;}
	template<class T> ostream& print_iterable_container(ostream& ost, T& container) {
		for (auto&& iter = begin(container); iter != end(container); ++iter) ost << (iter == begin(container) ? " [ " : ", ") << (*iter);
		return ost << " ]";
	}
#	define APPLY_PRINT_ITERABLE_CONTAINER(container_type)\
	template<class T> ostream& operator << (ostream& ost, container_type<T>& container) {\
		return print_iterable_container(ost, container);\
	}
	APPLY_PRINT_ITERABLE_CONTAINER(vector);
	APPLY_PRINT_ITERABLE_CONTAINER(list);
	APPLY_PRINT_ITERABLE_CONTAINER(deque);
}

template<class T> inline bool CHMAX(T &l,const T &r){return l<r?l=r,1:0;}
template<class T> inline bool CHMIN(T &l,const T &r){return l>r?l=r,1:0;}

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vi>;
using vvll = vector<vll>;
using vpii = vector<pii>;
using vpll = vector<pll>;

using ld = long double;

template<class T> using VV = vector<vector<T> >;
template<class T> using maxPQ = priority_queue<T>;
template<class T> using minPQ = priority_queue<T, vector<T>, greater<T> >;
template<class T, class U> using uomap = unordered_map<T, U>;
template<class T> using uoset = unordered_set<T>;

const ll INF = 1001001001;
const ll INFll = (ll)INF * INF;
const ll MOD = 1000000007;
const ll MOD_FFT = 998244353;
const ld EPS = 1e-15;

//function<void(int,int)> tensi_no_3p=[&](int v,int d){
//};


namespace ds {
	struct UnionFind {
		vector<int> par, rank;
		int n_merges;

		UnionFind(int n) : par(n), rank(n, 1), n_merges(0) {
			iota(begin(par), end(par), 0);
		}
		void merge(int a, int b) {
			int x = group(a), y = group(b);
			if (x == y) return;  //same
			n_merges++;
			if (rank[x] < rank[y]) par[x] = y;
			else if (rank[x] > rank[y]) par[y] = x;
			else { par[x] = y; rank[x]++; }
		}
		int group(int a) {
			if (par[a] == a) return a;
			return par[a] = group(par[a]);
		}
		bool same(int a, int b) {
			return group(a) == group(b);
		}
	};
}

int main() {
	int N; cin >> N;
	vi A(N), B(N);
	rep(i, N) cin >> A[i] >> B[i];
	minPQ<pair<pii, int> > minval;
	maxPQ<pii> maxval[2];
	rep(i, N) {
		minval.push(mp(mp(A[i], i), 0));
		minval.push(mp(mp(B[i], i), 1));
		maxval[0].push(mp(A[i], i));
		maxval[1].push(mp(B[i], i));
	}
	vvi used(N, vi(2));
	ds::UnionFind uf(N);
	ll ans = 0;
	rep(i, N) {
		while (!minval.empty() && used[minval.top().first.second][minval.top().second]) minval.pop();
		while (!maxval[0].empty() && used[maxval[0].top().second][0]) maxval[0].pop();
		while (!maxval[1].empty() && used[maxval[1].top().second][1]) maxval[1].pop();

		pair<pii, int> top;
		for(;;) {
			top = minval.top();
			minval.pop();
			if (! used[top.first.second][top.second]) {
				used[top.first.second][top.second] = 1;
				break;
			}
		}
		ans += top.first.first;
		if (i == N - 1) break;
		int ind = top.first.second;
		int side = top.second;
		int opp = maxval[1 - side].top().second;
		while (uf.same(ind, opp)) {
			auto temp = maxval[1 - side].top();
			maxval[1 - side].pop();
			opp = maxval[1 - side].top().second;
			maxval[1 - side].pop();
			maxval[1 - side].push(temp);
		}
		else {
			maxval[1 - side].pop();
		}
		uf.merge(ind, opp);
		used[opp][1 - side] = 1;
	}
	cout << ans << endl;
	exit(0);
	return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:147:17: error: 'else' without a previous 'if'
  147 |                 else {
      |                 ^~~~