Under construction

Still being built. Things may change

Kuki's Struggle to Open a Restaurant

Time limit2sMemory limit512 MB

Summary
Place the restaurant at a point off both axes so that the gap between the largest and smallest quadrant hunger sums is as small as possible.
Level

Medium6 of 10

Topics
Sorting, Prefix sum, Geometry
Solved
No attempts yet

Problem

Kuki plans to run a restaurant for cats at Catholic University. Kuki believes the location matters most, so Kuki is working out the best spot.

Kuki surveyed the homes of NN cats and recorded each home's coordinates (X,Y)(X, Y) and hunger level ZZ. No two cats live at the same coordinates.

In Kuki's country, there is a law: if a cat's home lies on a coordinate axis relative to a chosen point, the restaurant cannot be opened at that point. The restaurant may be placed at non-integer coordinates.

With the restaurant as the origin, define SS and EE as follows.

  • SiS_i = the sum of hunger levels ZZ of the cats in quadrant ii
  • EE = (the maximum of S1,S2,S3,S4S_1, S_2, S_3, S_4) minus (the minimum of S1,S2,S3,S4S_1, S_2, S_3, S_4)
  • If no cat is in quadrant ii, SiS_i is 0.
  • If the restaurant cannot be opened at a point, EE is 100,000,000.

Kuki wants to open the restaurant where EE is smallest. Help Kuki find the minimum value of EE.

Input

The first line contains an integer NN (4≤N≤1,0004 \le N \le 1,000).

Each of the next NN lines contains three integers XX (−100,000,000≤X≤100,000,000-100,000,000 \le X \le 100,000,000), YY (−100,000,000≤Y≤100,000,000-100,000,000 \le Y \le 100,000,000), and ZZ (1≤Z≤1,0001 \le Z \le 1,000) describing one cat.

Output

Print the minimum value of EE.

Examples1

  1. Example 1

    Input
    6
    -1 3 5
    -1 -2 4
    1 1 2
    6 -4 6
    -7 -7 1
    5 7 4
    
    Expected output
    1