Language: C++

//by yjz
#include<bits/stdc++.h>
using namespace std;
#define FF first
#define SS second
#define PB push_back
#define MP make_pair
#define foreach(it,s) for(__typeof((s).begin()) it=(s).begin();it!=(s).end();it++)
#ifndef LOCAL
#define cerr if(0)cout
#endif
typedef long long ll;
const int mod=1e9+7;
//My i/o stream
struct fastio
{
	char s[100000];
	int it,len;
	fastio(){it=len=0;}
	inline char get()
	{
		if(it<len)return s[it++];it=0;
		len=fread(s,1,100000,stdin);
		if(len==0)return EOF;else return s[it++];
	}
	bool notend()
	{
		char c=get();
		while(c==' '||c=='\n')c=get();
		if(it>0)it--;
		return c!=EOF;
	}
}_buff;
#define geti(x) x=getnum()
#define getii(x,y) geti(x),geti(y)
#define getiii(x,y,z) getii(x,y),geti(z)
#define puti(x) putnum(x),putchar(' ')
#define putii(x,y) puti(x),puti(y)
#define putiii(x,y,z) putii(x,y),puti(z)
#define putsi(x) putnum(x),putchar('\n')
#define putsii(x,y) puti(x),putsi(y)
#define putsiii(x,y,z) putii(x,y),putsi(z)
inline ll getnum()
{
	ll r=0;bool ng=0;char c;c=_buff.get();
	while(c!='-'&&(c<'0'||c>'9'))c=_buff.get();
	if(c=='-')ng=1,c=_buff.get();
	while(c>='0'&&c<='9')r=r*10+c-'0',c=_buff.get();
	return ng?-r:r;
}
template<class T> inline void putnum(T x)
{
	if(x<0)putchar('-'),x=-x;
	register short a[20]={},sz=0;
	while(x)a[sz++]=x%10,x/=10;
	if(sz==0)putchar('0');
	for(int i=sz-1;i>=0;i--)putchar('0'+a[i]);
}
inline char getreal(){char c=_buff.get();while(c<=32)c=_buff.get();return c;}
ll qpow(ll x,ll k){return k==0?1:1ll*qpow(1ll*x*x%mod,k>>1)*(k&1?x:1)%mod;}
const int maxn=100111;
int n;
pair<ll,int> a[maxn],b[maxn];
vector<pair<ll,int> > v;
bool f[maxn*2];
int main()
{
	geti(n);
	ll sum0=0,sum1=0;
	for(int i=1;i<=n;i++)
	{
		ll x,y;
		getii(x,y);
		sum0+=x;
		sum1+=y;
		a[i]=MP(x,i);
		b[i]=MP(y,i);
		v.PB(MP(x,i));
		v.PB(MP(y,n+i));
	}
	sort(v.begin(),v.end());
	ll ans=0;
	bool flag=0;
	for(int i=0;i<n;i++)
	{
		int id=v[i].SS;
		f[id]=1;
		ans+=v[i].FF;
	}
	pair<pair<int,int>,pair<int,int> > mx0,mx1;
	mx0=mx1=MP(MP(2e9,-1),MP(2e9,-1));
	for(int i=1;i<=n;i++)
	{
		if(f[i]&&f[i+n])flag=1;
		pair<int,int> cur;
		if(f[i])
		{
			cur=a[i];
			if(cur>mx0.FF)mx0.SS=mx0.FF,mx0.FF=cur;
			else (cur>mx0.SS)mx0.SS=cur;
		}
		else
		{
			cur=b[i];
			if(cur>mx1.FF)mx1.SS=mx1.FF,mx1.FF=cur;
			else if(cur>mx1.SS)mx1.SS=cur;
		}
	}
	if(flag||mx0.FF.SS==-1||mx1.FF.SS==-1)cout<<ans<<endl;
	else
	{
//		cerr<<mx0.FF.FF<<","<<mx0.FF.SS<<" "<<mx0.SS.FF<<","<<mx0.SS.SS<<endl;
//		cerr<<mx1.FF.FF<<","<<mx1.FF.SS<<" "<<mx1.SS.FF<<","<<mx1.SS.SS<<endl;
		ll mn=2e9;
		for(int i=1;i<=n;i++)
		{
			if(f[i]&&mx0.SS.SS!=-1)
			{
				mn=min(mn,b[i].FF-(i==mx0.FF.SS?mx0.SS.FF:mx0.FF.FF));
			}
			if(f[i+n]&&mx1.SS.SS!=-1)
			{
				mn=min(mn,a[i].FF-(i==mx1.FF.SS?mx1.SS.FF:mx1.FF.FF));
			}
		}
		cout<<min(ans+mn,min(sum0,sum1))<<endl;
	}
	return 0;
}


Compiler output:
a.cc: In function 'void putnum(T)':
a.cc:54:24: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   54 |         register short a[20]={},sz=0;
      |                        ^
a.cc:54:33: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   54 |         register short a[20]={},sz=0;
      |                                 ^~
a.cc: In function 'int main()':
a.cc:100:42: error: expected ';' before 'mx0'
  100 |                         else (cur>mx0.SS)mx0.SS=cur;
      |                                          ^~~
      |                                          ;