Vito's family
Time: 1 s · Memory: 256 MB
- Sorting
Statement
Vito wants to move to a long street where of his relatives live; the house of the -th relative has number . The distance between two houses is the absolute difference of their numbers.
Vito visits all his relatives every day, so he wants to choose a house that minimises the sum of distances to all relatives (the house number can be any integer). Find this minimum sum.
Input format
The first line contains (). The second line contains integers (). Several relatives may live in the same house.
Output format
A single number — the minimum sum of distances.
Examples
Input
3 2 4 6
Output
4
Input
4 7 1 9 3
Output
12
Note
The sum is minimised by = the median (the middle element of the sorted array): if is left of the median, moving it right decreases the sum, and vice versa. For even any between the two middle elements works. In the first example and the answer is .
The sum reaches — a 64-bit type is needed.
Source: UVa 10041.
