A warehouse has n shelf columns, indexed 1..n, each starting at height 0. A forklift operation selects any contiguous range of columns [l, r] (of any length, including a single column) and raises every column in that range by exactly 1 unit. Given a target height array target[1..n] (all non-negative), determine the MINIMUM number of forklift operations needed so that every column ends up at exactly its target height (columns can only go up, and the final heights must equal the targets exactly - no more, no less). Print this minimum count.
Line 1: an integer n.
Line 2: n space-separated non-negative integers, the target heights.
A single integer: the minimum number of forklift operations required.
Example 1
Input
5 2 4 4 1 3
Expected
6
Explanation
Reading left to right with an implicit 0 before column 1: column1 rises by 2 (0->2), column2 rises by 2 (2->4), column3 has no rise (4->4), column4 falls (4->1, needs no new lifts), column5 rises by 2 (1->3). Total new lifts: 2+2+0+0+2=6.
Example 2
Input
3 5 5 5
Expected
5
Explanation
Column1 rises by 5 from the implicit 0; columns 2 and 3 show no further rise, so 5 lifts (each covering the full range) suffice.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →