<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code>, both of length <code>n</code>.</p>

<p>You can choose two integers <code>left</code> and <code>right</code> where <code>0 &lt;= left &lt;= right &lt; n</code> and <strong>swap</strong> the subarray <code>nums1[left...right]</code> with the subarray <code>nums2[left...right]</code>.</p>

<ul>
	<li>For example, if <code>nums1 = [1,2,3,4,5]</code> and <code>nums2 = [11,12,13,14,15]</code> and you choose <code>left = 1</code> and <code>right = 2</code>, <code>nums1</code> becomes <code>[1,<strong><u>12,13</u></strong>,4,5]</code> and <code>nums2</code> becomes <code>[11,<strong><u>2,3</u></strong>,14,15]</code>.</li>
</ul>

<p>You may choose to apply the mentioned operation <strong>once</strong> or not do anything.</p>

<p>The <strong>score</strong> of the arrays is the <strong>maximum</strong> of <code>sum(nums1)</code> and <code>sum(nums2)</code>, where <code>sum(arr)</code> is the sum of all the elements in the array <code>arr</code>.</p>

<p>Return <em>the <strong>maximum possible score</strong></em>.</p>

<p>A <strong>subarray</strong> is a contiguous sequence of elements within an array. <code>arr[left...right]</code> denotes the subarray that contains the elements of <code>nums</code> between indices <code>left</code> and <code>right</code> (<strong>inclusive</strong>).</p>

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

<pre>
<strong>Input:</strong> nums1 = [60,60,60], nums2 = [10,90,10]
<strong>Output:</strong> 210
<strong>Explanation:</strong> Choosing left = 1 and right = 1, we have nums1 = [60,<u><strong>90</strong></u>,60] and nums2 = [10,<u><strong>60</strong></u>,10].
The score is max(sum(nums1), sum(nums2)) = max(210, 80) = 210.</pre>

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

<pre>
<strong>Input:</strong> nums1 = [20,40,20,70,30], nums2 = [50,20,50,40,20]
<strong>Output:</strong> 220
<strong>Explanation:</strong> Choosing left = 3, right = 4, we have nums1 = [20,40,20,<u><strong>40,20</strong></u>] and nums2 = [50,20,50,<u><strong>70,30</strong></u>].
The score is max(sum(nums1), sum(nums2)) = max(140, 220) = 220.
</pre>

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

<pre>
<strong>Input:</strong> nums1 = [7,11,13], nums2 = [1,1,1]
<strong>Output:</strong> 31
<strong>Explanation:</strong> We choose not to swap any subarray.
The score is max(sum(nums1), sum(nums2)) = max(31, 3) = 31.
</pre>

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

<ul>
	<li><code>n == nums1.length == nums2.length</code></li>
	<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums1[i], nums2[i] &lt;= 10<sup>4</sup></code></li>
</ul>

class Solution:
    def maximumsSplicedArray(self, nums1: List[int], nums2: List[int]) -> int:
        # 0 indexed integer arrays nums1 and nums2 of length n 
        # choose left and right where 0 <= left <= right < n 
        # swap subarray nums1[left : right + 1] with nums2[left : right + 1] 
        # can do this once or not do this 
        # score of the array is max of sum of nums1 and nums2, where sum(arr) is sum of all elements in array 
        # return maximum possible score 
        profits_21 = [nums2[i] - nums1[i] for i in range(len(nums1))]
        profits_12 = [nums1[i] - nums2[i] for i in range(len(nums1))]
        # print(profits_21) 
        # print(profits_12) 
        # best subarray of profits gives us the best indices of swapping. Might profit in either direction so see both sides
        # then we can do the swap at the indices indicated and calculate the max of the two 
        # best subarray of profits can be calculated using kadane's algorithm 
        max_so_far_21 = -1 
        max_so_far_12 = -1 
        max_ending_here = 0 
        left = 0 
        start = 0 
        end = 0 
        profitable_swap_found_21 = False 
        profitable_swap_found_12 = False 
        # consider for profits from 2 to 1 
        for right in range(len(profits_21)) : 
            max_ending_here += profits_21[right] 
            if max_so_far_21 < max_ending_here : 
                max_so_far_21 = max_ending_here 
                profitable_swap_found_21 = max_so_far_21 > 0 
                start = left 
                end = right + 1
            if max_ending_here < 0 : 
                max_ending_here = 0 
                left = right + 1 
        start_12 = 0 
        end_12 = 0 
        left = 0 
        max_ending_here = 0 
        # consider for profits from 1 to 2 
        for right in range(len(profits_12)) : 
            max_ending_here += profits_12[right] 
            if max_so_far_12 < max_ending_here : 
                max_so_far_12 = max_ending_here 
                profitable_swap_found_12 = max_so_far_12 > 0 
                start_12 = left 
                end_12 = right + 1 
            if max_ending_here < 0 : 
                max_ending_here = 0 
                left = right + 1 
        # if no profitable swap was ever found, return max of the sums 
        if profitable_swap_found_21 == profitable_swap_found_12 == False : 
            return max(sum(nums1), sum(nums2)) 
        else : 
            # print(f'After kadanes algorithm, start and end are {start}, {end} with max so far as {max_so_far_21} for profits 21')
            # print(f'After kadanes algorithm, start and end are {start_12}, {end_12} with max so far as {max_so_far_12} for profits 12')
            # otherwise, find the new nums forms associated and maximize and return the result 
            new_nums1 = nums1[:start] + nums2[start : end] + nums1[end : ]
            new_nums2 = nums2[:start] + nums1[start : end] + nums2[end : ]
            new_nums1_12 = nums1[:start_12] + nums2[start_12 : end_12] + nums1[end_12 : ] 
            new_nums2_12 = nums2[:start_12] + nums1[start_12 : end_12] + nums2[end_12 : ]
            # print(new_nums1)
            # print(new_nums2)
            # print(new_nums1_12)
            # print(new_nums2_12)
            return max(sum(new_nums1), sum(new_nums2), sum(new_nums1_12), sum(new_nums2_12))
663