Under construction

Still being built. Things may change

Meeting in Manhattan

Interview

Time limit1sMemory limit1024 MB

Summary
Given M points in N dimensions, find a point that minimizes the total Manhattan distance to all of them, and print that minimum sum and the point.
Level

Medium4 of 10

Topics
Sorting, Math
Solved
No attempts yet

Problem

Like many clubs, the MatKor club in the Department of Cyber Defense at Korea University meets regularly. During the semester, meeting at school makes sense, but during vacation it does not.

So Dongwoo, who wanted to find a sensible meeting place, treats each member's home as a point and decides to meet at the Fermat point.

A Fermat point is a point that minimizes the sum of distances to all given points.

Dongwoo likes taking taxis, so he uses taxicab distance, which is the Manhattan distance, when finding the Fermat point.

Besides location, he considers several other factors, so he treats each home's coordinates as an N-dimensional point.

Let point AA have coordinates (A1,A2,⋯ ,AN)(A_1, A_2, \cdots, A_N) and point BB have coordinates (B1,B2,⋯ ,BN)(B_1, B_2, \cdots, B_N). The Manhattan distance dABd_{AB} between the two points is defined as

dAB=∑i=1N∣Ai−Bi∣=∣A1−B1∣+∣A2−B2∣+⋯+∣AN−BN∣d_{AB} = \sum_{i=1}^N \lvert A_i - B_i\rvert = \lvert A_1 - B_1\rvert + \lvert A_2 - B_2\rvert + \cdots + \lvert A_N - B_N\rvert

Dongwoo surveyed the homes of MM club members and got MM distinct points in N-dimensional coordinates. Find the Manhattan-Fermat point F=(F1,F2,⋯ ,FN)F = (F_1, F_2, \cdots, F_N) that minimizes the total Manhattan distance to these points.

Input

The first line contains two integers NN and MM, the dimension and the number of points, separated by a space (1≤N,M≤1 0001 \le N, M \le 1\,000).

Each of the next MM lines contains NN integers separated by spaces, the coordinates Pi=(Pi,1,Pi,2,⋯ ,Pi,N)P_i = (P_{i,1}, P_{i,2}, \cdots, P_{i,N}) of the ii-th point (−1012≤Pi,j≤1012-10^{12} \le P_{i,j} \le 10^{12}).

Output

On the first line, print the total Manhattan distance from the Manhattan-Fermat point to all given points.

On the second line, print the coordinates F=(F1,F2,⋯ ,FN)F = (F_1, F_2, \cdots, F_N) of the Manhattan-Fermat point, separated by spaces.

Every coordinate of the Manhattan-Fermat point must be an integer with absolute value at most 101210^{12}. If several points satisfy the condition, print any of them.

For every input, at least one Manhattan-Fermat point that satisfies the condition is guaranteed to exist.

Examples2

  1. Example 1

    Input
    1 3
    1
    3
    7
    
    Expected output
    6
    3
    
  2. Example 2

    Input
    2 4
    1 0
    3 4
    5 6
    7 10
    
    Expected output
    20
    4 5