Developer

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

요약
같은 값이 연속된 구간의 양옆이 모두 그 값과 다른 경우가 없도록, 각 원소를 바꾸는 절댓값 비용의 합을 최소화한다.
난이도

어려움10점 중 8점

유형
동적 계획법, 그리디
정답자
아직 제출이 없습니다

문제

You are in charge of developing new properties in the suburbs of Toruń. You have already decided that there will be one main street and nn properties numbered from 11 to nn along the street. The area is somewhat hilly, and the elevation of the ii-th property is a_ia\_i centimetres.

It turns out that no one wants to buy a property that is on a slope. Formally, for elevations a_1,a_2,…,a_na\_1, a\_2, \dots , a\_n, a slope is a contiguous subsequence a_i−1,a_i,…,a_j,a_j+1a\_{i-1}, a\_i , \dots , a\_j , a\_{j+1} with 2≤i≤j≤n−12 ≤ i ≤ j ≤ n - 1 such that either (i) a_i−1<a_i=a_i+1=⋯=a_j<a_j+1a\_{i-1} < a\_i = a\_{i+1} = \dots = a\_j < a\_{j+1}, or (ii) a_i−1>a_i=a_i+1=⋯=a_j>a_j+1a\_{i-1} > a\_i = a\_{i+1} = \dots = a\_j > a\_{j+1}. Intuitively, a slope is a contiguous range of properties at positions i−1,i,i+1,…,j,j+1i - 1, i, i + 1, \dots , j, j + 1, where the elevations of all properties at positions i,i+1,…,ji, i + 1, \dots , j are equal to some hh, and hh is strictly between a_i−1a\_{i-1} and a_j+1a\_{j+1}.

You are able to increase or decrease the elevation of any property by any integer, but of course you want to minimise the overall effort. Your task is to determine the minimal total change in elevation such that there are no slopes at all. That is, you want to find elevations b_1,b_2,…,b_nb\_1, b\_2, \dots , b\_n without slopes such that ∣a_1−b_1∣+∣a_2−b_2∣+⋯+∣a_n−b_n∣|a\_1 - b\_1| + |a\_2 - b\_2| + \dots + |a\_n - b\_n| is as small as possible. The elevations b_ib\_i must be integers (in particular, they don’t have to be positive), and there are no other constraints on b_ib\_i.

입력

The first line contains an integer nn (1≤n≤2⋅1051 ≤ n ≤ 2 \cdot 10^5) denoting the number of properties along the street.

The second line contains nn integers a_1,a_2,…,a_na\_1, a\_2, \dots , a\_n (0≤a_i≤1090 ≤ a\_i ≤ 10^9), where the ii-th integer a_ia\_i is the initial elevation of the ii-th property.

출력

You should output the minimal total change in elevation to ensure that there are no slopes.

힌트

This is illustrated below. The dashed lines represent the changed elevations without slopes b_ib\_i of their corresponding properties.

예제1

  1. 예제 1

    입력
    11
    7 2 1 2 5 7 8 8 10 8 8
    
    예상 출력
    5