cho.sh
Notes
Loading...

Making Numbers Equal

Time limit

2s

Memory limit

128 MB

Problem

There are nnn natural numbers A[1],A[2],A[3],…,A[n]A[1], A[2], A[3], \ldots, A[n]A[1],A[2],A[3],…,A[n] arranged in a line, where 1≤n≤1,0001 \le n \le 1{,}0001≤n≤1,000. One operation Add(i) increases by 1 the entire maximal contiguous block of equal values that contains A[i]A[i]A[i]. In other words, every element equal to A[i]A[i]A[i] and connected to it through adjacent equal elements increases together. A[1]A[1]A[1] and A[n]A[n]A[n] are not adjacent.

For instance, suppose the array is {1,1,1,1,3,3,1}\{1, 1, 1, 1, 3, 3, 1\}{1,1,1,1,3,3,1}. If Add(2) is performed, the first four 1s increase together and the array becomes {2,2,2,2,3,3,1}\{2, 2, 2, 2, 3, 3, 1\}{2,2,2,2,3,3,1}. If Add(4) is then performed, it becomes {3,3,3,3,3,3,1}\{3, 3, 3, 3, 3, 3, 1\}{3,3,3,3,3,3,1}. Performing Add(1) after that makes it {4,4,4,4,4,4,1}\{4, 4, 4, 4, 4, 4, 1\}{4,4,4,4,4,4,1}.

Using Add operations, make all elements have the same value. Find the minimum number of Add operations required.

Input

The first line contains the integer nnn. The next nnn lines contain A[1],A[2],…,A[n]A[1], A[2], \ldots, A[n]A[1],A[2],…,A[n] in order. Every input value is a natural number not greater than 1,000,000,0001{,}000{,}000{,}0001,000,000,000.

Output

Print the minimum number of Add operations required to make all elements equal. This value is not greater than 102510^{25}1025.