Lemonade Line
InterviewTime limit2sMemory limit512 MB
Given each cow's maximum tolerated queue length, choose an arrival order that minimizes how many cows end up waiting in line.
- Level
Medium4 of 10
- Topics
- Greedy, Sorting, Array, Implementation
- Solved
- No attempts yet
Problem
It is a hot summer day on the farm, and Farmer John is serving lemonade to his cows. Every cow likes lemonade, but some like it more than others. The cows are numbered through , and cow is willing to wait in line behind at most other cows to get her lemonade.
Right now all cows are out in the fields. As soon as Farmer John rings his cowbell, the cows head straight for the lemonade stand. They all arrive before he starts serving, and no two cows arrive at the same time. When cow arrives, she joins the line if and only if at most cows are already in it. Otherwise she turns away.
Farmer John wants to prepare the lemonade in advance without wasting any. The number of cows who join the line depends on the order in which they arrive. Find the smallest number of cows who can end up in the line.
Input
The first line contains . The second line contains the space separated integers . It is guaranteed that and that for each cow .
Output
Print, on one line, the smallest number of cows who join the line, taken over all possible arrival orders.
Note
In the first example only three cows end up in line, and no arrival order does better. Suppose the cows with and arrive first and wait in line. The cow with then arrives and turns away, since two cows are already in line. The two cows with arrive last, one staying and one turning away.