Yunny's Trip
Time limit2sMemory limit512 MB
Find the minimum stamina needed to walk from (0, 0) to (Ex, Ey) with unit moves (cost 1) and repeatable item vectors (cost 2), never dropping below 0.
- Level
Hard8 of 10
- Topics
- Math, Number theory, Greedy
- Solved
- No attempts yet
Problem
Yunny enters Uniworld, her virtual world, to take a short break. Uniworld is an infinite two-dimensional grid, and Yunny is currently standing at . To reach her destination , Yunny can repeat the following two kinds of actions.
- Move one cell to an adjacent cell. That is, change the coordinate by or change the coordinate by . This action consumes stamina.
- Use one of the items she is carrying. Each item is given as . Using it increases her coordinate by and her coordinate by . So if she is at , using the item moves her to . This action does not consume the item, so the same item can be used many times. It consumes stamina.
If her stamina drops below , she returns to the real world, so she keeps her stamina at or above. Yunny is frail, so her initial stamina does not exceed . Determine whether she can reach the destination. If she can, find the minimum stamina she must spend to get there.
Input
The first line contains the number of items and Yunny's initial stamina , separated by a space. (; )
Each of the next lines contains and for one item, separated by a space. ()
The last line contains the destination and , separated by a space. ()
No item or destination is , and no item is given more than once.
Output
Print the minimum stamina that must be spent to reach the destination. If Yunny cannot reach the destination with her initial stamina , print -1.