Cosmic Assembly

Time limit1sMemory limit1024 MB

Summary
Find integer coordinates (x, y, z) minimizing the sum of Manhattan distances to N given points, breaking ties lexicographically.
Level

Medium4 of 10

Topics
Math, Sorting, Greedy
Solved
No attempts yet

Problem

A space company is organizing a meeting of the executive directors of its NN divisions. Each director has a personal single-seat spaceship, and the coordinates (xi,yi,zi)(x_i, y_i, z_i) of every director are known.

Spaceship fuel is very expensive, so the company wants to pick a meeting location (x,y,z)(x, y, z) that minimizes the sum of all travel distances. Space traffic rules require moving in only one of the xx, yy, or zz directions at any moment, so the distance of a single trip is computed as ∣xi−x∣+∣yi−y∣+∣zi−z∣|x_i - x| + |y_i - y| + |z_i - z|.

Given the directors' coordinates, find the most suitable meeting location. The meeting location must be easy to mark on a map, so its coordinates must be integers.

Input

The first line contains the number of directors NN. Each of the next NN lines contains three space-separated integers xix_i, yiy_i, ziz_i, giving the coordinates of the ii-th director.

Several directors may initially be at the same location.

Output

Output the coordinates of a meeting location that minimizes the total travel distance, as three space-separated integers xx, yy, zz. If several meeting locations are optimal, output the lexicographically smallest one: the smallest xx, breaking ties by the smallest yy, then by the smallest zz.

Constraints

  • 1≤N≤100 0001 \le N \le 100\,000
  • −108≤xi,yi,zi≤108-10^8 \le x_i, y_i, z_i \le 10^8

Examples3

  1. Example 1

    Input
    5
    0 0 1
    0 0 -1
    0 1 0
    0 -2 0
    1 0 0
    
    Expected output
    0 0 0
    
  2. Example 2

    Input
    1
    5 -3 7
    
    Expected output
    5 -3 7
    
  3. Example 3

    Input
    2
    1 1 1
    5 5 5
    
    Expected output
    1 1 1