This page is still under construction.

Parts of this page are still being built. What you see may change.

Recess Rocks

Interview

Time limit3sMemory limit1024 MB

Summary
Reorder the children so that the number who see someone ahead of them with at least as many rocks is minimized.
Level

Medium6 of 10

Topics
Greedy, Sorting, Array, Combinatorics
Solved
No attempts yet

Problem

As one gets older, it becomes harder to keep track of the latest fads and trends. The latest mind-boggling trend is actually sweeping through elementary schools: young children love rocks, even more than they love paper or scissors. As recess ends, Ms. Frizz needs to round up her class. Before going back to the classroom, the students all get in a line, holding bags full of rocks that they've mined from the playground.

The kids in this class are a dangerous combination of personality traits: they are jealous, smart, eagle-eyed, and good at counting. Each child knows how many rocks every child in front of them has. That is, the line leader knows their own rock total, the second kid in line knows the amount of rocks they have and the line leader has, the third student knows the rock totals of the first three students in line, and so on.

If a kid knows about another kid in front of them that has the same or more rocks than them, they begin crying. The line-leader never cries, as they have to set a mature, stoic example for their classmates.

If Ms. Frizz arranges the students optimally, what is the minimum number of students that are crying after recess?

Input

The first line of input is an integer NN (1≤N≤1051 \leq N \leq 10^5. Class sizes are getting large due to budget cuts). The next line has NN space-separated integers; the ii-th is the number of rocks child ii has. Each child has at least 11 and at most 10910^9 rocks.

Output

Output a single integer: the minimum number of children who are crying if the students are arranged optimally.

Hint

In the first sample, the optimal arrangement is [1,1,1,2,2,2][1, 1, 1, 2, 2, 2]. Then the students at indices 2,3,52, 3, 5 and 66 (11-indexed) are crying. No other arrangement has fewer crying children.

In the second sample, all of the kids have the same number of rocks, so any arrangement of them results in two crying children.

Examples2

  1. Example 1

    Input
    6
    1 2 1 2 1 2
    
    Expected output
    4
    
  2. Example 2

    Input
    3
    12 12 12
    
    Expected output
    2