Snakebird
InterviewTime limit1sMemory limit128 MB
Given N fruits at heights h_i and a snake of length L, grow by eating any fruit of height at most the current length and output the maximum length reached.
- Level
Medium4 of 10
- Topics
- Greedy, Sorting, Simulation
- Solved
- No attempts yet
Problem
The snakebird is a cute creature that resembles a mix of a snake and a bird.
The snakebird's main food is fruit, and eating one fruit increases its length by 1.
The fruits hang at fixed heights above the ground, and the height of the i-th fruit (1 ≤ i ≤ N) is hi.
The snakebird can eat fruits whose height is less than or equal to its own length.
Given the snakebird's initial length L, find the maximum length it can reach by eating fruits.
Input
The first line gives the number of fruits N (1 ≤ N ≤ 1,000) and the snakebird's initial length as an integer L (1 ≤ L ≤ 10,000).
The second line gives the integers h1, h2, ..., hN (1 ≤ hi ≤ 10,000).
Output
Print the snakebird's maximum length on the first line.