#K50477. Minimum Operations to Equalize Array Elements

    ID: 28873 Type: Default 1000ms 256MiB

Minimum Operations to Equalize Array Elements

Minimum Operations to Equalize Array Elements

You are given a sequence of n integers. In one operation, you may choose any contiguous subarray and add 1 to every element in that subarray. The goal is to make all elements in the sequence equal.

It can be proven that to achieve this goal by only incrementing elements (and without exceeding the target), the minimum number of operations required is:

$$\text{operations} = \max(\text{sequence}) - \min(\text{sequence})$$

For instance, if the sequence is [1, 2, 3, 4], then the answer is 3 because \(4 - 1 = 3\).

inputFormat

The first line contains an integer \(n\) (1 \(\leq n \leq\) 105) representing the number of elements in the array. The second line contains \(n\) space-separated integers which are the elements of the array.

outputFormat

Output a single integer, which is the minimum number of operations required to make all the elements equal.

## sample
4
1 2 3 4
3

</p>