cswork / OA打开练习工作台

UberOA #7

Stepwise Pattern (Min Operations)

估计中等可在线判题

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

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.

输入格式

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

输出格式

Output one integer, the answer.

样例 1

输入:

4
1 4 3 2

输出:

4

样例 2

输入:

5
5 7 9 4 11

输出:

9

样例 3

输入:

2
2 1

输出:

0

样例说明

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.