Making Numbers Equal
Time limit2sMemory limit128 MB
Given a line of n numbers, compute the minimum number of block-increment operations (merging equal adjacent runs) needed to make all elements equal.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Array, Greedy
- Solved
- No attempts yet
Problem
There are natural numbers arranged in a line, where . One operation Add(i) increases by 1 the entire maximal contiguous block of equal values that contains . In other words, every element equal to and connected to it through adjacent equal elements increases together. and are not adjacent.
For instance, suppose the array is . If Add(2) is performed, the first four 1s increase together and the array becomes . If Add(4) is then performed, it becomes . Performing Add(1) after that makes it .
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 . The next lines contain in order. Every input value is a natural number not greater than .
Output
Print the minimum number of Add operations required to make all elements equal. This value is not greater than .