Question
Equalizing Warehouse Stock

A company has N warehouses arranged along a supply chain, each currently holding A_i units of stock (this value may be negative, representing a pending shortfall that must be resolved).

The company wants to move stock between warehouses so that every warehouse ends up holding exactly the same amount, x (the company may choose any integer value of x). Moving stock into or out of a warehouse to change its value by 1 unit costs exactly 1.

Find the minimum total cost to make all warehouses hold the same amount of stock, choosing the best possible target value x.

Input

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.

Output

Print a single integer — the minimum total cost to equalize all warehouses.

Example
Example 1:
Input
4

1 10 2 9
Output
16
Explanation
Choosing 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

5
Output
0
Explanation
With a single warehouse, it is already trivially equal to itself, so no cost is needed.

Example 3:
Input
3

-5 -5 -5
Output
0
Explanation
All warehouses already hold the same amount, so no cost is needed.

Online