<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:
        def calculate_cost(arr):
            
            arr = [abs(x) for x in arr]
            
            
            arr.append(0)
            
            stack = [0]
            
            cost = 0
            
            for num in arr:
                while stack and num <= stack[-1]:
                    prev_sky = stack.pop()
                    
                    if stack:
                        cost += prev_sky - max(num,stack[-1])
                                
                stack.append(num)
            return cost + sum(stack)
        
        n = len(nums)
        
        diff = [target[idx]- nums[idx] for idx in range(n)]
        
        ans = 0
        
        idx = 0
        
        def get_sign(num):
            if num == 0:
                return 0
            
            if num > 0:
                return 1
            return -1
        
        
        while idx < len(diff):
            if diff[idx] == 0:
                idx +=1
                continue
                
            sign = 1
            if diff[idx] < 0:
                sign = -1
                
            start = idx
            end = idx+1
            
            while end < len(diff) and get_sign(diff[end]) == sign:
                end +=1
                
            arr = diff[start:end]
            
            
            ans += calculate_cost(arr)
            
            idx = end
            

        return ans
    
    
        
34000