<p>Given an array of integers called <code>nums</code>, you can perform <strong>any</strong> of the following operation while <code>nums</code> contains <strong>at least</strong> <code>2</code> elements:</p>

<ul>
	<li>Choose the first two elements of <code>nums</code> and delete them.</li>
	<li>Choose the last two elements of <code>nums</code> and delete them.</li>
	<li>Choose the first and the last elements of <code>nums</code> and delete them.</li>
</ul>

<p>The<strong> score</strong> of the operation is the sum of the deleted elements.</p>

<p>Your task is to find the <strong>maximum</strong> number of operations that can be performed, such that <strong>all operations have the same score</strong>.</p>

<p>Return <em>the <strong>maximum</strong> number of operations possible that satisfy the condition mentioned above</em>.</p>

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

<pre>
<strong>Input:</strong> nums = [3,2,1,2,3,4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> We perform the following operations:
- Delete the first two elements, with score 3 + 2 = 5, nums = [1,2,3,4].
- Delete the first and the last elements, with score 1 + 4 = 5, nums = [2,3].
- Delete the first and the last elements, with score 2 + 3 = 5, nums = [].
We are unable to perform any more operations as nums is empty.
</pre>

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

<pre>
<strong>Input:</strong> nums = [3,2,6,1,4]
<strong>Output:</strong> 2
<strong>Explanation:</strong> We perform the following operations:
- Delete the first two elements, with score 3 + 2 = 5, nums = [6,1,4].
- Delete the last two elements, with score 1 + 4 = 5, nums = [6].
It can be proven that we can perform at most 2 operations.
</pre>

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

<ul>
	<li><code>2 &lt;= nums.length &lt;= 2000</code></li>
	<li><code>1 &lt;= nums[i] &lt;= 1000</code></li>
</ul>

class Solution:
    def maxOperations(self, nums: List[int]) -> int:
        if len(nums) < 2:
            return 0
        
        # n = len(nums)
        # count = {}
        # for i in range(n // 2 - 1):
        #     first_two = nums[i] + nums[i + 1]
        #     count[first_two] = count.get(first_two, 0) + 1

        #     last_two = nums[n - 1 - i] + nums[n - 1 - i - 1]
        #     count[last_two] = count.get(last_two, 0) + 1

        #     first_last = nums[i] + nums[n - 1 - i]
        #     count[first_last] = count.get(first_last, 0) + 1

        # print(count)
        # return max(count.values())

        cache = {}

        def dp(left, right, target_sum):
            # If subarray length is less than 2, no more moves can be made
            if right - left + 1 < 2:
                return 0
            
            # Check if the current state is already cached
            if (left, right) in cache:
                return cache[(left, right)]

            temp_result = 0
            
            # Option 1: Remove the first two elements
            if nums[left] + nums[left + 1] == target_sum:
                temp_result = max(temp_result, dp(left + 2, right, target_sum) + 1)
            # Option 2: Remove the first and last elements
            if nums[left] + nums[right] == target_sum:
                temp_result = max(temp_result, dp(left + 1, right - 1, target_sum) + 1)
            # Option 3: Remove the last two elements
            if nums[right - 1] + nums[right] == target_sum:
                temp_result = max(temp_result, dp(left, right - 2, target_sum) + 1)

            # Cache the result for the current subarray
            cache[(left, right)] = temp_result
            return temp_result
        
        # Try all three possible starting sums
        opt1_result = dp(2, len(nums) - 1, nums[0] + nums[1])
        opt2_result = dp(1, len(nums) - 2, nums[0] + nums[-1])
        opt3_result = dp(0, len(nums) - 3, nums[-2] + nums[-1])
    
        return max(opt1_result, opt2_result, opt3_result) + 1
        
273998