Events are coming (Easy)
InterviewTime limit1sMemory limit1024 MB
Given times T with forbidden open intervals (A, B), find the minimum distance Minjeong walks from 0 at speed at most 1 to avoid them, or report -1.
- Level
Easy3 of 10
- Topics
- Dynamic programming, Simulation
- Solved
- No attempts yet
Problem
Minjeong is currently at the origin (position 0) of the number line. At any moment, Minjeong can move left or right along the number line at a speed of 1 unit per second. Of course, Minjeong may also stay still.
Minjeong knows that the following event will occur times.
- If Minjeong is strictly between position and position after seconds, Minjeong will be hit by Karina's body bang.
Positions and are safe.
Minjeong wants to move appropriately so as not to be hit by Karina's body bang. Determine whether Minjeong can avoid the body bang by moving, and if so, minimize the travel distance.
Input
The first line contains the number of events .
Each of the next lines contains , , and for one event, separated by spaces.
Output
If Minjeong cannot avoid Karina's body bang, print -1.
If Minjeong can avoid Karina's body bang, print the minimum travel distance of Minjeong.
Constraints
- All given numbers are integers.
- The of each event is distinct.