Warehouse
Time limit1sMemory limit128 MB
Find a crossroads minimizing the weighted sum of Chebyshev distances to n shops.
- Level
Hard8 of 10
- Topics
- Geometry, Binary search, Sorting, Divide and conquer
- Solved
- No attempts yet
Problem
The streets of New Byte City form a rectangular grid. The streets running east-west are called h-streets, and those running north-south are called v-streets. The v-streets are numbered from to from west to east, and the h-streets are numbered from to from south to north. Every v-street crosses every h-street, so a crossroads is written as a pair meaning the -th v-street meets the -th h-street. Consecutive v-streets, and consecutive h-streets, are exactly one kilometre apart.

There are shops in the city, each standing at a crossroads. A merchant supplies every shop from a single warehouse, which also stands at a crossroads. Each delivery is a separate course: the lorry leaves the warehouse, drives to one shop, and drives back, always taking a shortest route each way. The shop at receives deliveries per day.
A lorry may travel along the streets and diagonally across the blocks, so the length of a shortest route between crossroads and is the Chebyshev distance kilometres.
If the warehouse is built at crossroads , the lorry's total daily driving distance is
where the factor counts the return leg of every course. Determine the smallest possible value of over all crossroads at which the warehouse could be built.
Input
The first line contains one integer , the number of shops.
Each of the next lines contains three integers , and , separated by single spaces: the -th shop stands at the crossing of the -th v-street and the -th h-street and receives deliveries per day.
Output
Print one integer: the minimum possible total daily driving distance, that is taken over every crossroads .
Hint
In the first example the shops sit at , and , each with one delivery per day. Building the warehouse at makes the Chebyshev distance to every shop equal to , so the total daily distance is , which is optimal.
