Harmonic sequence
Time limit1sMemory limit512 MB
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 is called harmonic if every term except and equals the sum of its two neighbors: , , ..., . For example, is harmonic because and .
For two sequences of equal length, and , the distance is . For example, .
At the end of a lecture, the professor wrote a sequence of integers on the board and assigned students the task of finding a harmonic sequence with minimal . To make checking easy, the professor asks for only the minimal distance as the answer. Write a program that finds the minimal distance from to a harmonic sequence.
Input
The first line contains an integer , the number of elements in the sequence ().
The second line contains integers ().
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 .