#include<stdio.h>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll inf=9223372036854775807ll;
int a[100010],b[100010],L[200010],M;
ll s[100010];
struct pr{
	int x,y;
	pr(int x=0,int y=0):x(x),y(y){}
}p[200010];
bool operator<(pr a,pr b){return a.x==b.x?a.y<b.y:a.x<b.x;}
int main(){
	int n,i,x,y;
	ll sa,sb,res;
	scanf("%d",&n);
	sa=sb=0;
	for(i=1;i<=n;i++){
		scanf("%d%d",a+i,b+i);
		p[++M]=pr(a[i],i);
		p[++M]=pr(b[i],i+n);
		sa+=a[i];
		sb+=b[i];
	}
	res=min(sa,sb);
	sort(p+1,p+M+1);
	for(i=1;i<=n;i++)s[i]=s[i-1]+p[i].x;
	for(i=1;i<=M;i++)L[i]=p[i].x==p[i-1].x?L[i-1]:i;
	for(i=1;i<=n;i++){
		x=L[lower_bound(p+1,p+M+1,pr(a[i],i))-p];
		y=L[lower_bound(p+1,p+M+1,pr(b[i],i+n))-p];
		if(x<=n&&y<=n)
			res=min(res,s[n]);
		else if(x<=n)
			res=min(res,(x==n?s[n]-p[n-1].x:s[n-1])+b[i]);
		else if(y<=n)
			res=min(res,(y==n?s[n]-p[n-1].x:s[n-1])+a[i]);
		else
			res=min(res,s[n-2]+a[i]+b[i]);
	}
	printf("%lld",res);
}
### Prompt

Please formulate a Cpp solution to the following problem:

We have a directed weighted graph with N vertices. Each vertex has two integers written on it, and the integers written on Vertex i are A_i and B_i.

In this graph, there is an edge from Vertex x to Vertex y for all pairs 1 \leq x,y \leq N, and its weight is {\rm min}(A_x,B_y).

We will consider a directed cycle in this graph that visits every vertex exactly once. Find the minimum total weight of the edges in such a cycle.

Constraints

* 2 \leq N \leq 10^5
* 1 \leq A_i \leq 10^9
* 1 \leq B_i \leq 10^9
* All values in input are integers.

Input

Input is given from Standard Input in the following format:


N
A_1 B_1
A_2 B_2
:
A_N B_N


Output

Print the minimum total weight of the edges in such a cycle.

Examples

Input

3
1 5
4 2
6 3


Output

7


Input

4
1 5
2 6
3 7
4 8


Output

10


Input

6
19 92
64 64
78 48
57 33
73 6
95 73


Output

227

### Response

```cpp
#include<stdio.h>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll inf=9223372036854775807ll;
int a[100010],b[100010],L[200010],M;
ll s[100010];
struct pr{
	int x,y;
	pr(int x=0,int y=0):x(x),y(y){}
}p[200010];
bool operator<(pr a,pr b){return a.x==b.x?a.y<b.y:a.x<b.x;}
int main(){
	int n,i,x,y;
	ll sa,sb,res;
	scanf("%d",&n);
	sa=sb=0;
	for(i=1;i<=n;i++){
		scanf("%d%d",a+i,b+i);
		p[++M]=pr(a[i],i);
		p[++M]=pr(b[i],i+n);
		sa+=a[i];
		sb+=b[i];
	}
	res=min(sa,sb);
	sort(p+1,p+M+1);
	for(i=1;i<=n;i++)s[i]=s[i-1]+p[i].x;
	for(i=1;i<=M;i++)L[i]=p[i].x==p[i-1].x?L[i-1]:i;
	for(i=1;i<=n;i++){
		x=L[lower_bound(p+1,p+M+1,pr(a[i],i))-p];
		y=L[lower_bound(p+1,p+M+1,pr(b[i],i+n))-p];
		if(x<=n&&y<=n)
			res=min(res,s[n]);
		else if(x<=n)
			res=min(res,(x==n?s[n]-p[n-1].x:s[n-1])+b[i]);
		else if(y<=n)
			res=min(res,(y==n?s[n]-p[n-1].x:s[n-1])+a[i]);
		else
			res=min(res,s[n-2]+a[i]+b[i]);
	}
	printf("%lld",res);
}
```