Orig Description
Score : 1200 points
Problem StatementSnuke 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.
InputInput 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
OutputPrint the maximum possible sum of the scores of the pairs.
Sample Input 12
0 0 1
3 2 1
2 2 1
5 0 1
Sample Output 18
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 23
0 0 1
2 2 1
0 0 2
1 1 1
1 1 1
3 3 2
Sample Output 216
Snuke may have performed multiple operations at the same coordinates.
Sample Input 310
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 345152033546
Extracted Specification
Integer N (1 ≤ N ≤ 1000), representing some quantity or size.
N tuples, each containing three integers (X1, Y1, Z1), where 0 ≤ X1, Y1 ≤ 10^9 and 1 ≤ Z1 ≤ 10.
N tuples, each containing three integers (X2, Y2, Z2), where 0 ≤ X2, Y2 ≤ 10^9 and 1 ≤ Z2 ≤ 10.
### Example Input:
```
2
0 0 1
3 2 1
2 2 1
5 0 1
```
### Function Signature:
Write a function f(N, red_operations, blue_operations) that takes in the input.
def f(N: int, red_operations: List[Tuple[int, int, int]], blue_operations: List[Tuple[int, int, int]]):
'''
N: an integer
red_operations: a list of N tuples, each containing three integers
blue_operations: a list of N tuples, each containing three integers
'''
Test Case Generator
To create a test case generator for the given problem statement, we need to follow these steps:
1. Randomly select \(N\) (the number of operations for red and blue balls).
2. Generate \(N\) sets of coordinates and counts for red balls.
3. Generate \(N\) sets of coordinates and counts for blue balls, ensuring that the total number of red and blue balls are equal.
We need to ensure that the total number of red balls placed equals the total number of blue balls placed, as indicated by the constraint \(\sum_{i=1}^{N} RC_i = \sum_{i=1}^{N} BC_i\).
Here is the test case generator implemented in Python:
```python
import random
def tcgen():
N = random.randint(1, 1000)
RX = []
RY = []
RC = []
BX = []
BY = []
BC = []
total_red = 0
total_blue = 0
for _ in range(N):
rx = random.randint(0, 10**9)
ry = random.randint(0, 10**9)
rc = random.randint(1, 10)
RX.append(rx)
RY.append(ry)
RC.append(rc)
total_red += rc
for _ in range(N):
bx = random.randint(0, 10**9)
by = random.randint(0, 10**9)
bc = random.randint(1, 10)
BX.append(bx)
BY.append(by)
BC.append(bc)
total_blue += bc
# Adjust BC values to ensure total_red == total_blue
diff = total_red - total_blue
if diff > 0:
for i in range(N):
if diff <= 0:
break
if BC[i] + diff <= 10:
BC[i] += diff
diff = 0
else:
diff -= (10 - BC[i])
BC[i] = 10
elif diff < 0:
diff = abs(diff)
for i in range(N):
if diff <= 0:
break
if BC[i] - diff >= 1:
BC[i] -= diff
diff = 0
else:
diff -= (BC[i] - 1)
BC[i] = 1
return N, RX, RY, RC, BX, BY, BC
# Example usage
N, RX, RY, RC, BX, BY, BC = tcgen()
print(N)
for i in range(N):
print(RX[i], RY[i], RC[i])
for i in range(N):
print(BX[i], BY[i], BC[i])
```
This generator first creates two lists of operations for red and blue balls, ensuring that their total counts match by adjusting the blue ball counts if necessary. It then prints the generated values in the required format.
Extract Arguments
def extract_arguments(fh):
N = int(fh.readline().strip())
red_balls = []
blue_balls = []
for _ in range(N):
RX, RY, RC = map(int, fh.readline().strip().split())
red_balls.append((RX, RY, RC))
for _ in range(N):
BX, BY, BC = map(int, fh.readline().strip().split())
blue_balls.append((BX, BY, BC))
return N, red_balls, blue_balls