#C4972. Minimum Operations to Sort Books

    ID: 48569 Type: Default 1000ms 256MiB

Minimum Operations to Sort Books

Minimum Operations to Sort Books

You are given a bookshelf divided into n sections, where each section contains a certain number of books. The goal is to rearrange the sections so that the number of books in each section is in non-increasing order.

You are allowed to perform the following two operations:

  1. Swap all the books between two sections.
  2. Move m books from one section to another (provided the source section has at least m books).

Determine the minimum number of operations required to achieve a non-increasing order of books.

If the books are already arranged in non-increasing order, no operations are required.

The challenge is to compute the minimum number of such operations required.

inputFormat

The first line contains an integer n (n1n \geq 1), the number of sections on the shelf. The second line contains n space-separated integers representing the number of books in each section.

outputFormat

Output a single integer: the minimum number of operations required to arrange the books in non-increasing order.## sample

5
4 3 5 2 1
2