Warehouse Location
Warehouse Location
There are N shops on a number line, located at positions A1 to AN.
A warehouse needs to be built on the number line. Every morning, goods are delivered from the warehouse to each shop.
To maximize efficiency, find the location to build the warehouse such that the total distance from the warehouse to all shops is minimized.
Input Format
The first line contains an integer N.
The second line contains N integers A1 to AN.
Output Format
Output a single integer, representing the minimum total distance.
Constraints
1 ≤ N ≤ 100000
0 ≤ Ai ≤ 40000
Sample Input
4
6 2 9 1
Sample Output
12
Comments