Under construction

Still being built. Things may change

Happy Sequence

Interview

Time limit3sMemory limit512 MB

Summary
Change a sequence with unit-cost edits so every interior term equals the sum of its two neighbours, minimizing total edits.
Level

Medium6 of 10

Topics
Math, Implementation, Greedy, Brute force
Solved
No attempts yet

Problem

A sequence of integers a1,a2,…,ana_1, a_2, \dots, a_n is called happy if every element except the first and the last one equals the sum of its two neighbours, that is, aj=aj−1+aj+1a_j = a_{j-1} + a_{j+1} for every jj with 2≤j≤n−12 \le j \le n-1.

You are given a sequence of integers. In one step you can choose one element of the sequence and increase or decrease it by one. Find the minimum number of steps needed to obtain a happy sequence.

Input

The first line contains a positive integer nn, the length of the sequence. (3≤n≤300 0003 \le n \le 300\,000)

The second line contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n separated by spaces. (−109≤aj≤109-10^9 \le a_j \le 10^9)

Output

Print the minimum number of steps.

Note

In the second example, 6 steps are enough to reach the happy sequence (2,2,0,−2)(2, 2, 0, -2).

Examples2

  1. Example 1

    Input
    4
    5 1 -4 -5
    
    Expected output
    0
    
  2. Example 2

    Input
    4
    3 2 0 3
    
    Expected output
    6