Problem list
Log in
#icpc00389 · ICPC GCPC 2024

Musical Mending

Gold V 2.0 s 2048 MB

Shortly before the concert starts, you notice that your piano is completely out of tune! Having the ability of relative pitch, you are able to discern the difference between the pitch of any piano key to the first piano key. While this does not help you find the absolute pitch, you decide to at least tune the keys relative to each other. To do this, you need to make sure that the pitch of each key is exactly one higher than the key before it and one lower than the key after it. As the concert will start shortly, you need to minimize the total tuning effort, which is the sum of the absolute changes in pitch you apply to each key. For example, Figure illustrates a solution for Sample Input 3, resulting in a total tuning effort of 23.

Input

The input consists of:

  • One line with an integer $n$ ($2 \leq n \leq 10^5$), the number of keys on the piano.
  • One line with $n$ integers $t_1,\dots,t_n$ ($-2 \cdot 10^5 \leq t_i \leq 2 \cdot 10^5$), where $t_i$ describes the difference in pitch between the $i$th key and the first key.

The first integer $t_1$ is always $0$.

Output

Output the minimal total tuning effort.

Example 1

Input
7
0 1 2 3 4 5 6
Output
0

Example 2

Input
5
0 1 4 3 6
Output
4

Example 3

Input
6
0 -2 10 6 7 -1
Output
23

Example 4

Input
4
0 -4 -2 1
Output
7

Example 5

Input
9
0 23452 145043 -3423 -20 9845 435 -3 4453
Output
186237
Source
ICPC GCPC 2024
Appears in
Not part of a ladder, contest or set yet.
Accepted 0 Submissions 0 Acceptance 0% Solvers 0

Discussion

Threads about this problem. Spoilers are collapsed; wrap hints in ||double bars||.

No discussion yet. Start a thread if something here needs explaining.

Recent submissions Open with filters
Code