atcoder#ABC217H. [ABC217H] Snuketoon

[ABC217H] Snuketoon

Score : 600600 points

Problem Statement

In a game called Snuketoon, developed by AtCoder, Inc., the player plays as Snuke to dodge water from water guns.

The platform consists of an infinite number line, and Snuke is at the point 00 at the beginning of the game. Starting from the beginning of the game, Snuke can make one of the three moves in each second: move 11 in the negative direction, move 11 in the positive direction, or remain still. More formally, if Snuke's position is pp at tt seconds (t0t \geq 0, tt is an integer) from the beginning of the game, his position at t+1t+1 seconds can be p1p-1, pp, or p+1p+1.

Snuke takes damage from getting soaked by water guns. The water guns shoot NN times, and the ii-th shot is represented by TiT_i, DiD_i, and XiX_i as follows.

  • At TiT_i seconds from the beginning of the game, water is shot from the left or right. Let pp be Snuke's position at that time. He takes the following damage if he is in the following range.- When Di=0D_i = 0, he takes XipX_i - p points of damage if he is in the range p<Xip \lt X_i.
    • When Di=1D_i = 1, he takes pXip - X_i points of damage if he is in the range Xi<pX_i \lt p.

Takahashi, a pro gamer, wants to minimize the total damage taken by Snuke after the end of the NN-th shot, to post a tweet on this game. Find the total damage taken by Snuke when the game is played to minimize it.

Constraints

  • 1N2×1051 \leq N \leq 2 \times 10^5
  • 1T1<T2<<TN1091 \leq T_1 \lt T_2 \lt \dots \lt T_N \leq 10^9
  • DiD_i (1iN)(1 \leq i \leq N) is 00 or 11.
  • 109Xi109-10^9 \leq X_i \leq 10^9 (1iN)(1 \leq i \leq N)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN

T1T_1 D1D_1 X1X_1

T2T_2 D2D_2 X2X_2

\vdots

TNT_N DND_N XNX_N

Output

Print the number of points of damage taken by Snuke when the game is played to minimize it.

3
1 0 3
3 1 0
4 0 6
7

For convenience, let tt denote the number of seconds elapsed from the beginning of the game. The optimal course of movements for Snuke until all the shots are done is as follows.

  • When t=0t = 0, Snuke is at the point 00. He moves 11 in the positive direction.
  • When t=1t = 1, Snuke is at the point 11 and takes 22 points of damage from the first shot. He moves 11 in the negative direction.
  • When t=2t = 2, Snuke is at the point 00. He remains still.
  • When t=3t = 3, Snuke is at the point 00 and takes no damage from the second shot. He moves 11 in the positive direction.
  • When t=4t = 4, Snuke is at the point 11 and takes 55 points of damage from the third shot.

Here, Snuke takes a total of 77 points of damage, so you should print 77.

3
1 0 1
6 1 1
8 0 -1
0
5
1 0 1000000000
2 1 -1000000000
3 0 1000000000
4 1 -1000000000
5 0 1000000000
4999999997