<p>Given an integer array <code>nums</code> and two integers <code>firstLen</code> and <code>secondLen</code>, return <em>the maximum sum of elements in two non-overlapping <strong>subarrays</strong> with lengths </em><code>firstLen</code><em> and </em><code>secondLen</code>.</p>

<p>The array with length <code>firstLen</code> could occur before or after the array with length <code>secondLen</code>, but they have to be non-overlapping.</p>

<p>A <strong>subarray</strong> is a <strong>contiguous</strong> part of an array.</p>

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

<pre>
<strong>Input:</strong> nums = [0,6,5,2,2,5,1,9,4], firstLen = 1, secondLen = 2
<strong>Output:</strong> 20
<strong>Explanation:</strong> One choice of subarrays is [9] with length 1, and [6,5] with length 2.
</pre>

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

<pre>
<strong>Input:</strong> nums = [3,8,1,3,2,1,8,9,0], firstLen = 3, secondLen = 2
<strong>Output:</strong> 29
<strong>Explanation:</strong> One choice of subarrays is [3,8,1] with length 3, and [8,9] with length 2.
</pre>

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

<pre>
<strong>Input:</strong> nums = [2,1,5,6,0,9,5,0,3,8], firstLen = 4, secondLen = 3
<strong>Output:</strong> 31
<strong>Explanation:</strong> One choice of subarrays is [5,6,0,9] with length 4, and [0,3,8] with length 3.
</pre>

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

<ul>
	<li><code>1 &lt;= firstLen, secondLen &lt;= 1000</code></li>
	<li><code>2 &lt;= firstLen + secondLen &lt;= 1000</code></li>
	<li><code>firstLen + secondLen &lt;= nums.length &lt;= 1000</code></li>
	<li><code>0 &lt;= nums[i] &lt;= 1000</code></li>
</ul>

class Solution:
    def maxSumTwoNoOverlap(self, nums: List[int], firstLen: int, secondLen: int) -> int:
        n = len(nums)
        def helper(l):
            curr_sum = [0] * (n - l + 1)
            dp = [0] * (n - l + 1) # dp[i] = maximum secondLen-subarray sum in dp[i:]
            for i in range(n-1, n-1-l, -1): # base case
                curr_sum[-1] += nums[i]
                dp[-1] += nums[i]
            curr = dp[-1]
            for i in range(n - l - 1, -1, -1):
                curr -= (nums[i + l] - nums[i])
                curr_sum[i] = curr
                dp[i] = max(curr, dp[i + 1])
            return curr_sum, dp
        cs1, dp1 = helper(firstLen)
        cs2, dp2 = helper(secondLen)
        print(dp1)
        print(cs1)
        print(dp2)
        print(cs2)
        ans = 0
        # first on the left
        for i in range(len(cs1)):
            if i + firstLen < len(dp2):
                ans = max(ans, cs1[i] + dp2[i + firstLen])
        # second on the left
        for i in range(len(cs2)):
            if i + secondLen < len(dp1):
                ans = max(ans, cs2[i] + dp1[i + secondLen])
        return ans
16600