Container Rearrangement
InterviewTime limit1sMemory limit1024 MB
Given container counts in n slots, find the fewest containers to move so that every pair of slots differs in height by at most 1.
Problem
A yard at a port has slots in a row on its floor where containers can be stacked, and the yard currently holds containers. Every container has height 1, and there is no limit on how many containers a slot can hold. If () is the number of containers stacked in the -th slot, then holds.
With no height limit, the counts in each slot can differ greatly, which can cause safety problems. So a crane moves some containers to other slots so that the height difference between any two slots is at most 1. That is, must hold for any and . Containers can be moved only one at a time, and moving a container has no extra cost based on distance.
For example, in Figure 1, 35 containers are stacked in 8 slots, so and . Rearranging them so that the height difference is at most 1 gives the result in Figure 2, and 5 containers must be moved.
Input
The first line contains a positive integer (), the number of slots. The next line contains non-negative integers , the number of containers currently in each slot. The total number of containers satisfies .
Output
Print, on one line, the minimum number of containers that must be moved to satisfy the condition.

