MEETING
Problem
A group of programmers lives in a city where there are horizontal and vertical streets, equally spaced apart, forming a grid. The streets are numbered in order with natural numbers, both in the horizontal and vertical dimensions. Each programmer lives at an intersection in this city, and we know the coordinates of their homes.
The programmers want to meet at another programmer's house to work. They want to choose the house that minimizes the total distance all programmers will travel. Assume that each one starts from their own home (except for the "lucky" one where the meeting will be held at their house) and moves along the streets of the city, turning only at intersections. We consider that the distance between two neighboring intersections is equal to one unit.
Find the minimum distance that the programmers will need to cover if the meeting point is chosen optimally.
Input:
The first line of the input will contain an integer , the number of programmers. Each of the next lines of the input will contain two numbers and , separated by a space. These are the coordinates of the house where the corresponding programmer lives. Assume that there will not be two programmers whose houses have the same coordinates.
Output:
The output should consist of exactly one line containing exactly one integer: the minimum total distance that the programmers will need to cover.
Constraints
- .
- The values of the coordinates will not be negative and will not exceed .
- Time limit: sec.
Memory limit: MB.
For test cases with a total value of , it will be .
Note that for large values of and the coordinates, the total distances (as well as some intermediate results that may be needed for their calculation) may exceed . Also, note that the size of the input data may be large. We recommend reading the data efficiently (see <cstdio> and scanf).
Example of Input - Output Files:
STDIN (incint.in)
7
1 3
3 2
3 5
6 9
10 1
12 4
5 7
STDOUT (incint.out)
39
Explanation of the example:
If the house of the third programmer, with coordinates , is chosen, the total distance that needs to be covered is . This is also the minimum possible.
Comments