Under construction

Still being built. Things may change

Harmonic sequence

Time limit1sMemory limit512 MB

Summary
Find the minimum sum of |a_i - b_i| over all harmonic sequences A, where each inner term equals the sum of its two neighbors.
Level

Medium7 of 10

Topics
Math, Binary search, Sorting
Solved
No attempts yet

Statement

A professor at a university in Flatland is running a lecture series on sequences. A sequence of integers a1,a2,…,ana_1, a_2, \ldots, a_n is called harmonic if every term except a1a_1 and ana_n equals the sum of its two neighbors: a2=a1+a3a_2 = a_1 + a_3, a3=a2+a4a_3 = a_2 + a_4, ..., an−1=an−2+ana_{n-1} = a_{n-2} + a_n. For example, [1,2,1,−1][1, 2, 1, -1] is harmonic because 2=1+12 = 1 + 1 and 1=2+(−1)1 = 2 + (-1).

For two sequences of equal length, A=[a1,…,an]A = [a_1, \ldots, a_n] and B=[b1,…,bn]B = [b_1, \ldots, b_n], the distance is d(A,B)=∣a1−b1∣+∣a2−b2∣+…+∣an−bn∣d(A, B) = |a_1 - b_1| + |a_2 - b_2| + \ldots + |a_n - b_n|. For example, d([1,2,1,−1],[1,2,0,0])=0+0+1+1=2d([1, 2, 1, -1], [1, 2, 0, 0]) = 0 + 0 + 1 + 1 = 2.

At the end of a lecture, the professor wrote a sequence of nn integers B=[b1,…,bn]B = [b_1, \ldots, b_n] on the board and assigned students the task of finding a harmonic sequence A=[a1,…,an]A = [a_1, \ldots, a_n] with minimal d(A,B)d(A, B). To make checking easy, the professor asks for only the minimal distance as the answer. Write a program that finds the minimal distance from BB to a harmonic sequence.

Input

The first line contains an integer nn, the number of elements in the sequence (3≤n≤300 0003 \le n \le 300\,000).

The second line contains nn integers b1,b2,…,bnb_1, b_2, \ldots, b_n (−109≤bi≤109-10^9 \le b_i \le 10^9).

Output

Print one integer: the minimal possible distance from the sequence in the input to a harmonic sequence.

Hint

In the example, one optimal harmonic sequence is [1,2,1,−1][1, 2, 1, -1].

Examples1

  1. Example 1

    Input
    4
    1 2 0 0
    
    Expected output
    2