#P16777. [GKS 2020 #H] Rugby

[GKS 2020 #H] Rugby

Problem Description

On a far away planet, rugby is played in the 22-dimensional Cartesian coordinate system without bounds. The players can occupy integer grid points only and they can move to the neighboring grid points in any of the 44 cardinal directions. Specifically, if a player is currently at the point (X,Y)(X, Y), then they can move to either of the points (X+1,Y)(X+1, Y), (X1,Y)(X-1, Y), (X,Y+1)(X, Y+1), or (X,Y1)(X, Y-1) in a single step.

After the game, NN players are scattered throughout the coordinate system such that any grid point is empty or occupied by 11 or more players. They want to gather for a picture and form a perfect horizontal line of NN grid points, 11 player per point, all occupied points next to each other. Formally, the players have to move so as to occupy the grid points (X,Y)(X, Y), (X+1,Y)(X+1, Y), (X+2,Y)(X+2, Y), ..., (X+N1,Y)(X+N-1, Y) for some coordinates XX and YY. What is the minimum total number of steps the players should make to form a perfect line if they are free to choose the position of the line in the coordinate system and the ordering of players is not important?

Input Format

The first line of the input gives the number of test cases TT. TT test cases follow. The first line of each test case gives the number of players NN. The subsequent NN lines give the initial coordinates of the players. The ii-th of these lines contains 22 integers XiX_i and YiY_i, which describe the initial position of the ii-th player (1iN1 \le i \le N).

Output Format

For each test case, output one line containing Case #x\#x: yy, where xx is the test case number (starting from 11) and yy is the minimum total number of steps that the players need to make in order to form a perfect horizontal line.

2
2
1 1
4 4
3
1 1
1 2
1 3
Case #1: 5
Case #2: 4

Hint

In the 11st test case, 11 of many optimal solutions is obtained by the 22nd player moving 22 steps to the left and 33 steps down to the point (2,1)(2, 1).

In the 22nd test case, a perfect line can be formed with a total of 44 steps if the 11st player moves to the point (0,2)(0, 2) and the 33rd player moves to the point (2,2)(2, 2).

Limits

Test Set 11

1N101 \le N \le 10.

500Xi500-500 \le X_i \le 500.

500Yi500-500 \le Y_i \le 500.

Test Set 22

1N1051 \le N \le 10^5 for at most 1010 cases.

1N1041 \le N \le 10^4 for the remaining cases.

109Xi109-10^9 \le X_i \le 10^9.

109Yi109-10^9 \le Y_i \le 10^9.