#K95172. Minimum Total Modification Cost
Minimum Total Modification Cost
Minimum Total Modification Cost
Given an array of integers, your task is to compute the minimum total modification cost required to make all elements equal. The cost to change an element is defined as the absolute difference between its original value and the chosen target value. It can be proven that setting all elements to the median minimizes the total cost. Note that if the array is empty, the cost is 0.
The cost function is given by: [ \text{cost} = \sum_{i=1}^{n} |a_i - m| ]
where (m) is the median of the array.
inputFormat
The input is given via standard input (stdin) and consists of two lines:
- The first line contains a single integer (n) ((n \ge 0)) representing the number of elements in the array.
- The second line contains (n) space-separated integers representing the array elements. If (n = 0), the second line will be empty.
outputFormat
Output a single integer to standard output (stdout) representing the minimum total modification cost needed to equalize the array.## sample
3
1 3 2
2
</p>