Problem:
Score : 800 points

Problem Statement

There are N cards placed on a grid with H rows and W columns of squares.

The i-th card has an integer A_i written on it, and it is placed on the square at the R_i-th row from the top and the C_i-th column from the left.

Multiple cards may be placed on the same square.

You will first pick up at most one card from each row.

Then, you will pick up at most one card from each column.

Find the maximum possible sum of the integers written on the picked cards.

Constraints

All values are integers.

1 \leq N \leq 10^5

1 \leq H, W \leq 10^5

1 \leq A_i \leq 10^5

1 \leq R_i \leq H

1 \leq C_i \leq W

Input

Input is given from Standard Input in the following format:

N H W
R_1 C_1 A_1
R_2 C_2 A_2
\vdots
R_N C_N A_N

Output

Print the maximum possible sum of the integers written on the picked cards.

Sample Input 1

6 2 2
2 2 2
1 1 8
1 1 5
1 2 9
1 2 7
2 1 4

Sample Output 1

28

The sum of the integers written on the picked cards will be 28, the maximum value possible, if you pick up cards as follows:

Pick up the fourth card from the first row.

Pick up the sixth card from the second row.

Pick up the second card from the first column.

Pick up the fifth card from the second column.

Sample Input 2

13 5 6
1 3 35902
4 6 19698
4 6 73389
3 6 3031
3 1 4771
1 4 4784
2 1 36357
2 1 24830
5 6 50219
4 6 22645
1 2 30739
1 4 68417
1 5 78537

Sample Output 2

430590

Sample Input 3

1 100000 100000
1 1 1

Sample Output 3

1

Solution in Rust (1.15.1) (judge verdict: Time Limit Exceeded)

use std::io::*;
use std::str::FromStr;

fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

#[derive(Copy, Clone)]
struct Edge {
    to: usize,
    init: i64,
    cap: i64,
    cst: i64,
    rev: usize,
}

struct MinCostCirculation {
    // Input
    num_vs: usize,
    adj: Vec<Vec<Edge>>,
    // Intermediate variables
    h: Vec<f64>,
    ex: Vec<i64>,
    iter: Vec<usize>,
    visit: Vec<bool>,
}

impl MinCostCirculation {
    fn new(num_vs: usize) -> MinCostCirculation {
        MinCostCirculation {
            num_vs: num_vs,
            adj: vec![vec![]; num_vs],
            h: vec![0.0; num_vs],
            ex: vec![0; num_vs],
            iter: vec![],
            visit: vec![],
        }
    }

    fn add_edge(&mut self, i: usize, j: usize, cap: i64, cst: i64) {
        let rev = self.adj[j].len();
        self.adj[i].push(Edge{to: j, init: cap, cap: cap, cst: cst, rev: rev});
        let rev = self.adj[i].len() - 1;
        self.adj[j].push(Edge{to: i, init: 0, cap: 0, cst: -cst, rev: rev});
    }

    fn dfs1(&mut self, v: usize) {
        self.visit[v] = true;
        for i in 0..self.adj[v].len() {
            let b;
            let to;
            {
                let e = &self.adj[v][i];
                to = e.to;
                b = e.cap > 0 && !self.visit[e.to] && (e.cst as f64) + self.h[v] - self.h[e.to] < 0.0;
            }
            if b {
                self.dfs1(to);
            }
        }
    }

    fn dfs2(&mut self, v: usize, f: i64) -> i64 {
        if self.ex[v] < 0 {
            let d = std::cmp::min(f, -self.ex[v]);
            self.ex[v] += d;
            return d;
        }
        while self.iter[v] < self.adj[v].len() {
            let e = self.adj[v][self.iter[v]];
            if e.cap > 0 && (e.cst as f64) + self.h[v] - self.h[e.to] < 0.0 {
                let d = self.dfs2(e.to, std::cmp::min(f, e.cap));
                if d > 0 {
                    self.adj[v][self.iter[v]].cap -= d;
                    self.adj[e.to][e.rev].cap += d;
                    return d;
                }
            }
            self.iter[v] += 1;
        }
        return 0;
    }

    fn solve(&mut self) -> i64 {
        let mut eps = 0.0;
        for v in 0..self.num_vs {
            for e in self.adj[v].iter() {
                if e.cap > 0 && -e.cst as f64 > eps {
                    eps = -e.cst as f64;
                }
            }
        }
        while eps * ((self.num_vs + 1) as f64) > 1.0 {
            eps /= 4.0;
            for v in 0..self.num_vs {
                for i in 0..self.adj[v].len() {
                    let e = self.adj[v][i];
                    if e.cap > 0 && (e.cst as f64) + self.h[v] - self.h[e.to] < 0.0 {
                        self.ex[e.to] += e.cap;
                        self.ex[v] -= e.cap;
                        self.adj[e.to][e.rev].cap += e.cap;
                        self.adj[v][i].cap = 0;
                        // e.cap = 0;
                    }
                }
            }
            loop {
                self.visit = vec![false; self.num_vs];
                for v in 0..self.num_vs {
                    if self.ex[v] > 0 {
                        self.dfs1(v);
                    }
                }
                for v in 0..self.num_vs {
                    if self.visit[v] {
                        self.h[v] -= eps;
                    }
                }
                self.iter = vec![0; self.num_vs];
                let mut ok = true;
                for v in 0..self.num_vs {
                    loop {
                        if self.ex[v] <= 0 {
                            break;
                        }
                        let ex = self.ex[v];
                        let f = self.dfs2(v, ex);
                        if f <= 0 {
                            break;
                        }
                        self.ex[v] -= f;
                    }
                    if self.ex[v] > 0 {
                        ok = false;
                    }
                }
                if ok {
                    break;
                }
            }
        }
        let mut res = 0;
        for v in 0..self.num_vs {
            for e in self.adj[v].iter() {
                if e.init > e.cap {
                    res += e.cst * (e.init - e.cap);
                }
            }
        }
        return res;
    }
}

fn main() {
    let n = read();
    let h = read();
    let w = read();

    let mut mcc = MinCostCirculation::new(h + w + n + 1);
    let s = h + w + n;
    for y in 0..h {
        mcc.add_edge(s, y, 1, 0);
    }
    for x in 0..w {
        mcc.add_edge(s, h + x, 1, 0);
    }

    for i in 0..n {
        let y = read::<usize>() - 1;
        let x = read::<usize>() - 1;
        let a = read::<i64>();

        let v = h + w + i;
        mcc.add_edge(y, v, 1, 0);
        mcc.add_edge(h + x, v, 1, 0);
        mcc.add_edge(v, s, 1, -a);
    }

    println!("{}", -mcc.solve());
}