#AGC034D. [AGC034D] Manhattan Max Matching

[AGC034D] Manhattan Max Matching

Score : 12001200 points

Problem Statement

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

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

Snuke will now form SS 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)(rx, ry) and a blue ball at coordinates (bx,by)(bx, by) as rxbx+ryby|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

  • 1N10001 \leq N \leq 1000
  • 0RXi,RYi,BXi,BYi1090 \leq RX_i,RY_i,BX_i,BY_i \leq 10^9
  • 1RCi,BCi101 \leq RC_i,BC_i \leq 10
  • i=1NRCi=i=1NBCi\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:

NN

RX1RX_1 RY1RY_1 RC1RC_1

RX2RX_2 RY2RY_2 RC2RC_2

\vdots

RXNRX_N RYNRY_N RCNRC_N

BX1BX_1 BY1BY_1 BC1BC_1

BX2BX_2 BY2BY_2 BC2BC_2

\vdots

BXNBX_N BYNBY_N BCNBC_N

Output

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

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

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

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

Snuke may have performed multiple operations at the same coordinates.

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
45152033546