Problem:
Score : 1200 points

Problem Statement

Snuke is playing with red and blue balls, placing them on a two-dimensional plane.

First, he performed N operations to place red balls. In the i-th of these operations, he placed RC_i red balls at coordinates (RX_i,RY_i).
Then, he performed another N operations to place blue balls. In the i-th of these operations, he placed BC_i blue balls at coordinates (BX_i,BY_i).
The total number of red balls placed and the total number of blue balls placed are equal, that is, \sum_{i=1}^{N} RC_i = \sum_{i=1}^{N} BC_i. Let this value be S.

Snuke will now form S pairs of red and blue balls so that every ball belongs to exactly one pair.
Let us define the score of a pair of a red ball at coordinates (rx, ry) and a blue ball at coordinates (bx, by) as |rx-bx| + |ry-by|.

Snuke wants to maximize the sum of the scores of the pairs.
Help him by finding the maximum possible sum of the scores of the pairs.

Constraints

1 \leq N \leq 1000

0 \leq RX_i,RY_i,BX_i,BY_i \leq 10^9

1 \leq RC_i,BC_i \leq 10

\sum_{i=1}^{N} RC_i = \sum_{i=1}^{N} BC_i

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
RX_1 RY_1 RC_1
RX_2 RY_2 RC_2
\vdots
RX_N RY_N RC_N
BX_1 BY_1 BC_1
BX_2 BY_2 BC_2
\vdots
BX_N BY_N BC_N

Output

Print the maximum possible sum of the scores of the pairs.

Sample Input 1

2
0 0 1
3 2 1
2 2 1
5 0 1

Sample Output 1

8

If we pair the red ball at coordinates (0,0) and the blue ball at coordinates (2,2), the score of this pair is |0-2| + |0-2|=4.
Then, if we pair the red ball at coordinates (3,2) and the blue ball at coordinates (5,0), the score of this pair is |3-5| + |2-0|=4.
Making these two pairs results in the total score of 8, which is the maximum result.

Sample Input 2

3
0 0 1
2 2 1
0 0 2
1 1 1
1 1 1
3 3 2

Sample Output 2

16

Snuke may have performed multiple operations at the same coordinates.

Sample Input 3

10
582463373 690528069 8
621230322 318051944 4
356524296 974059503 6
372751381 111542460 9
392867214 581476334 6
606955458 513028121 5
882201596 791660614 9
250465517 91918758 3
618624774 406956634 6
426294747 736401096 5
974896051 888765942 5
726682138 336960821 3
715144179 82444709 6
599055841 501257806 6
390484433 962747856 4
912334580 219343832 8
570458984 648862300 6
638017635 572157978 10
435958984 585073520 7
445612658 234265014 6

Sample Output 3

45152033546

Solution in Rust (1.15.1) (judge verdict: Accepted)

// https://atcoder.jp/contests/agc034/tasks/agc034_d
//
#![allow(unused_imports)]
use std::io::*;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        println!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

type Capacity = i64;
type Cost = i64;
type Graph = Vec<Vec<Edge>>;
type Edge = (usize, usize, Capacity, Cost); // (to_index, rev_index, cap, cost)

const INF: Capacity = 1e12 as Capacity;

struct MinCostFlow {
    n: usize,
    graph: Graph,
}

type State = (i64, usize);

impl MinCostFlow {
    fn new(n: usize) -> Self {
        MinCostFlow {
            n: n,
            graph: vec![vec![]; n],
        }
    }

    fn add_edge(&mut self, u: usize, v: usize, cap: Capacity, cost: Cost) {
        let udeg = self.graph[u].len();
        let vdeg = self.graph[v].len();
        self.graph[u].push((v, vdeg, cap, cost));
        self.graph[v].push((u, udeg, 0, -cost));
    }

    fn min_cost_flow(&mut self, from: usize, to: usize, flow: Capacity) -> Cost {
        let n = self.graph.len();
        let mut flow = flow;
        let mut ans = 0;

        let mut h = vec![0; n];
        let mut from_v = vec![vec![0; 2]; n];

        while flow > 0 {
            let mut dist = vec![INF; n];
            dist[from] = 0;

            let mut q = BinaryHeap::new();
            q.push((0, from));
            while q.len() >= 1 {
                let state = q.pop().unwrap();
                let now = state.1;
                let val = -state.0;
                if dist[now] < val {
                    continue;
                }
                let mut ii = 0;
                for &e in &self.graph[now] {
                    let tval = dist[now] + e.3 + h[now] - h[e.0];
                    if e.2 > 0 && dist[e.0] > tval {
                        dist[e.0] = tval;
                        from_v[e.0][0] = now;
                        from_v[e.0][1] = ii;
                        q.push((-tval, e.0));
                    }
                    ii += 1;
                }
            }
            if dist[to] == INF {
                return ans;
            }

            for i in 0..n {
                h[i] += dist[i];
            }

            {
                let mut min_cap = flow;
                let mut tt = to;
                while tt != from {
                    min_cap = min(min_cap, self.graph[from_v[tt][0]][from_v[tt][1]].2);
                    tt = from_v[tt][0];
                }
                flow -= min_cap;
                ans += min_cap * h[to];
                let mut tt = to;
                while tt != from {
                    self.graph[from_v[tt][0]][from_v[tt][1]].2 -= min_cap;
                    let e = self.graph[from_v[tt][0]][from_v[tt][1]];
                    self.graph[tt][e.1].2 += min_cap;
                    tt = from_v[tt][0];
                }
            }
        }
        ans
    }
}

fn main() {
    input! {
        n: usize,
        reds: [(i64, i64, i64); n],
        blues: [(i64, i64, i64); n],
    };

    // [0, n): red
    // [n, 2n): blue
    // [2n, 2n+4): center
    // 2n+4: source
    // 2n+5: sink
    let mut min_cost_flow = MinCostFlow::new(2*n+2+4);
    let offset = 2000000000;
    let source = 2*n+4;
    let sink = 2*n+5;

    let mut total_flow = 0;
    for i in 0..n {
        total_flow += reds[i].2;
        min_cost_flow.add_edge(source, i, reds[i].2 as Capacity, 0);
        for j in 0..2 {
            for k in 0..2 {
                let tj = j as i64 * 2 - 1;
                let tk = k as i64 * 2 - 1;
                min_cost_flow.add_edge(i, 2*n+j*2+k, reds[i].2 as Capacity,      (reds[i].0 * tj) +  (reds[i].1 * tk) + offset);
                min_cost_flow.add_edge(2*n+j*2+k, n+i, blues[i].2 as Capacity, -(blues[i].0 * tj) - (blues[i].1 * tk) + offset);
            }
        }
        min_cost_flow.add_edge(n+i, sink, blues[i].2 as Capacity, 0);
    }

    println!("{}", total_flow * offset * 2 - min_cost_flow.min_cost_flow(source, sink, total_flow));
}