Happy Sequence
InterviewTime limit3sMemory limit512 MB
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 is called happy if every element except the first and the last one equals the sum of its two neighbours, that is, for every with .
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 , the length of the sequence. ()
The second line contains integers separated by spaces. ()
Output
Print the minimum number of steps.
Note
In the second example, 6 steps are enough to reach the happy sequence .