#C6745. Equalizing Plant Water Levels
Equalizing Plant Water Levels
Equalizing Plant Water Levels
You are given n plants, each with a certain water level. In one operation, you can either increase or decrease the water level of a single plant by 1. The goal is to make the water levels of all plants equal using the minimum number of such operations.
Mathematically, if the water levels are \(a_1, a_2, \dots, a_n\), you need to choose an integer \(m\) such that the total cost \(\sum_{i=1}^{n} |a_i - m|\) is minimized. It is well known that the optimal \(m\) is the median of the given water levels.
inputFormat
Input is read from standard input (stdin). The first line contains an integer n
(the number of plants). The second line contains n
space-separated integers representing the water levels of the plants.
outputFormat
Output the minimum number of operations required to equalize the water levels to standard output (stdout).## sample
4
2 3 1 5
5