Convex Sequence

Subtract 1 from elements of a sequence (N up to 50) so that it becomes convex, minimizing total subtractions.

Medium7Dynamic programmingGreedyMathImplementationNo attempts yetTime limit2sMemory limit512 MB

Problem

An integer sequence x0,x1,,xN1x_0, x_1, \dots, x_{N-1} is convex when xi1+xi+12xix_{i-1} + x_{i+1} \ge 2 x_i holds for every ii with 1iN21 \le i \le N-2. A sequence of length 1 or 2 is always convex.

For example, 7, 3, 4, 5, 7 and 4, 2, 1, 3 are convex, while 4, 3, 1, 2 and 5, 7, 3 are not.

You are given a sequence A=a0,a1,,aN1A = a_0, a_1, \dots, a_{N-1}. One operation picks an index ii and replaces aia_i with ai1a_i - 1, and no other operation is allowed. You may pick the same index several times, and an element may become negative. Write a program that finds the minimum number of operations that makes AA convex.

Input

The first line contains the size of the sequence, NN (1N501 \le N \le 50). The second line contains the integers a0,a1,,aN1a_0, a_1, \dots, a_{N-1} (0ai1090 \le a_i \le 10^9) of the sequence AA, separated by spaces.

Output

Print the minimum number of operations that makes AA convex on the first line.