<p>You are given an integer array <code>target</code>. You have an integer array <code>initial</code> of the same size as <code>target</code> with all elements initially zeros.</p>

<p>In one operation you can choose <strong>any</strong> subarray from <code>initial</code> and increment each value by one.</p>

<p>Return <em>the minimum number of operations to form a </em><code>target</code><em> array from </em><code>initial</code>.</p>

<p>The test cases are generated so that the answer fits in a 32-bit integer.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> target = [1,2,3,2,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> We need at least 3 operations to form the target array from the initial array.
[<strong><u>0,0,0,0,0</u></strong>] increment 1 from index 0 to 4 (inclusive).
[1,<strong><u>1,1,1</u></strong>,1] increment 1 from index 1 to 3 (inclusive).
[1,2,<strong><u>2</u></strong>,2,1] increment 1 at index 2.
[1,2,3,2,1] target array is formed.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> target = [3,1,1,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> [<strong><u>0,0,0,0</u></strong>] -&gt; [1,1,1,<strong><u>1</u></strong>] -&gt; [<strong><u>1</u></strong>,1,1,2] -&gt; [<strong><u>2</u></strong>,1,1,2] -&gt; [3,1,1,2]
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> target = [3,1,5,4,2]
<strong>Output:</strong> 7
<strong>Explanation:</strong> [<strong><u>0,0,0,0,0</u></strong>] -&gt; [<strong><u>1</u></strong>,1,1,1,1] -&gt; [<strong><u>2</u></strong>,1,1,1,1] -&gt; [3,1,<strong><u>1,1,1</u></strong>] -&gt; [3,1,<strong><u>2,2</u></strong>,2] -&gt; [3,1,<strong><u>3,3</u></strong>,2] -&gt; [3,1,<strong><u>4</u></strong>,4,2] -&gt; [3,1,5,4,2].
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= target.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= target[i] &lt;= 10<sup>5</sup></code></li>
</ul>

import heapq

class Solution:
    def minNumberOperations(self, target: List[int]) -> int:
        n = len(target)
        operations = 0

        for i in range(n):
            if i + 1 < n:
                diff = target[i] - target[i+1]

                if diff > 0:
                    operations += diff

            else:
                operations += target[i]

        return operations


"""
input: int array target
output: int minimum number of operations to create target

target = [1,2,3,2,1]

[1,2,3,2,1]

[0:4]
[1:3]
[2:2]
operations = 3

target = [3,1,1,2]
operations = 4
[2,1,1,2]
(0,3)
(3,3)
(0,0)
(0,0)

Do reverse, how many times to decrement initial to get to target?

target = [0,0,0,0,0]

(0,4)
(0,0)
(0,0)
(2,4)
(2,3)
(2,3)
(2,2)

chip away at mountains
0s are valleys that cannot be chipped further
Greedily chip away at mountain ranges, removing height of minimum mountain
How to know which indices are part of the same mountain?
How to know where mountains are separated?

Perform DFS/BFS and chip away at mountain ranges
operation = 57
{
    -0: 1
    -1: 0, 2
    -2: 1, 3
    -3: 2, 4
    -4: 3
}

target = [3,1,5,4,2]
operations = 7
[2,0,4,3,1]
[0,0,4,3,1]
[0,0,3,2,0]
[0,0,1,0,0]
[0,0,0,0,0]

removing minimum first [1,2,3,4,5]
minheap (value, index)
use left and right pointers to subtract

target = [3,1,5,4,2] -> 

For a given range of values, optimal to reduce entire range by the minimum value
We know that if there is a valley between mountains then it will take diff of height to cut down the mountain
Smaller peaks next to larger ones will be taken care of already
"""
27600