Problem:
D: Dangerous Tower

ICPC で良い成績を収めるには修行が欠かせない．うさぎは ICPC で勝ちたいので，今日も修行をすることにした．

今日の修行は，積み木を丁寧に積み上げることによって，決してミスタイピングをしないほどの手先の器用さを手に入れようというものである．せっかく積み木がたくさんあるので，高い塔を作ろう．

積み木は N 個あり，i 番目 (1 ≤ i ≤ N) の積み木は 1 × Ai × Bi の直方体の形をしている．長さ 1 の辺は奥行き方向に用いて，長さ Ai, Bi の辺を横方向と高さ方向に 1 つずつ割り当てることにした．積み木を積み上げるとき，上の段の積み木は下の段の積み木より横の長さが厳密に短くなければならない．積み木は好きな順番に使うことができ，また，使わない積み木があってもよい．このような制約下で，作ることが可能な最も高い塔を作りたい．

Input

N
A1 B1
...
AN BN

1 ≤ N ≤ 1,000，1 ≤ Ai，Bi ≤ 1,000,000 を満たす．入力の値はすべて整数である．

Output

塔の高さの最大値を 1 行に出力せよ．

Sample Input 1

3
10 40
10 40
20 30

Sample Output 1

80

Sample Input 2

4
1 2
2 3
3 4
4 1

Sample Output 2

11

Solution in C++ (judge verdict: Accepted)

#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef pair<int,int> pint;

#define DE 1
#define FI first
#define SE second
#define PB push_back
#define MP make_pair
#define ALL(s) (s).begin(),(s).end()
#define REP(i,n) for (int i = 0; i < (int)(n); ++i)
#define EACH(i,s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define COUT(x) cout<<#x<<" = "<<(x)<<" (L"<<__LINE__<<")"<<endl

template<class T1, class T2> ostream& operator<<(ostream &s, pair<T1,T2> P){return s<<'<'<<P.first<<", "<<P.second<<'>';}
template<class T> ostream& operator<<(ostream &s, vector<T> P) {s<<"{ ";for(int i=0;i<P.size();++i){if(i>0)s<<", ";s<<P[i];}return s<<" }"<<endl;}
template<class T1, class T2> ostream& operator<<(ostream &s, map<T1,T2> P) {s<<"{ ";for(__typeof__(P.begin()) it=P.begin();it!=P.end();++it){if(it!=P.begin())s<<", ";s<<'<'<<it->first<<"->"<<it->second<<'>';}return s<<" }"<<endl;}



struct Edge {
    int rev, from, to, cap, icap, flow, cost;
    Edge() {}
    Edge(int r, int f, int t, int ca, int co) : rev(r), from(f), to(t), cap(ca), icap(ca), flow(0), cost(co) {}
    friend bool operator < (const Edge &e1, const Edge &e2) {return e1.cost < e2.cost;}
    friend ostream& operator << (ostream& s, const Edge& E) {s << E.from << "->" << E.to << '(' << E.cap << ',' << E.cost << ')';return s;}
};

const int MAX_V = 5000;
struct Graph {
    int V,E;
    vector<Edge> list[MAX_V];
    
    Graph(int n = 0) : V(n), E(0) {}
    void init(int n = 0) {V = n; E = 0; for (int i = 0; i < MAX_V; ++i) list[i].clear();}
	void resize(int n = 0) {V = n;}
    void reset() {for (int i = 0; i < V; ++i) for (int j = 0; j < list[i].size(); ++j) list[i][j].cap = list[i][j].icap;}
    inline vector<Edge>& operator [] (int i) {return list[i];}
    friend ostream& operator << (ostream& s, const Graph& G) {for (int i = 0; i < G.V; ++i) {s << i << " : " << G.list[i];}return s;}
    
    void addedge(int from, int to, int cap, int cost) {
        if (cap > 0) {
            ++E;
            list[from].push_back(Edge(list[to].size(), from, to, cap, cost));
            list[to].push_back(Edge(list[from].size()-1, to, from, 0, -cost));
        }
    }
    
    Edge &redge(Edge e) {
        if (e.from != e.to) return list[e.to][e.rev];
        else return list[e.to][e.rev+1];
    }
};

Graph G;

int dist[MAX_V];
int prevv[MAX_V];
int preve[MAX_V];

const ll INF = 1LL<<30;
int MinCost(Graph &G, int s, int t, int f) {
    int res = 0;
    while (f > 0) {
        fill(dist, dist+G.V, INF);
        dist[s] = 0;
        while (true) {
            bool update = false;
            for (int v = 0; v < G.V; ++v) {
                if (dist[v] == INF) continue;
                for (int i = 0; i < G[v].size(); ++i) {
                    Edge &e = G[v][i];
                    if (e.cap > 0 && dist[e.to] > dist[v] + e.cost) {
                        dist[e.to] = dist[v] + e.cost;
                        prevv[e.to] = v;
                        preve[e.to] = i;
                        update = true;
                    }
                }
            }
            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]];
            Edge &re = G.redge(e);
            e.cap -= d;
            re.cap += d;
        }
    }
    return res;
}

int n;
int a, b;
vector<pint> block;
map<int,int> ind;

int main() {
    //freopen( "/Users/macuser/Documents/Programming/Contest/input.in", "r", stdin );
    
    while (cin >> n) {
        block.clear(); ind.clear();
        int id = 0;
        for (int i = 0; i < n; ++i) {
            cin >> a >> b;
            block.PB(MP(a,b));
            if (!ind.count(a)) {ind[a] = id; ++id;}
            if (!ind.count(b)) {ind[b] = id; ++id;}
        }
        int V = n + id + 2;
        int s = V-2, t = V-1;
        G.init(V);
        
        G.addedge(s, t, 1000000, 0);
        for (int i = 0; i < n; ++i) G.addedge(s, i, 1, 0);
        for (int i = 0; i < id; ++i) G.addedge(i+n, t, 1, 0);
        for (int i = 0; i < n; ++i) {
            int from = i;
            int to1 = ind[block[i].FI], to2 = ind[block[i].SE];
            G.addedge(from, to1+n, 1, -block[i].SE);
            if (to2 != to1) G.addedge(from, to2+n, 1, -block[i].FI);
        }
        //COUT(G);
        
        int res = MinCost(G, s, t, n);
        
        //COUT(G);
        
        cout << -res << endl;
    }
    
    return 0;
}