#include<iostream>
#include<sstream>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<complex>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cassert>

#define rep(i,n) for(int i=0;i<(int)n;i++)
#define all(c) (c).begin(),(c).end()
#define mp make_pair
#define pb push_back
#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define dbg(x) cerr<<__LINE__<<": "<<#x<<" = "<<(x)<<endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

template<class T>struct costflowGraph{
	typedef pair<T, int> P;
	struct edge{
		int to, cap, rev;
		T cost;
		edge(int to, int cap, T cost, int rev) :
			to(to), cap(cap), cost(cost), rev(rev){
			}
	};
	
	int n, *prevv, *preve;
	T *dist;
	vector<vector<edge> > G;
	
	costflowGraph(int sz) : n(sz){
		G.resize(n);
		dist = new T[n];
		prevv = new int[n]; preve = new int[n];
	}
	~costflowGraph(){
		delete dist;
		delete prevv; delete preve;
	}
	
	void add(int s, int t, int cap, T cost){
		G[s].pb(edge(t, cap, cost, G[t].size()));
		G[t].pb(edge(s, 0, -cost, G[s].size() - 1));
	}
	T min_cost_flow(int s, int t, int f){
		T res = 0;
		while(f > 0){
			fill(dist, dist + n, inf);
			dist[s] = 0;
			while(1){
				bool update = 0;
				rep(v, n){
					if(dist[v] == inf) continue;
					rep(i, G[v].size()){
						edge &e = G[v][i];
						if(e.cap > 0 && dist[e.to] > dist[v] + e.cost + EPS){
							dist[e.to] = dist[v] + e.cost;
							prevv[e.to] = v;
							preve[e.to] = i;
							update = 1;
						}
					}
				}
				if(!update) break;
			}
			if(dist[t] == inf) return -1;
		
			int d = f;
			for(int v = t; v != s; v = prevv[v]) d = min(d, G[prevv[v]][preve[v]].cap);
			f -= d;
			res += d * dist[t];
			for(int v = t; v != s; v = prevv[v]){
				edge &e = G[prevv[v]][preve[v]];
				e.cap -= d;
				G[v][e.rev].cap += d;
			}
		}
		return res;
	}
};

int n, a[1000], b[1000];

int main(){
	vi v;
	cin >> n;
	rep(i, n){
		cin >> a[i] >> b[i];
		v.pb(a[i]);
		v.pb(b[i]);
	}
	sort(all(v));
	v.erase(unique(all(v)), v.end());
	
	int ans = 0;
	int s = n + v.size() + 1, t = s + 1;
	costflowGraph<int> g(t + 1);
	rep(i, n){
		g.add(s, i, 1, 0);
		int c = a[i] + b[i];
		g.add(i, lower_bound(all(v), a[i]) - v.begin() + n, 1, a[i]);
		g.add(i, lower_bound(all(v), b[i]) - v.begin() + n, 1, b[i]);
		g.add(i, n + v.size(), 1, c);
		ans += c;
	}
	rep(i, v.size()) g.add(n + i, t, 1, 0);
	g.add(n + v.size(), t, inf, 0);
	
	cout << ans - g.min_cost_flow(s, t, n) << endl;
	
	
	return 0;
}
### Prompt

Please formulate a CPP solution to the following problem:

Training is indispensable for achieving good results at ICPC. Rabbit wants to win at ICPC, so he decided to practice today as well.

Today's training is to gain dexterity that never mistypes by carefully stacking blocks. Since there are many building blocks, let's build a tall tower.

There are N blocks, and the i-th (1 ≤ i ≤ N) building blocks are in the shape of a rectangular parallelepiped of 1 x Ai x Bi. The side of length 1 is used in the depth direction, and the sides of lengths Ai and Bi are assigned one by one in the horizontal direction and one in the height direction. When building blocks, the upper building blocks must be exactly shorter in width than the lower building blocks. The blocks can be used in any order, and some blocks may not be used. Under these restrictions, I want to make the tallest tower that can be built.



Input


N
A1 B1
...
AN BN


Satisfy 1 ≤ N ≤ 1,000, 1 ≤ Ai, Bi ≤ 1,000,000. All input values ​​are integers.

Output

Output the maximum height of the tower on one line.

Examples

Input

3
10 40
10 40
20 30


Output

80


Input

4
1 2
2 3
3 4
4 1


Output

11

### Response

```cpp
#include<iostream>
#include<sstream>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<complex>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cassert>

#define rep(i,n) for(int i=0;i<(int)n;i++)
#define all(c) (c).begin(),(c).end()
#define mp make_pair
#define pb push_back
#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define dbg(x) cerr<<__LINE__<<": "<<#x<<" = "<<(x)<<endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

template<class T>struct costflowGraph{
	typedef pair<T, int> P;
	struct edge{
		int to, cap, rev;
		T cost;
		edge(int to, int cap, T cost, int rev) :
			to(to), cap(cap), cost(cost), rev(rev){
			}
	};
	
	int n, *prevv, *preve;
	T *dist;
	vector<vector<edge> > G;
	
	costflowGraph(int sz) : n(sz){
		G.resize(n);
		dist = new T[n];
		prevv = new int[n]; preve = new int[n];
	}
	~costflowGraph(){
		delete dist;
		delete prevv; delete preve;
	}
	
	void add(int s, int t, int cap, T cost){
		G[s].pb(edge(t, cap, cost, G[t].size()));
		G[t].pb(edge(s, 0, -cost, G[s].size() - 1));
	}
	T min_cost_flow(int s, int t, int f){
		T res = 0;
		while(f > 0){
			fill(dist, dist + n, inf);
			dist[s] = 0;
			while(1){
				bool update = 0;
				rep(v, n){
					if(dist[v] == inf) continue;
					rep(i, G[v].size()){
						edge &e = G[v][i];
						if(e.cap > 0 && dist[e.to] > dist[v] + e.cost + EPS){
							dist[e.to] = dist[v] + e.cost;
							prevv[e.to] = v;
							preve[e.to] = i;
							update = 1;
						}
					}
				}
				if(!update) break;
			}
			if(dist[t] == inf) return -1;
		
			int d = f;
			for(int v = t; v != s; v = prevv[v]) d = min(d, G[prevv[v]][preve[v]].cap);
			f -= d;
			res += d * dist[t];
			for(int v = t; v != s; v = prevv[v]){
				edge &e = G[prevv[v]][preve[v]];
				e.cap -= d;
				G[v][e.rev].cap += d;
			}
		}
		return res;
	}
};

int n, a[1000], b[1000];

int main(){
	vi v;
	cin >> n;
	rep(i, n){
		cin >> a[i] >> b[i];
		v.pb(a[i]);
		v.pb(b[i]);
	}
	sort(all(v));
	v.erase(unique(all(v)), v.end());
	
	int ans = 0;
	int s = n + v.size() + 1, t = s + 1;
	costflowGraph<int> g(t + 1);
	rep(i, n){
		g.add(s, i, 1, 0);
		int c = a[i] + b[i];
		g.add(i, lower_bound(all(v), a[i]) - v.begin() + n, 1, a[i]);
		g.add(i, lower_bound(all(v), b[i]) - v.begin() + n, 1, b[i]);
		g.add(i, n + v.size(), 1, c);
		ans += c;
	}
	rep(i, v.size()) g.add(n + i, t, 1, 0);
	g.add(n + v.size(), t, inf, 0);
	
	cout << ans - g.min_cost_flow(s, t, n) << endl;
	
	
	return 0;
}
```