#include <bits/stdc++.h>

typedef long long int int64;
int n;
int64 a[100003],b[100003];
struct tuple {
	int64 val;
	int i;
	int k;
} t[200003];
bool cmp1(const tuple &a,const tuple &b)
{
	return a.val < b.val;
}
int stat[100003];
int main()
{
	scanf("%d",&n);
	int64 sa = 0,sb = 0;
	for(int i = 0;i < n;++i) {
		scanf("%lld%lld",&a[i],&b[i]);
		sa += a[i];sb += b[i];
		t[2*i].val = a[i];t[2*i].i = i;t[2*i].k = 0;
		t[2*i+1].val = b[i];t[2*i+1].i = i;t[2*i+1].k = 1;
	}
	std::sort(t,t+2*n,cmp1);
	int64 sc = 0;
	bool ok = false;
	for(int i = 0;i < n;++i) {
		sc += t[i].val;
		stat[t[i].i] |= (1<<t[i].k);
		if(stat[t[i].i] == 3) ok = true;
	}
	if(!ok) {
		int64 min = -1;
		for(int i = n;i < 2*n;++i) {
			int64 k = t[i].i == t[n-1].i ? t[i].val-t[n-2].val : t[i].val-t[n-1].val;
			if(min == -1 || k < min) min = k;
		}
		sc += min;
	}
	printf("%lld\n", std::min(std::min(sa,sb),sc));
}

### Prompt

Please provide a Cpp coded solution to the problem described below:

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 <bits/stdc++.h>

typedef long long int int64;
int n;
int64 a[100003],b[100003];
struct tuple {
	int64 val;
	int i;
	int k;
} t[200003];
bool cmp1(const tuple &a,const tuple &b)
{
	return a.val < b.val;
}
int stat[100003];
int main()
{
	scanf("%d",&n);
	int64 sa = 0,sb = 0;
	for(int i = 0;i < n;++i) {
		scanf("%lld%lld",&a[i],&b[i]);
		sa += a[i];sb += b[i];
		t[2*i].val = a[i];t[2*i].i = i;t[2*i].k = 0;
		t[2*i+1].val = b[i];t[2*i+1].i = i;t[2*i+1].k = 1;
	}
	std::sort(t,t+2*n,cmp1);
	int64 sc = 0;
	bool ok = false;
	for(int i = 0;i < n;++i) {
		sc += t[i].val;
		stat[t[i].i] |= (1<<t[i].k);
		if(stat[t[i].i] == 3) ok = true;
	}
	if(!ok) {
		int64 min = -1;
		for(int i = n;i < 2*n;++i) {
			int64 k = t[i].i == t[n-1].i ? t[i].val-t[n-2].val : t[i].val-t[n-1].val;
			if(min == -1 || k < min) min = k;
		}
		sc += min;
	}
	printf("%lld\n", std::min(std::min(sa,sb),sc));
}

```