#C7621. Equalize Building Heights
Equalize Building Heights
Equalize Building Heights
You are given a list of non-negative integers representing the heights of different buildings. Your task is to compute the minimum total number of height increments needed to make all the buildings reach the height of the tallest building. You can only increase the height of a building and cannot decrease any height.
If the list is empty, output -1
.
In mathematical terms, if the list of heights is \(H = [H_1, H_2, \dots, H_n]\) and \(H_{max} = \max\{H_1, H_2, \dots, H_n\}\), then the answer is given by:
[ \text{result} = \sum_{i=1}^{n} \left(H_{max} - H_i\right), ]
For example, for the input list [4, 7, 8, 6]
, the maximum height is 8. The required increments are \(8-4=4,\ 8-7=1,\ 8-8=0,\ 8-6=2\). Therefore, the total required increase is \(4 + 1 + 0 + 2 = 7\).
inputFormat
The first line of input contains a single integer \(n\) indicating the number of buildings. If \(n = 0\), it indicates an empty list. If \(n > 0\), the second line contains \(n\) space-separated non-negative integers representing the heights of the buildings.
outputFormat
Output a single integer on a new line which is the total number of height increases required to equalize all buildings to the height of the tallest one. If the list is empty, output -1
.
4
4 7 8 6
7
</p>