Make all numbers equal 2
Time limit2sMemory limit512 MB
Given a sequence where one Add raises a whole run of equal neighboring values by 1, find the minimum number of Adds to make all values equal.
- Level
Medium5 of 10
- Topics
- Greedy, Implementation
- Solved
- No attempts yet
Problem
A sequence of natural numbers is given. The operation Add(i) raises by 1. It does not touch alone: the whole run of neighboring positions that currently hold the same value as goes up by 1 at once. and are not adjacent.
Look at the sequence {1, 1, 1, 1, 3, 3, 1}. Add(2) raises together with the equal values next to it and gives {2, 2, 2, 2, 3, 3, 1}. Add(4) then gives {3, 3, 3, 3, 3, 3, 1}, and Add(1) after that gives {4, 4, 4, 4, 4, 4, 1}.
You want to use the Add operation several times until . Find the smallest number of Add operations that is enough.
Input
The first line contains the integer . Each of the next lines contains one value, through in order.
, and every is a natural number no greater than .
Output
Print the smallest number of Add operations on the first line.