<p>You are given two positive integer arrays <code>nums</code> and <code>target</code>, of the same length.</p>

<p>In a single operation, you can select any subarray of <code>nums</code> and increment each element within that subarray by 1 or decrement each element within that subarray by 1.</p>

<p>Return the <strong>minimum</strong> number of operations required to make <code>nums</code> equal to the array <code>target</code>.</p>

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

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [3,5,1,2], target = [4,6,2,4]</span></p>

<p><strong>Output:</strong> <span class="example-io">2</span></p>

<p><strong>Explanation:</strong></p>

<p>We will perform the following operations to make <code>nums</code> equal to <code>target</code>:<br />
- Increment&nbsp;<code>nums[0..3]</code> by 1, <code>nums = [4,6,2,3]</code>.<br />
- Increment&nbsp;<code>nums[3..3]</code> by 1, <code>nums = [4,6,2,4]</code>.</p>
</div>

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

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,3,2], target = [2,1,4]</span></p>

<p><strong>Output:</strong> <span class="example-io">5</span></p>

<p><strong>Explanation:</strong></p>

<p>We will perform the following operations to make <code>nums</code> equal to <code>target</code>:<br />
- Increment&nbsp;<code>nums[0..0]</code> by 1, <code>nums = [2,3,2]</code>.<br />
- Decrement&nbsp;<code>nums[1..1]</code> by 1, <code>nums = [2,2,2]</code>.<br />
- Decrement&nbsp;<code>nums[1..1]</code> by 1, <code>nums = [2,1,2]</code>.<br />
- Increment&nbsp;<code>nums[2..2]</code> by 1, <code>nums = [2,1,3]</code>.<br />
- Increment&nbsp;<code>nums[2..2]</code> by 1, <code>nums = [2,1,4]</code>.</p>
</div>

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

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

class Solution:
    def minimumOperations(self, nums: List[int], target: List[int]) -> int:
        #[99, 99, 101, 99, 103, 99]
        #[100, 100, 100, 100, 100, 100]
        #optimal is 1 + 1 + 1 + 3 + 1 = 7?
        #how to find?
        #1 1 -1 1 -3 1
        #1 2 1 -3 -5 -3 2 2
        #1 2 3 3 2 1 = 3 (4) (1) change + min - 2
        #3 2 1 3 2 1 = 5 (4) (1) 
        #6 5 4 6 5 4 = 8 (4) (4)
        #3 4 6 6 4 3 = 6 (3) (3)
        #1 2 4 4 2 1 = 4 (3) (1)
        #10 3 7 1 9 3 = 9 + 4 + 8
        #5 7 2 1 3 19 = 24
        #total_change -
        def calculate_differences(nums):
            global_min = min(nums)
            
            nums = [global_min] + nums + [global_min]
            total_diff = 0
            prev_point = global_min
            going_up = True
            for i in range(1, len(nums) - 1):
                if going_up and nums[i] > nums[i + 1]:
                    #print(nums[i], " is a local max ", abs(prev_point - nums[i]))
                    total_diff += abs(prev_point - nums[i])
                    prev_point = nums[i]
                    going_up = False
                if not going_up and nums[i] < nums[i + 1]:
                    #print(nums[i], " is a local min", abs(prev_point - nums[i]))
                    #total_diff += abs(prev_point - nums[i])
                    prev_point = nums[i]
                    going_up = True
            
            return total_diff + global_min
        subbed_nums = [(nums[i] - target[i]) for i in range(len(nums))]
        prev = None
        total_change = 0
        res = 0
        curr = []
        for i in subbed_nums:
            if prev == None:
                prev = i
                curr.append(abs(i))
            else:
                if i * prev > 0: #continue
                    curr.append(abs(i))
                elif i * prev <= 0:
                    res += calculate_differences(curr)
                    curr = [abs(i)]
                    prev = i
        return res + calculate_differences(curr)
                    

                
                

35000