Meeting in Manhattan
InterviewTime limit1sMemory limit1024 MB
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.
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 have coordinates and point have coordinates . The Manhattan distance between the two points is defined as
Dongwoo surveyed the homes of club members and got distinct points in N-dimensional coordinates. Find the Manhattan-Fermat point that minimizes the total Manhattan distance to these points.
Input
The first line contains two integers and , the dimension and the number of points, separated by a space ().
Each of the next lines contains integers separated by spaces, the coordinates of the -th point ().
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 of the Manhattan-Fermat point, separated by spaces.
Every coordinate of the Manhattan-Fermat point must be an integer with absolute value at most . 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.