cswork / OA打开练习工作台

JPMorgan ChaseOA #11

Minimum Total Cost

估计中等可在线判题

考点(AI 标注)贪心调度原地数组

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.

输入格式

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 a single integer answer.

样例 1

输入:

4
1 4 2 1

输出:

6

样例 2

输入:

2
-1 -1

输出:

0

样例 3

输入:

3
-2 2 -2

输出:

8

样例说明

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