Problem list
Log in
#coci00230 · COI 2012

Setnja

Platinum IV 1.0 s 256 MB

Mirko is taking all his friends to a Zaz concert soon to be held in Zagreb. He has already obtained the tickets, and is now walking around the neighbourhood delivering them to everyone. Mirko's neighbourhood can be represented by a Cartesian plane. While walking, Mirko is stepping only on lattice (integer-coordinates) points of that plane. He takes a single step to move to any one of eight adjacent lattice points (up, down, left, right, or diagonally in any direction). Each one of Mirko's friends also lives at some lattice point (x, y), and is willing to walk some distance to meet him. Specifically, Mirko will meet a given friend at some lattice point with distance of at most P steps from this friend's house, with P depending on the specific friend's laziness. After finishing his stroll, Mirko has recalled the order in which he has met with his friends. Compute the minimum possible number of steps that Mirko had to make during his stroll. Mirko's starting and ending position are not known.

Input

The first line of input contains the positive integer N ($2 \le N \le 200,000$), the number of Mirko's friends. Each of the following N lines contains three integers describing one friend: x, y, and P ($0 \le x$, y, P ≤ 200 000). Friends are given in the order in which Mirko has met them.

Output

The first and only line of output must contain the required minimum possible number of steps that Mirko had to make.

Constraints

In test data worth a total of 30 points, all numbers in the input (including N) will be at most 200. In test data worth an additional 30 points, no friend will have P greater than 10.

Example 1

Input
3
3 10 2
8 4 2
2 5 2
Output
4

Example 2

Input
4
3 3 5
7 11 5
20 8 10
30 18 3
Output
19

First sample description: Mirko could have started his stroll at coordinates (4, 8), met the first friend,
taken two steps to the point (6, 6), met the second friend, taken two steps to the point (4, 5) and met
the third friend there.
Source
COI 2012
Appears in
Not part of a ladder, contest or set yet.
Accepted 0 Submissions 0 Acceptance 0% Solvers 0

Discussion

Threads about this problem. Spoilers are collapsed; wrap hints in ||double bars||.

No discussion yet. Start a thread if something here needs explaining.

Recent submissions Open with filters
Code