cswork / OAOpen practice workspace

PaypalOA #4

Modify Array

Estimated HardJudge-ready

Topics (AI-labelled)DP BasicsStacks & Heaps

Find the minimum total absolute modification cost to turn the array into a non-decreasing or a non-increasing sequence; each position may be changed to any integer value, at a cost equal to the absolute difference between the new and old values.

Input

The first line contains n; the second line contains n integers. The original constraints are 1≤n≤1000 and 1≤arr[i]≤10^9; this site widens the value range to 0..10^9 to keep the 0 in the third source example.

Output

Output the minimum total modification cost over the two directions, as a 64-bit non-negative integer.

Example 1

Input:

6
9 8 7 2 3 3

Output:

1

Example 2

Input:

5
1 2 3 3 4

Output:

0

Example 3

Input:

7
0 1 2 5 6 5 7

Output:

1

Editorial

Loading solution…