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
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
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,
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
Comments