<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 issorted(self,a):
        sort = False
        mi,ma = 0,0
        for i in range(len(a)):
            if(a[mi] > a[i]):
                mi = i
            if(a[ma] < a[i]):
                ma = i
        for i in range(len(a)-1):
            if(a[i] <= a[i+1]):
                continue
            else:
                break
        else:
            sort = True
        if(not sort):
            for i in range(len(a)-1):
                if(a[i] >= a[i+1]):
                    continue
                else:
                    break
            else:
                sort = True
        return (sort,mi,ma)
    
    def generate(self,arr,lim=0):
        if(len(arr) == 0):
            return 0
        if(len(arr) == 1):
            return abs(arr[0]) - lim
        a = len(arr)
        if(arr[-1]>0):
            ret = self.issorted(arr)
            if(ret[0]):
                return arr[ret[2]] - lim
            ind = ret[1]
            r1 = self.generate(arr[:ind],arr[ind])
            r2 = self.generate(arr[ind+1:],arr[ind])
            return r1 + r2 + arr[ind] - lim

        else:
            ret = self.issorted(arr)
            if(ret[0]):
                return arr[ret[1]]*-1 - lim
            ind = ret[2]
            r1 = self.generate(arr[:ind],abs(arr[ind]))
            r2 = self.generate(arr[ind+1:],abs(arr[ind]))
            return r1 + r2 + abs(arr[ind]) - lim
        return 0


    def minimumOperations(self, nums: List[int], target: List[int]) -> int:
        diff = []
        n = len(nums)
        for i in range(n):
            diff.append(target[i] - nums[i])
        res = 0
        cnt = [diff[0]]
        flag = diff[0] > 0
        i = 1
        while(i < len(diff)):
            if(diff[i] > 0 ):
                if(flag):
                    cnt.append(diff[i])
                else:
                    flag = True
                    res += self.generate(cnt)
                    cnt = [diff[i]]
            else:
                if(not flag):
                    cnt.append(diff[i])
                else:
                    flag = False
                    res += self.generate(cnt)
                    cnt = [diff[i]]
            i+=1
        res += self.generate(cnt)
        return res
31400