Problem 597
Furthest Building You Can Reach
Medium · 20 pts · Greedy, Heap
Problem
You walk along buildings with the given heights, starting at building 0. Moving to the next building is free if it is not taller. If it is taller by d, you must use either d bricks or one ladder. Find the furthest building index you can reach with the given bricks and ladders. Greedy: keep the climbs in a min-heap and give ladders to the largest climbs. Whenever the heap holds more climbs than ladders, pay the smallest one with bricks; stop when the bricks run out.
Input
The first line contains N, B (bricks) and L (ladders). The second line contains the N heights.
Output
Print the furthest reachable building index (counted from 0).
Constraints
1 <= N <= 100000; 1 <= height <= 10^6; 0 <= B <= 10^9; 0 <= L <= N
Example input
7 5 1 4 2 7 6 9 14 12
Example output
4
Explanation
Heights 4 2 7 6 9 14 12 with 5 bricks and 1 ladder: go down to 2 for free, climb 5 to 7 with bricks, go down to 6, climb 3 to 9 with the ladder, but the climb of 5 to 14 cannot be paid. So the answer is 4.