Special Oreum Climb
InterviewTime limit1sMemory limit1024 MB
Find the highest y coordinate on any valid path from (0, 0) to (2N, 0) that avoids teacher points, or report -1 if no such path exists.
- Level
Medium4 of 10
- Topics
- Dynamic programming, Implementation
- Solved
- No attempts yet
Problem
Oreum must be climbed.

The name of the NLCS Jeju dormitory, "Oreum," comes from the Jeju word for a peak or mountain. Once a year, the students of each dormitory climb the oreum that gave their dormitory its name.
The oreum is a triangle with vertices , , and in the plane. You start at and must reach .
From , you can move only to or . Every position you arrive at must be inside the oreum or on its boundary.
It is easy to get lost on the oreum, so teachers are stationed at points where people tend to lose their way. You have not done your homework, so you do not want to meet a teacher. Move while avoiding the teachers.
You also decide to take photos to make the climb memorable. A photo is more beautiful when it is taken at a higher point. Specifically, the coordinate of the place where the photo is taken is its beauty score. Find the maximum beauty score of a photo you can take.
Input
The first line contains , the height of the oreum, and , the number of points where teachers are, separated by a space.
Lines 2 to each contain the coordinate and the coordinate of a teacher, separated by a space. Every teacher is inside the oreum or on its boundary.
Output
Print the maximum beauty score of a photo you can take on the first line. If you cannot travel from the start to the destination, print -1.
Constraints
- is even.
- There is no teacher at .