Description
Spring kindergarten held the annual "building block competition ". This year's competition is about building a building with a width of n. The building can be regarded as composed of N blocks with a width of 1 ?? The final height of block blocks must be hi.
No building blocks (N blocks with a height of 0 can be considered) before the building starts ). Next, each operation, the children can select a continuous interval [L, R], and then between the L Block and the R block (including the L Block and the R Block) the height of all building blocks increases by 1.
John M is a smart child. She quickly came up with the best strategy to build a building, minimizing the number of operations required for the building. However, she is not a very diligent child, so I would like to ask you to implement this strategy and find the minimum number of operations.
Format input format
The input contains two rows. The first row contains an integer N, indicating the width of the building.
The second row contains N integers, and the I-th integer is hi.
Output Format
Only one row, that is, the minimum number of operations required for building.
Example 1 input 1
5
2 3 4 1 2
Sample output 1
5
Prompt
One of the feasible best solutions is to select[] [] [] [] []
For 30% of data, 1 ≤ n ≤ 10;
For 70% of data, 1 ≤ n ≤ 1000;
For 100% of data, 1 ≤ n ≤ 100000,0 ≤ Hi ≤ 10000.
Solution:
I wanted to write it with rmq, but it's a pity that it's down,
After reading the problem on the internet, I went through greedy O (N). I ......
Find the minimum value of the entire segment, and then find a bunch of non-decreasing intervals. The maximum value of each interval is added and the minimum value of the entire segment is equal to orz.
Noip 2013 block game