Viewing Terraces
Time limit1sMemory limit128 MB
In a chain of terraces where climbing up costs height difference and descending is free, find the most distinct terraces reachable on k credits without returning to ground.
- Level
Medium5 of 10
- Topics
- Sliding window, Two pointers, Array
- Solved
- No attempts yet
Problem
In the mountains stand viewing terraces connected by elevators. Going up from a lower terrace to the adjacent, higher terrace costs as many credits as the difference between the two terraces' heights. Going down from a higher terrace to a lower one is free. The terraces form a single chain: from the first terrace you can reach only the second, from the second only the first and the third, and so on.
A tourist holds only credits. Find the largest number of distinct terraces the tourist can visit in one continuous trip, without ever coming back down to the ground in between. Stepping onto the terrace where the trip begins costs nothing.
Input
The first line contains two integers and , separated by a single space (, ). Here is the number of terraces and is the number of credits the tourist has.
Each of the next lines contains the height of one terrace, , one per line. Every height satisfies .
Output
Print a single integer: the largest number of terraces the tourist can visit with credits.