Problem:
Score : 700 points

Problem Statement

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.

Sample Input 1

3
1 5
4 2
6 3

Sample Output 1

7

Consider the cycle 1→3→2→1. The weights of those edges are {\rm min}(A_1,B_3)=1, {\rm min}(A_3,B_2)=2 and {\rm min}(A_2,B_1)=4, for a total of 7.
As there is no cycle with a total weight of less than 7, the answer is 7.

Sample Input 2

4
1 5
2 6
3 7
4 8

Sample Output 2

10

Sample Input 3

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

Sample Output 3

227

Solution in Rust (1.15.1) (judge verdict: Wrong Answer)

// Try AtCoder
// author: Leonardone @ NEETSDKASU

fn main() {
    let mut stdin = String::new();
    std::io::Read::read_to_string(
        &mut std::io::stdin(),
        &mut stdin).unwrap();
    let mut stdin = stdin.split_whitespace();
    let mut get = || stdin.next().unwrap();
    macro_rules! get {
        ($t:ty) => (get().parse::<$t>().unwrap());
        () => (get!(i64));
    }
    
    let n = get!(usize);
    let mut xs = vec![];
    let mut ys = vec![];
    let mut ws = vec![];
    let mut zs = vec![];

    for i in 0..n {
        let a = get!();
        let b = get!();
        xs.push((a, -b, i));
        ys.push((b, -a, i));
        ws.push((-a, a, i));
        zs.push((-b, b, i));
    }
    xs.sort();
    ys.sort();
    ws.sort();
    zs.sort();
    
    let mut xi = 0;
    let mut yi = 0;
    let mut wi = 0;
    let mut zi = 0;
    
    let mut flag_a = vec![false; n];
    let mut flag_b = vec![false; n];
    let mut cost = 0;
    use std::cmp::min;
    let mut rem = n;
    while rem > 0 {
        if xs[xi].0 * zs[zi].0  > ys[yi].0 * ws[wi].0 && xs[xi].2 != zs[zi].2 {
            cost += min(xs[xi].0, -zs[zi].0);
            flag_a[xs[xi].2] = true;
            flag_b[zs[zi].2] = true;
        } else if ys[yi].2 != ws[wi].2 {
            cost += min(ys[yi].0, -ws[wi].0);
            flag_a[ws[wi].2] = true;
            flag_b[ys[yi].2] = true;
        } else {
            let e1 = if xi+1<xs.len() && !flag_a[xs[xi+1].2] && xs[xi+1].2 != zs[zi].2 {  Some((min(xs[xi+1].0, -zs[zi].0), 1)) } else { None };
            let e2 = if zi+1<zs.len() && !flag_b[zs[zi+1].2] && xs[xi].2 != zs[zi+1].2 {  Some((min(xs[xi].0, -zs[zi+1].0), 2)) } else { None };
            let e3 = if yi+1<ys.len() && !flag_b[ys[yi+1].2] && ys[yi+1].2 != ws[wi].2 {  Some((min(ys[yi+1].0, -ws[wi].0), 3)) } else { None };
            let e4 = if wi+1<ws.len() && !flag_a[ws[wi+1].2] && ys[yi].2 != ws[wi+1].2 {  Some((min(ys[yi].0, -ws[wi+1].0), 4)) } else { None };
            if let Some(Some((_, p))) = vec![e1, e2, e3, e4].into_iter().filter(|s|s.is_some()).min() {
                rem += 1;
                match p {
                1 => { let t = xs[xi]; xs[xi] = xs[xi+1]; xs[xi+1] = t; },
                2 => { let t = zs[zi]; zs[zi] = zs[zi+1]; zs[zi+1] = t; },
                3 => { let t = ys[yi]; ys[yi] = ys[yi+1]; ys[yi+1] = t; },
                4 => { let t = ws[wi]; ws[wi] = ws[wi+1]; ws[wi+1] = t; },
                _ => {},
                }
            } else {
                println!("foo");
            }
        }
        while xi < xs.len() && flag_a[xs[xi].2] { xi += 1; }
        while yi < ys.len() && flag_b[ys[yi].2] { yi += 1; }
        while wi < ws.len() && flag_a[ws[wi].2] { wi += 1; }
        while zi < zs.len() && flag_b[zs[zi].2] { zi += 1; }
        rem -= 1;
    }
    println!("{}", cost);
}