#K72862. Minimum Operations to Equalize Array

    ID: 33848 Type: Default 1000ms 256MiB

Minimum Operations to Equalize Array

Minimum Operations to Equalize Array

You are given an array of integers. In one operation, you can choose any contiguous subarray and increment every element in that subarray by 1. The goal is to make all the elements of the array equal using the minimum number of operations.

Mathematically, if the maximum element in the array is (m) and the array elements are (a_1, a_2, \dots, a_n), then one valid strategy is to compute the number of operations as:

[ \sum_{i=1}^{n} (m - a_i) ]

This strategy works under the assumption that individually incrementing each element to match (m) is optimal under the given operation constraints.

Note: The input will be provided via standard input and the output should be written to standard output.

inputFormat

The first line contains an integer (n) representing the number of elements in the array. The second line contains (n) space-separated integers (a_1, a_2, \dots, a_n).

outputFormat

Output a single integer, the minimum number of operations required to make all elements of the array equal.## sample

4
2 2 2 2
0

</p>