Language: C++

#pragma GCC optimize("Ofast,inline")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx,avx2,tune=native")
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<deque>
#define ll long long
#define mp make_pair
#define rep(i,x,y) for(int i=(x);i<=(y);++i)
#define r0p(i,n) for(int i=0;i<n;++i)
#pragma GCC optimize(1)
#pragma G++ optimize(1)
#pragma GCC optimize(2)
#pragma G++ optimize(2)
#pragma GCC optimize(3)
#pragma G++ optimize(3)
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#define pii pair<int,int>
#pragma GCC diagnostic error "-std=c++11"
using namespace std;
inline void rdl(long long &val){
    long long x=0;
    int f=1;
    char ch=getchar();
    while((ch<'0' or ch>'9') and ch!='-') ch=getchar();
    if(ch=='-'){
        f=-1;
        ch=getchar();
	}
	while(ch>='0' and ch<='9'){
	    x=(x<<3)+(x<<1)+ch-'0';
		ch=getchar();
	}
    val=x*f;
}
inline void rdi(int &val){
    int x=0;
    int f=1;
    char ch=getchar();
    while((ch<'0' or ch>'9') and ch!='-') ch=getchar();
    if(ch=='-'){
        f=-1;
        ch=getchar();
    }
    while(ch>='0' and ch<='9'){
        x=(x<<3)+(x<<1)+ch-'0';
        ch=getchar();
    }
    val=x*f;
}
inline long long rdl(){
    long long x=0;
    int f=1;
    char ch=getchar();
    while((ch<'0' or ch>'9') and ch!='-') ch=getchar();
    if(ch=='-'){
        f=-1;
        ch=getchar();
    }
    while(ch>='0' and ch<='9'){
        x=(x<<3)+(x<<1)+ch-'0';
        ch=getchar();
    }
    return x*f;
}
inline int rdi(){
    int x=0;
    int f=1;
	char ch=getchar();
	while((ch<'0' or ch>'9') and ch!='-') ch=getchar();
	if(ch=='-'){
        f=-1;
        ch=getchar();
    }
    while(ch>='0' and ch<='9'){
        x=(x<<3)+(x<<1)+ch-'0';
        ch=getchar();
    }
    return x*f;
}
inline void write(int x){
    if(x<0) putchar('-'),x=-x;
    if(x>9) write(x/10);
    putchar(x%10+'0');
}
inline void _write(int x){
	write(x);
	putchar(' ');
}
inline void print(int x){
	write(x);
	putchar('\n');
}
inline void write(ll x){
    if(x<0) putchar('-'),x=-x;
    if(x>9) write(x/10);
    putchar(x%10+'0');
}
inline void _write(ll x){
	write(x);
	putchar(' ');
}
inline void print(ll x){
	write(x);
	putchar('\n');
}
int n;
int a[100005],b[100005];
bool uf[100005],us[100005];
multiset<int> nu,u;
vector<int> all;
int main(){
	rdi(n);
	rep(i,1,n) rdi(a[i]),rdi(b[i]),all.push_back(a[i]),all.push_back(b[i]);
	sort(all.begin(),all.end());
	ll cnt=0;
	for(int i=0;i<n;++i) u.insert(all[i]),cnt+=all[i];
	for(int i=n;i<all.size();++i) nu.insert(all[i]);
	rep(i,1,n){
		if(u.count(a[i])){
			uf[i]=1;
			u.erase(u.find(a[i]));
		}
		if(u.count(b[i])){
			us[i]=1;
			u.erase(u.find(b[i]));
		}
	}
	vector<int>v0,v1,v2,v3;
	rep(i,1,n){
		if(uf[i] and us[i]) v3.push_back(i);
		else if(uf[i]) v1.push_back(i);
		else if(us[i]) v2.push_back(i);
		else v0.push_back(i);
	}
	if((v0.size()!=0 and v3.size()!=0)){
		print(cnt);
		return 0;
	}
	if(v1.size()==n or v2.size()==n){
		print(cnt);
		return 0;
	}
	ll ans=2e18;
	rep(i,1,n){
		if(uf[i]){
			auto it=nu.begin();
			if(b[i]==*it) it++;
			ans=min(ans,cnt+(*it-a[i]));
		}else{
			auto it=nu.begin();
			if(a[i]==*it) it++;
			ans=min(ans,cnt+(*it-b[i]));
		}
	}
	print(ans);
	return 0;
}

