Going to School
English
Language
Contribute a translation- Time limit
- 1000 ms
- Memory limit
- 1024 MB
- Submissions
- 0
- Correct
- 0
- Solved by
- 0
- AC rate
- —
Statement
Alex has to arrive at school within $X$ minutes to avoid consequences. To get there, he can take one of $N$ buses that stop at a nearby bus stop.
Alex doesn't want to arrive at school too early, so he looked up the schedules of all $N$ buses. For each bus, he knows how many minutes from now the bus will depart from the bus stop and how many minutes it takes to reach the school.
Determine the latest time at which Alex can board a bus and still arrive at school on time. If no bus can get him to school in time, report that it is impossible.
Constraints
- All numbers in the input are integers.
- $1 \leq N \leq 100$
- $2 \leq X \leq 200$
- For each bus, let $S$ be the number of minutes until the bus departs from the bus stop, and let $T$ be the number of minutes it takes for the bus to travel from the bus stop to the school.
- $1 \leq S \leq 100$
- $1 \leq T \leq 100$
Subtasks
- (10 points) $N = 1$
- (15 points) All buses arrive at the school within $X$ minutes.
- (30 points) $T = 20$ for all buses.
- (45 points) No additional constraints.
Input
The first line contains $N$ and $X$, separated by a space.
The next $N$ lines each contain two integers $S$ and $T$, separated by a space. $S$ is the number of minutes until the bus departs from the bus stop, and $T$ is the number of minutes it takes for the bus to travel from the bus stop to the school.
Output
If it is impossible to arrive at the school within $X$ minutes, output $-1$.
Otherwise, output the number of minutes until the latest bus that allows Alex to arrive at the school within $X$ minutes departs.
Examples
1 30 15 20
-1
3 8 2 1 6 3 4 4
4
Tags
No tags yet