cswork / OAOpen practice workspace

UberOA #7

Stepwise Pattern (Min Operations)

Estimated MediumJudge-ready

Topics (AI-labelled)GreedyIn-place Arrays

Each operation increases the height of one building by 1. In the end, the heights must form a strict arithmetic staircase with common difference +1 or −1. Find the minimum number of increases over the two directions.

This site's standard input and output are explicit.

Input

The first line contains n, and the second line contains n heights; 2≤n≤100000, 1≤height[i]≤10⁹.

Output

Output one integer, the answer.

Example 1

Input:

4
1 4 3 2

Output:

4

Example 2

Input:

5
5 7 9 4 11

Output:

9

Example 3

Input:

2
2 1

Output:

0

Example notes

Example 1: raise the first building to 5 to form 5, 4, 3, 2, for 4 operations in total. Example 2: form 7, 8, 9, 10, 11, for a total increase of 9. Example 3: it is already a descending staircase, so 0 operations.

Editorial

Loading solution…