Compiler output:
a.cc:23:51: warning: bad option '-funsafe-loop-optimizations' to pragma 'optimize' [-Wpragmas]
   23 | #pragma GCC optimize("-funsafe-loop-optimizations")
      |                                                   ^
a.cc:25:39: warning: bad option '-fwhole-program' to pragma 'optimize' [-Wpragmas]
   25 | #pragma GCC optimize("-fwhole-program")
      |                                       ^
a.cc:34:41: warning: bad option '-fcse-skip-blocks' to pragma 'optimize' [-Wpragmas]
   34 | #pragma GCC optimize("-fcse-skip-blocks")
      |                                         ^
a.cc:51:41: warning: bad option '-fstrict-overflow' to pragma 'optimize' [-Wpragmas]
   51 | #pragma GCC optimize("-fstrict-overflow")
      |                                         ^
a.cc:56:30: warning: '-std=c++11' is not an option that controls warnings [-Wpragmas]
   56 | #pragma GCC diagnostic error "-std=c++11"
      |                              ^~~~~~~~~~~~
a.cc:58:31: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
   58 | inline void rdl(long long &val){
      |                               ^
a.cc:58:31: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
a.cc:58:31: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
a.cc:58:31: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
a.cc:73:25: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
   73 | inline void rdi(int &val){
      |                         ^
a.cc:73:25: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
a.cc:73:25: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
a.cc:73:25: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
a.cc:88:22: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
   88 | inline long long rdl(){
      |                      ^
a.cc:88:22: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
a.cc:88:22: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
a.cc:88:22: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
a.cc:103:16: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
  103 | inline int rdi(){
      |                ^
a.cc:103:16: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
a.cc:103:16: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
a.cc:103:16: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
a.cc:118:24: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
  118 | inline void write(int x){
      |                        ^
a.cc:118:24: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
a.cc:118:24: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
a.cc:118:24: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
a.cc:123:25: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
  123 | inline void _write(int x){
      |                         ^
a.cc:123:25: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
a.cc:123:25: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
a.cc:123:25: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
a.cc:127:24: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
  127 | inline void print(int x){
      |                        ^
a.cc:127:24: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
a.cc:127:24: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
a.cc:127:24: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
a.cc:131:23: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
  131 | inline void write(ll x){
      |                       ^
a.cc:131:23: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
a.cc:131:23: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
a.cc:131:23: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
a.cc:136:24: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
  136 | inline void _write(ll x){
      |                        ^
a.cc:136:24: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
a.cc:136:24: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
a.cc:136:24: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
a.cc:140:23: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
  140 | inline void print(ll x){
      |                       ^
a.cc:140:23: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
a.cc:140:23: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
a.cc:140:23: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
a.cc:149:10: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
  149 | int main(){
      |          ^
a.cc:149:10: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
a.cc:149:10: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
a.cc:149:10: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
In file included from /usr/include/c++/14/string:43,
                 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:3:
/usr/include/c++/14/bits/allocator.h: In destructor 'std::_Rb_tree<int, int, std::_Identity<int>, std::less<int>, std::allocator<int> >::_Rb_tree_impl<std::less<int>, true>::~_Rb_tree_impl()':
/usr/include/c++/14/bits/allocator.h:182:7: error: inlining failed in call to 'always_inline' 'std::allocator< <template-parameter-1-1> >::~allocator() noexcept [with _Tp = std::_Rb_tree_node<int>]': target specific option mismatch
  182 |       ~allocator() _GLIBCXX_NOTHROW { }
      |       ^
In file included from /usr/include/c++/14/set:62,
                 from a.cc:8:
/usr/include/c++/14/bits/stl_tree.h:658:16: note: called from here
  658 |         struct _Rb_tree_impl
      |                ^~~~~~~~~~~~~