Language: C++

#include <bits/stdc++.h>

using namespace std;

#ifdef DEBUG_MODE
	#define DBG(n) n;
#else
	#define DBG(n) ;
#endif
#define REP(i,n) for(ll (i) = (0);(i) < (n);++i)
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {for(int W = 0;W < (n);W++)cerr << v[W] << ' ';cerr << endl << endl;}
#define SHOW2d(v,i,j) {for(int aaa = 0;aaa < i;aaa++){for(int bbb = 0;bbb < j;bbb++)cerr << v[aaa][bbb] << ' ';cerr << endl;}cerr << endl;}
#define ALL(v) v.begin(),v.end()
#define Decimal fixed<<setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000
#define MOD 1000000007

typedef long long ll;
typedef pair<ll,ll> P;

struct UF
{
	vector<int> par; // 親のインデックスを記憶する配列
	vector<int> sz; // サイズを記憶する。
	vector<int> rank;
	// 初期化
	UF(int n):par(n),sz(n),rank(n){
		for(int i = 0; i < n; i++){
			par[i] = i;sz[i] = 1;rank[i] = 0;
		}
	}
	// 親を求める
	int find(int x) {
		if (par[x] == x) return x;
		else return par[x] = find(par[x]);
	}
	// xとyの属する集合を併合
	void unite(int x, int y) {
		x = find(x); y = find(y);
		if (x == y) return;
		if(rank[x] < rank[y]){
			par[x] = y;
			sz[y] += sz[x];
		}
		else{
			par[y] = x;
			sz[x] += sz[y];
			if(rank[x] == rank[y])rank[x]++;
		}
	}
	// xとyが同じ集合ならtrue
	bool same(int x, int y) { return find(x) == find(y); }
	// 素の集合のサイズを求める
	int size(int n){return sz[find(n)];}
};

bool used[111111];

int main(){
	
	int n;cin >> n;
	UF uf(n);
	ll ans = 0;
	vector<pair<ll,ll>> A;
	vector<pair<ll,ll>> B;
	REP(i,n){
		ll a,b;cin >> a >> b;
		ans += a;
		A.PB(MP(a,i));
		B.PB(MP(b,i));
	}
	
	sort(ALL(A),greater<pair<ll,ll>>());
	sort(ALL(B));
	int cou = 0;
	
	REP(i,n){
		//cout << A[i].FI << " " << A[i].SE << " " << mp[A[i].SE] << endl;
		while(used[B[cou].SE])cou++;
		int tmp = cou;
		while(A[i].SE == B[tmp].SE || uf.same(B[tmp].SE,A[i].SE) || used[B[tmp].SE])tmp++;
		if(tmp < n - 1 && A[i].FI > B[tmp].FI){
			ans -= A[i].FI;
			ans += B[tmp].FI;				
			used[B[tmp].SE] = true;
			uf.unite(B[tmp].SE,A[i].SE);
		}
	}
		
	//cout << ans << endl;
	
	ll seica = 0;
	REP(i,n)seica += B[i].FI;
	UF uff(n);
	REP(i,111111)used[i] = false;
	sort(ALL(B),greater<pair<ll,ll>>());
	sort(ALL(A));
	cou = 0;
	
	REP(i,n){
		//cout << A[i].FI << " " << A[i].SE << " " << mp[A[i].SE] << endl;
		while(used[A[cou].SE])cou++;
		int tmp = cou;
		while(tmp < n && (B[i].SE == A[tmp].SE || uuf.same(A[tmp].SE,B[i].SE) || used[A[tmp].SE]))tmp++;
		if(tmp < n - 1 && B[i].FI > A[tmp].FI){
			seica -= B[i].FI;
			seica += A[tmp].FI;				
			used[A[tmp].SE] = true;
			uuf.unite(A[tmp].SE,B[i].SE);
		}
	}
	cout << min(ans,seica) << endl;	

	
	return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:109:59: error: 'uuf' was not declared in this scope; did you mean 'uff'?
  109 |                 while(tmp < n && (B[i].SE == A[tmp].SE || uuf.same(A[tmp].SE,B[i].SE) || used[A[tmp].SE]))tmp++;
      |                                                           ^~~
      |                                                           uff
a.cc:114:25: error: 'uuf' was not declared in this scope; did you mean 'uff'?
  114 |                         uuf.unite(A[tmp].SE,B[i].SE);
      |                         ^~~
      |                         uff