This page is still under construction.

Parts of this page are still being built. What you see may change.

Warehouse

Time limit1sMemory limit128 MB

Summary
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 11 to 500,000,000500{,}000{,}000 from west to east, and the h-streets are numbered from 11 to 500,000,000500{,}000{,}000 from south to north. Every v-street crosses every h-street, so a crossroads is written as a pair (x,y)(x, y) meaning the xx-th v-street meets the yy-th h-street. Consecutive v-streets, and consecutive h-streets, are exactly one kilometre apart.

There are nn 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 (xi,yi)(x_i, y_i) receives tit_i deliveries per day.

A lorry may travel along the streets and diagonally across the blocks, so the length of a shortest route between crossroads (xi,yi)(x_i, y_i) and (xj,yj)(x_j, y_j) is the Chebyshev distance max⁡(∣xi−xj∣, ∣yi−yj∣)\max(|x_i - x_j|,\ |y_i - y_j|) kilometres.

If the warehouse is built at crossroads (xm,ym)(x_m, y_m), the lorry's total daily driving distance is

D(xm,ym)=2∑i=1nti⋅max⁡(∣xm−xi∣, ∣ym−yi∣),D(x_m, y_m) = 2 \sum_{i=1}^{n} t_i \cdot \max(|x_m - x_i|,\ |y_m - y_i|),

where the factor 22 counts the return leg of every course. Determine the smallest possible value of DD over all crossroads at which the warehouse could be built.

Input

The first line contains one integer nn (1≤n≤100,000)(1 \le n \le 100{,}000), the number of shops.

Each of the next nn lines contains three integers xix_i, yiy_i and tit_i (1≤xi,yi≤500,000,000, 1≤ti≤1,000,000)(1 \le x_i, y_i \le 500{,}000{,}000,\ 1 \le t_i \le 1{,}000{,}000), separated by single spaces: the ii-th shop stands at the crossing of the xix_i-th v-street and the yiy_i-th h-street and receives tit_i deliveries per day.

Output

Print one integer: the minimum possible total daily driving distance, that is min⁡(xm,ym)D(xm,ym)\min_{(x_m, y_m)} D(x_m, y_m) taken over every crossroads (xm,ym)(x_m, y_m).

Hint

In the first example the shops sit at (2,2)(2, 2), (6,2)(6, 2) and (4,6)(4, 6), each with one delivery per day. Building the warehouse at (4,4)(4, 4) makes the Chebyshev distance to every shop equal to 22, so the total daily distance is 2⋅(2+2+2)=122 \cdot (2 + 2 + 2) = 12, which is optimal.

Examples3

  1. Example 1

    Input
    3
    2 2 1
    6 2 1
    4 6 1
    
    Expected output
    12
    
  2. Example 2

    Input
    1
    7 7 1
    
    Expected output
    0
    
  3. Example 3

    Input
    2
    5 5 3
    5 5 10
    
    Expected output
    0