cswork / OAOpen practice workspace

JPMorgan ChaseOA #11

Minimum Total Cost

Estimated MediumJudge-ready

Topics (AI-labelled)GreedyIn-place Arrays

Choose any non-empty prefix and add an integer x to all of it, at a cost of |x|; find the minimum total cost to make all elements equal. The final common value is not specified.

Input

The first line contains n, followed by n integers. The original gives no numeric ranges; this site adds 1≤n≤200000, −1000000000≤a[i]≤1000000000.

Output

Output a single integer answer.

Example 1

Input:

4
1 4 2 1

Output:

6

Example 2

Input:

2
-1 -1

Output:

0

Example 3

Input:

3
-2 2 -2

Output:

8

Example notes

The outputs of the three public examples are, in order: 6; 0; 8. These answers have been checked by independent enumeration or direct simulation.

Editorial

Loading solution…