#K34742. Minimal Cost to Minimize Absolute Differences
Minimal Cost to Minimize Absolute Differences
Minimal Cost to Minimize Absolute Differences
You are given an array of N integers. Your task is to compute the minimal cost required to make all elements as close as possible to a chosen target value. The cost is defined as the sum of the absolute differences between each element and the target value.
Mathematically, the cost is given by: $$\text{Cost} = \sum_{i=1}^{N} \left|a_i - T\right|,$$ where \(T\) is the target value.
It is known that choosing \(T\) as the median of the array minimizes the cost. In this problem, if \(N\) is even, use the element at index \(\frac{N}{2}\) (0-indexed after sorting) as the median.
Input is read from standard input and 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.
outputFormat
Output a single integer which is the minimal cost to make all elements as close to the median as possible.
## sample4
1 3 2 -2
6
</p>