Po
InterviewTime limit1sMemory limit512 MB
Given a nonnegative array built by adding positive values to nested or disjoint segments starting from zeros, find the minimum number of such segment additions needed.
- Level
Medium6 of 10
- Topics
- Greedy, Stack, Array, Implementation
- Solved
- No attempts yet
Problem
Tinky Winky left a sequence of zeroes in the Tubbytronic Superdome, and left for a walk with Dipsy. When he came back, he saw that a misdeed had been done. The sequence was changed, and Po was smiling mischievously in the corner of the room.
Oh dear! Po, what have you done?! asked Tinky Winky in horror.
I enhanced the sequence! replied Po.
After cross-examination, it was established that Po did a number of enhancements on the sequence. In every enhancement, she took a segment of the sequence and increased all elements in the segment by some positive integer. Also, every two segments were either disjoint or one was completely contained in the other.
How many enhancements have you done, Po? Laa-Laa inquired.
I really don't know! I'm only sure I did the minimum number of enhancements possible to get this sequence! said Po exhaustedly.
Then it surely must be ! proclaimed Noo-Noo. (Noo-Noo is the Teletubbies' vacuum cleaner pet)
What number did Noo-Noo say?
Input
The first line contains an integer (), the length of the sequence.
The second line contains nonnegative integers (), the sequence after Po's enhancements.
Output
Output , the minimum possible number of enhancements.