## WC '18 Contest 3 J4 - Leveling Up

View as PDF

Points: 7 (partial)
Time limit: 1.0s
Memory limit: 64M

Author:
Problem type
##### Woburn Challenge 2018-19 Round 3 - Junior Division

Jessie loves her Arbok, but the poor snake seems to not have much luck winning any battles. Jessie's decided to turn things around by helping Arbok level up! There's no better way to train than going around and defeating wild Pokémon who are just minding their own business, so that's exactly what Jessie intends on doing.

Jessie and her Arbok, as well as wild Pokémon, are all standing at various points along a trail, which can be represented as a number line. Jessie's initial position along the trail is , while the -th wild Pokémon's position is . All of these positions are distinct.

Jessie can walk in either the positive or negative direction along the trail. However, whenever she arrives at the same location as a wild Pokémon, sneaking by is out of the question - she must have Arbok battle it.

Arbok's initial level is , while the -th wild Pokémon's level is . Arbok can defeat a Pokémon if Arbok's current level is greater than or equal to that Pokémon's level. If Arbok defeats the -th wild Pokémon, Arbok's current level will increase by , and that Pokémon will faint and no longer occupy a point on the trail. Jessie will never make Arbok battle against a Pokémon whose level is strictly greater than Arbok's level, as Arbok would faint instead and would not be able to gain any more levels.

What's the maximum possible level which Jessie can help Arbok achieve, by optimally choosing how to walk around on the trail?

#### Input Specification

The first line of input consists of a single integer, .
The next line consists of two space-separated integers, and .
lines follow, the -th of which consists of three space-separated integers, , , and , for .

#### Output Specification

Output a single integer, the maximum possible level which Arbok can achieve.

#### Sample Input

5
8 5
3 9 2
19 2 9
12 6 8
5 2 1
15 17 3

#### Sample Output

16

#### Sample Explanation

The following diagram illustrates Jessie's starting location (indicated in blue) as well as the wild Pokémon (indicated in brown):

Jessie can move left to position , and defeat the Pokémon there to increase Arbok's level to . She can then move right to position , defeating the Pokémon there and raising Arbok's level to . Finally, she can defeat the Pokémon at position to increase Arbok's level to . This is the highest level which Arbok would ever be able to reach.