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 [copy]
52 3 4 1 2
Sample output 1 [copy]
5
Restrictions
1 s for each test point.
Prompt
One of the feasible best solutions is to select[1,5] [1,3] [2,3] [3,3] [5,5]
For 30% of data, 1 ≤ n ≤ 10;
For 70% of data, 1 ≤ n ≤ 1000;
For 100% of data, 1 ≤ n ≤ 100000,0 ≤ Hi ≤ 10000.
Source
Noip 2013 raise group Day 2
Analysis questions, for every H [I] if greater than H [I-1] then be sure to stack H [I]-H [I-1] block blocks to make the building blocks can be built, otherwise the problem cannot meet the requirements, but if H [I] <H [I-1] Then don't waste building blocks to build it, then we can judge if H [I]> H [I-1] Then ans + = H [I]-H [I-1] otherwise do not operate should note, the first building block needs to be built several times in any case. Otherwise, wa
# Include <cstdio>
# Define maxn100010
Using namespace STD;
Int N, H [maxn];
Long long ans = 0;
Int main ()
{
Scanf ("% d", & N );
For (INT I = 1; I <= N; I ++) scanf ("% d", & H [I]);
For (INT I = 0; I <n; I ++)
If (H [I] <H [I + 1]) ans + = (H [I + 1]-H [I]);
Printf ("% LLD", ANS );
Return 0;
}
[Noip2013] building blocks Competition