LCC '22 Contest 2 J3 - Qual-leaf-ications

View as PDF

Submit solution

Points: 5
Time limit: 2.0s
Memory limit: 128M

Author:
Problem type

Shane has just become a new swimming coach. One may even say he is very qual-leaf-fied for the job, since he has won several swimming competitions. However, because he is 16, he can only coach little children.

Shane has N potential children he can coach for money. However, he must prove to the child's parent that he is qual-leaf-fied to teach. Shane will only be able to coach the i^{th} child if their parent sees that Shane has coached at least A_i children.

Shane is poor, so he wants to teach all N children for money. He has no experience, so he decides to teach K other children for free first to rack up experience. Shane is also lazy, so he wants to know what the minimum value of K is, so he can earn money as soon as possible.

Constraints

1 \le N \le 10^6

0 \le A_i \le 10^9

Input Specification

The first line will contain a single integer N, the number of potential children Shane can coach for money.

The next line will contain A, represented by N space-separated integers. A_i is the minimum amount of children Shane must coach to be qual-leaf-fied to teach the i^{th} child.

Output Specification

Output K, the minimum amount of children Shane must coach for free in order to coach all N children for money.

Sample Input 1

5
5 3 1 2 3

Sample Output 1

1

Sample Explanation 1

If Shane teaches one child for free, he can teach the 3^{rd} child for money, while also gaining another coaching experience. Then, he can teach the 4^{th} child he now has coached two children. He can then coach the 5^{th} and 2^{nd} child, before finally teaching the 1^{st} child. Note that if Shane does not coach one child for free, he cannot teach any of the N children for money.

Sample Input 2

20
2 3 53 6 12 6 3 21 6 2 3 42 2 6 12 3 5 6 1 34

Sample Output 2

34

Comments

There are no comments at the moment.