problems / 135

Where to Put the Mail Room

easy geometry sorting math 1000 ms · 256 MB

Where to Put the Mail Room

A company campus is laid out on a grid of streets, and its n offices sit at integer coordinates. Each morning a courier walks from the mail room to every office, always along the streets. The walk from point (a, b) to an office at (x, y) is |a − x| + |b − y| meters.

Facilities wants to build the mail room at the integer point (a, b) that minimizes the total walking distance

D(a, b) = Σ (|a − x_i| + |b − y_i|), summed over all offices i.

The mail room may go anywhere, including on the same point as an office. If several points give the minimum, facilities picks the one with the smallest a, and among those, the one with the smallest b.

Find that point and the minimum total distance.

Input

The first line contains an integer n (1 ≤ n ≤ 2·10^5), the number of offices.

Each of the next n lines contains two integers x_i and y_i (−10^9 ≤ x_i, y_i ≤ 10^9), the coordinates of one office. Several offices may share the same point.

Output

Print three integers separated by spaces: a, b and D(a, b).

Notes

In the first example the best point is (3, 2), which is not an office. The distances from it to the three offices are 3, 3 and 1, for a total of 7.

In the second example, every point with 0 ≤ a ≤ 10 and 0 ≤ b ≤ 10 gives the same total, 40. The tie-break picks (0, 0).

The total can exceed 2^31, so use 64-bit integers.

Example 1

Input

3
1 1
3 5
4 2

Output

3 2 7

Example 2

Input

4
0 0
10 0
0 10
10 10

Output

0 0 40

Hints · reveal one at a time if you're stuck

Hint 1

The total distance is a sum of a part depending only on a and a part depending only on b. Can you minimize the two parts separately?

Hint 2

For one coordinate, the sum of absolute differences is smallest at a median of the values. With an even number of offices several values are optimal, so the tie-break decides which one; the smallest integer minimizer is the one to use. Keep the total in 64-bit integers.

report an issue Prefer your own editor? sooks prep 135 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.