#P7399. Minimum Operations to Transform Array

    ID: 20594 Type: Default 1000ms 256MiB

Minimum Operations to Transform Array

Minimum Operations to Transform Array

Given an array of length \(n\) initially filled with zeros, you are allowed to perform the following operation:

  • Select a contiguous subarray \([l, r]\) and add a positive integer \(x\) to all elements in that range.

The operations have a restriction: for any two operations, their intervals must either be completely disjoint or one must be completely contained within the other.

Given a target array \(a\), determine the minimum number of operations required to transform the original zero array into \(a\).

Hint: The answer can be computed as \(a_0 + \sum_{i=1}^{n-1} \max(0, a_i - a_{i-1})\).

inputFormat

The first line contains an integer \(n\), the length of the array.

The second line contains \(n\) space-separated integers representing the target array \(a\).

outputFormat

Output a single integer representing the minimum number of operations required.

sample

3
1 2 3
3