Code Lock
InterviewTime limit2sMemory limit1024 MB
Given n displays where pressing button i copies display i+1 into display i, find the fewest presses that make the sequence non-decreasing.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Array
- Solved
- No attempts yet
Statement
To get into the secret base where Dr. No is hiding, James Bond has to crack a combination lock. Cryptography and lock cracking are not part of his job, so he asked you for help.
The lock has displays, and each display shows a number . Under every display except the last one is a large red button. Pressing the button under display replaces the number on display with the number currently shown on display .
Bond can get into the base only when the numbers on the displays form a non-decreasing sequence. Find the minimum number of button presses needed to reach such a state.
Input
The first line contains one integer (), the number of displays. The second line contains integers (), the numbers shown on the displays before the lock is cracked.
Output
Print one integer, the answer to the problem.