cswork / OA打开练习工作台

BNPOA #2

Piles Of Boxes

估计中等可在线判题

考点(AI 标注)原地数组贪心调度

You are given several piles of boxes. Each step may only choose one current tallest pile and lower it to the next height that is strictly less than the current maximum height. Find the minimum number of steps needed to make all piles the same height. Only one pile changes per step; a whole level cannot be processed at once, and the next-highest height cannot be skipped.

输入格式

The first line contains a non-negative integer n, followed by n whitespace-separated heights. Following the original Java int[] and the meaning of non-negative box counts, this site interprets heights as 0..2147483647, with 0 being the public convention for an empty pile. The original gives no numeric upper bound on n; this site supports any valid transmittable array within the full 32MiB standard input budget of a single test case, without inventing a small n limit. n=0 is the empty collection, a mathematical extension by this site.

输出格式

Output the minimum number of steps as a 64-bit integer. Output 0 when all heights are already equal, there is only one pile, or there are no piles.

样例 1

输入:

3
5 2 1

输出:

3

样例 2

输入:

4
5 5 2 1

输出:

5

样例 3

输入:

2
0 2147483647

输出:

1

样例说明

The first example keeps the original [5,2,1]→3: lower 5 to 2 once, then lower each of the two 2s to 1 once. The second and third public examples were derived by this site: [5,5,2,1]→5, [0,2147483647]→1; the difference in box counts is not the number of moves.