Musical Mending

면접 대비

시간 제한2초메모리 제한1024 MB

요약
각 건반의 첫 건반 대비 음높이 차이가 주어질 때, 음높이가 매 단계 정확히 1씩 커지도록 만드는 최소 총 조정량을 구한다.
난이도

보통10점 중 4점

유형
수학, 그리디, 누적 합
정답자
아직 제출이 없습니다

문제

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 M.1 illustrates a solution for Sample Input 3, resulting in a total tuning effort of 23.

Figure M.1: Visualization of Sample Input 3.

입력

The input consists of:

  • One line with an integer nn (2≤n≤1052 \leq n \leq 10^5), the number of keys on the piano.
  • One line with nn integers t_1,…,t_nt\_1,\dots,t\_n (−2⋅105≤t_i≤2⋅105-2 \cdot 10^5 \leq t\_i \leq 2 \cdot 10^5), where t_it\_i describes the difference in pitch between the iith key and the first key. The first integer t_1t\_1 is always 00.

출력

Output the minimal total tuning effort.

예제5

  1. 예제 1

    입력
    7
    0 1 2 3 4 5 6
    
    예상 출력
    0
    
  2. 예제 2

    입력
    5
    0 1 4 3 6
    
    예상 출력
    4
    
  3. 예제 3

    입력
    6
    0 -2 10 6 7 -1
    
    예상 출력
    23
    
  4. 예제 4

    입력
    4
    0 -4 -2 1
    
    예상 출력
    7
    
  5. 예제 5

    입력
    9
    0 23452 145043 -3423 -20 9845 435 -3 4453
    
    예상 출력
    186237