#BS0000361. Tòa nhà xa nhất có thể đến (Furthest Building You Can Reach)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Tòa nhà xa nhất có thể đến (Furthest Building You Can Reach)
Furthest Building You Can Reach
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
There are consecutive buildings along a route, numbered from to . Building has height . You start at building with bricks and ladders.
Each move goes from building to its immediate successor .
- If , the move needs neither bricks nor a ladder.
- If , choose either one ladder or exactly bricks.
Used resources are not recovered. You need not exhaust the available bricks or ladders.
Find the largest building index you can reach.
Input
The first line contains integers , the number of buildings, bricks and ladders, respectively.
The second line contains the integer heights in route order.
Output
Print one integer: the largest reachable building index, using zero-based numbering.
Subtasks
- Subtask 1 (20 points): ; for every ; ; .
- Subtask 2 (30 points): ; for every ; ; .
- Subtask 3 (50 points): ; for every ; ; .
Examples
Input 1
7 5 1
4 2 7 6 9 14 12
Output 1
4

Explanation 1
Move freely from 0 to 1, spend five bricks to reach 2, move freely to 3, then use the ladder to reach 4. Reaching 5 requires climbs of 5, 3 and 5. Even using the ladder on a largest climb leaves 3+5=8 bricks needed, exceeding five. The furthest index is 4.
Input 2
9 10 2
4 12 2 7 3 18 20 3 19
Output 2
7
Explanation 2
Use ladders on climbs 8 (0 to 1) and 15 (4 to 5). Spend five bricks from 2 to 3 and two from 5 to 6. Descents are free, so building 7 is reachable with three bricks left. Reaching 8 requires climbs 8,5,15,2,16; after assigning ladders to 16 and 15, the remaining cost is 8+5+2=15 bricks, more than ten.
Input 3
4 17 0
14 3 19 3
Output 3
3
Explanation 3
The move from 0 to 1 is free. Spend 19−3=16 bricks to reach 2, leaving one brick. The descent to 3 is free. Building 3 is the final building.