atcoder#ABC217H. [ABC217H] Snuketoon
[ABC217H] Snuketoon
Score : 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 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 in the negative direction, move in the positive direction, or remain still. More formally, if Snuke's position is at seconds (, is an integer) from the beginning of the game, his position at seconds can be , , or .
Snuke takes damage from getting soaked by water guns. The water guns shoot times, and the -th shot is represented by , , and as follows.
- At seconds from the beginning of the game, water is shot from the left or right. Let be Snuke's position at that time. He takes the following damage if he is in the following range.- When , he takes points of damage if he is in the range .
- When , he takes points of damage if he is in the range .
Takahashi, a pro gamer, wants to minimize the total damage taken by Snuke after the end of the -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
- is or .
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
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 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 , Snuke is at the point . He moves in the positive direction.
- When , Snuke is at the point and takes points of damage from the first shot. He moves in the negative direction.
- When , Snuke is at the point . He remains still.
- When , Snuke is at the point and takes no damage from the second shot. He moves in the positive direction.
- When , Snuke is at the point and takes points of damage from the third shot.
Here, Snuke takes a total of points of damage, so you should print .
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