Trapped in the Haybales
Time limit1sMemory limit256 MB
Bessie breaks any bale smaller than her running start, and you must find the cheapest single bale to enlarge so she never leaves the outer bales.
- Level
Medium7 of 10
- Topics
- Two pointers, Sorting, Simulation
- Solved
- No attempts yet
Problem
Farmer John received hay bales and put them at various points along the straight road that connects his barn with his house. Bale has size and position , and no two bales share a position. Bessie the cow stands at position , where there is no bale.
Bessie moves freely along the road and can walk right up to the position of a bale, but she cannot pass through that position. There is one exception: if she runs in the same direction for units of distance, she builds up enough speed to smash through a bale of size strictly less than and remove it for good. Removing a bale gives her a longer run, so she may smash other bales as well.
FJ is repainting his house and his barn, and he wants Bessie to reach neither one. He therefore wants to make sure she never breaks through the leftmost or the rightmost bale. To help, he can pick a single bale and add hay to it, raising its size by any nonnegative amount. Find the smallest amount of hay he has to add to keep Bessie trapped.
Input
The first line contains and Bessie's starting position . Each of the next lines describes one bale with two integers, its size and its position.
, and every size, every position and are integers between and . The positions are distinct, and no bale sits at position .
Output
Print a single integer, the smallest amount of hay FJ has to add. Print if Bessie is already trapped. Print if she escapes no matter which bale gets hay and how much.