Sugar Glider
Time limit2sMemory limit256 MB
Starting partway up tree 1, climb trees and glide between them while losing one meter of height per glide second to reach the top of tree N in minimum time.
- Level
Hard8 of 10
- Topics
- Shortest path, Heap
- Solved
- No attempts yet
Problem
JOI the sugar glider lives in a forest with eucalyptus trees numbered 1 to . Tree has height meters.
There are pairs of trees JOI can glide between directly, each with a fixed glide time. While gliding, height drops 1 meter per second. If current height is and a glide takes seconds, landing height is . Gliding is impossible if is below 0 or above the destination tree height.
JOI can climb or descend along a tree between 0 and that tree's height at 1 meter per second.
JOI starts at height on tree 1 and wants the top of tree (height ). Find the minimum time, or if impossible.
Input
From standard input:
- Line 1: integers , ,
- Next lines: height of tree
- Next lines: , , describing a bidirectional glide of seconds
Output
Print one integer: minimum seconds to reach the top of tree , or if impossible.