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 C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;

#define TRACE(x) x
#define WATCH(x) TRACE(cout << #x" = " << x << endl)
#define WATCHR(a, b) TRACE(for (auto it=a; it!=b;) cout << *(it++) << " "; cout << endl)
#define WATCHC(V) TRACE({cout << #V" = "; WATCHR(V.begin(), V.end());})

#define all(x) (x).begin(), (x).end()

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vs = vector<string>;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

template<typename F, typename C> struct min_cost_flow {
    static constexpr int bits = 63 - __builtin_clzll(numeric_limits<F>::max());

    int V, E;
    vvi adj;
    vi dest;
    vector<F> cap;
    vector<C> cost;

    min_cost_flow(int V = 0) : V(V), E(0) {
        adj.resize(V);
    }

    void __arc(int u, int v, F f, C c) {
        E++;
        adj[u].push_back(dest.size());
        dest.push_back(v);
        cap.push_back(f);
        cost.push_back(c);
    }

    // Inserts a directed edge u --> v with capacity f and cost c.
    void arc(int u, int v, F f, C c) {
        assert(c >= 0);
        __arc(u, v, f, c);
        __arc(v, u, F(0), -c);
    }

    bool dijkstra(auto& imb, auto& flow, auto& pot, F delta) const {
        priority_queue<tuple<C, int, int>> q;
        vi ent(V, -2);
        vector<C> dist(V, numeric_limits<C>::max());
        for (int v = 0; v < V; v++) if (imb[v] >= delta) {
            dist[v] = 0;
            q.push(make_tuple(0., v, -1));
        }

        while (!q.empty()) {
            C d; int v, f; tie(d, v, f) = q.top(); q.pop();
            if (ent[v] != -2) continue;
            dist[v] = -d; ent[v] = f;
            for (int e : adj[v]) if (cap[e] - flow[e] >= delta) {
                double cd = dist[v] + (cost[e] + pot[v] - pot[dest[e]]);
                if (cd < dist[dest[e]]) {
                    dist[dest[e]] = cd;
                    q.push(make_tuple(-cd, dest[e], e ));
                }
            }
        }

        for (int v = 0; v < V; v++) if (ent[v] != -2 && imb[v] <= -delta) {
            for (int u = 0; u < V; u++) pot[u] += dist[u];
            for (int e = ent[v]; ~e; e = ent[dest[e^1]]) {
                flow[e] += delta;
                flow[e^1] -= delta;
                imb[dest[e]] += delta;
                imb[dest[e^1]] -= delta;
            }
            return true;
        }
        return false;
    }

    // Computes the minimum cost to satisfy the specified imbalances.
    // Runs in O(E^2 * log V * log inf).
    pair<bool, C> solve(vector<F> imb) const {
        vector<F> flow(E);
        vector<C> pot(V);
        for (F delta = 1ll << bits; delta; delta >>= 1) {
            for (int e = 0; e < E; e++) {
                int u = dest[e^1], v = dest[e];
                F res = cap[e] - flow[e];
                if (res >= delta && cost[e] + pot[u] - pot[v] < 0) {
                    flow[e^1] -= res;
                    flow[e] += res;
                    imb[u] -= res;
                    imb[v] += res;
                }
            }
            while (dijkstra(imb, flow, pot, delta));
        }

        C ans = 0;
        for (int e = 0; e < E; e++) if (flow[e] > 0)  {
            ans += flow[e] * cost[e];
        }
        return { imb == vector<F>(V, 0), ans };
    }
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    int N;
    cin >> N;

    const int V = 1 + N + 4 + N + 1;
    const int SRC = 0, L1 = 1, M = L1 + N, L2 = M + 4, SNK = L2 + N;

    min_cost_flow<int, ll> g(V);

    int total = 0;

    const int MAXD = 2e9;

    vi rx(N), ry(N), rw(N);
    for (int i = 0; i < N; i++) {
        cin >> rx[i] >> ry[i] >> rw[i];
        total += rw[i];
        g.arc(SRC, L1 + i, rw[i], 0);

        for (int j = 0; j < 4; j++) {
            ll w = MAXD;
            w += j&1 ? rx[i] : -rx[i];
            w += j&2 ? ry[i] : -ry[i];
            g.arc(L1 + i, M + j, rw[i], w);
        }
    }

    vi bx(N), by(N), bw(N);
    for (int i = 0; i < N; i++) {
        cin >> bx[i] >> by[i] >> bw[i];
        g.arc(L2 + i, SNK, bw[i], 0);

        for (int j = 0; j < 4; j++) {
            ll w = MAXD;
            w += j&1 ? -bx[i] : bx[i];
            w += j&2 ? -by[i] : by[i];
            g.arc(M + j, L2 + i, bw[i], w);
        }
    }

    vi imb(V);
    imb[SRC] = total;
    imb[SNK] = -total;

    bool did; ll ans;
    tie(did, ans) = g.solve(imb);

    cout << ll(2 * total) * MAXD - ans << endl;

    return 0;
}