Walking Santa Claus
Time limit1sMemory limit128 MB
Place a depot on a huge grid so that twice the sum of Manhattan distances to all houses, minus the farthest one, is minimized, and print the best cell.
Problem
Santa Claus wants to hand out chocolate cakes to the children of a certain district. The district's roads form a grid: there are north-south roads and east-west roads. The north-south roads are numbered from west to east, and the east-west roads are numbered from south to north. The intersection of the -th north-south road (counting from the west) and the -th east-west road (counting from the south) is written as . Every house sits on an intersection, and there are houses in total. Santa can move only along the roads, and moving between two adjacent intersections takes time (so the travel time between intersections and is ).
Santa parks Rudolph at one intersection and walks from there to deliver the cakes. He can carry only one cake at a time, so after delivering to a house he must return to the intersection where Rudolph waits, pick up the next cake, and set out again. He wants to minimize the total time needed to deliver a cake to every house. However, after delivering the cake to the last house he does not go back to Rudolph, so that final return trip is not counted.
Given the positions of the houses, determine at which intersection Santa should park Rudolph so that the delivery time is minimized, and report that minimum time.
Input
The first line contains the number of north-south roads and the number of east-west roads . ()
The second line contains the number of houses . ()
Each of the next lines contains the position and of one house. No intersection holds more than one house.
Output
On the first line, print the minimum time needed to deliver a cake to every house. On the second line, print the position and of the intersection where Rudolph should be parked. If several intersections achieve the minimum, print the one farthest to the west (smallest ); if there is still a tie, print the one farthest to the south (smallest ).