A company has
The company wants to move stock between warehouses so that every warehouse ends up holding exactly the same amount,
Find the minimum total cost to make all warehouses hold the same amount of stock, choosing the best possible target value
The first line contains a single integer KaTeX can only parse string typed expression — the number of warehouses.
The second line contains KaTeX can only parse string typed expression integers KaTeX can only parse string typed expression — the current stock at each warehouse.
Print a single integer — the minimum total cost to equalize all warehouses.
Input
4
1 10 2 9Output16ExplanationChoosing target KaTeX can only parse string typed expression (or KaTeX can only parse string typed expression, both give the same minimum cost here since they are the two middle values when sorted): the total cost is KaTeX can only parse string typed expression.
Example 2:
Input
1
5Output0ExplanationWith a single warehouse, it is already trivially equal to itself, so no cost is needed.
Example 3:
Input
3
-5 -5 -5Output0ExplanationAll warehouses already hold the same amount, so no cost is needed.