PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000361. Tòa nhà xa nhất có thể đến (Furthest Building You Can Reach)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

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 nn consecutive buildings along a route, numbered from 00 to n−1n-1. Building ii has height hih_i. You start at building 00 with BB bricks and LL ladders.

Each move goes from building ii to its immediate successor i+1i+1.

  • If hi+1≤hih_{i+1}\le h_i, the move needs neither bricks nor a ladder.
  • If hi+1>hih_{i+1}>h_i, choose either one ladder or exactly hi+1−hih_{i+1}-h_i 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 n,B,Ln,B,L, the number of buildings, bricks and ladders, respectively.

The second line contains the nn integer heights h0,h1,…,hn−1h_0,h_1,\ldots,h_{n-1} in route order.

Output

Print one integer: the largest reachable building index, using zero-based numbering.

Subtasks

  • Subtask 1 (20 points): 1≤n≤121\le n\le 12; 1≤hi≤1061\le h_i\le 10^6 for every 0≤i<n0\le i<n; 0≤B≤1090\le B\le 10^9; 0≤L≤n0\le L\le n.
  • Subtask 2 (30 points): 1≤n≤20001\le n\le 2000; 1≤hi≤1061\le h_i\le 10^6 for every 0≤i<n0\le i<n; 0≤B≤1090\le B\le 10^9; 0≤L≤n0\le L\le n.
  • Subtask 3 (50 points): 1≤n≤1051\le n\le 10^5; 1≤hi≤1061\le h_i\le 10^6 for every 0≤i<n0\le i<n; 0≤B≤1090\le B\le 10^9; 0≤L≤n0\le L\le n.

Examples

Input 1

7 5 1
4 2 7 6 9 14 12

Output 1

4

Sample 1

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.