<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:
        max_op = 0
        n = len(nums)
        memo = [[0]*n for _ in range(n)]
        def compute_score(arr, i, j, target_score, iter, memo):

            if len(arr)<=2:
                if len(arr)==2 and sum(arr)==target_score:
                    return iter + 1
                return iter

            a1, a2, a3 = iter, iter, iter
            # delete first two
            s1 = arr[0]+ arr[1]
            
            if s1 == target_score:
                if memo[i+2][j] !=0:
                    a1 = memo[i+2][j]
                else:
                    a1 = compute_score(arr[2::], i+2, j, target_score, iter+1, memo)
                    memo[i+2][j] = a1


            #delete last two
            s2 = arr[-1] + arr[-2]
            if s2 == target_score:
                if memo[i][j-2] !=0:
                    a2 = memo[i][j-2]
                else:
                    a2 = compute_score(arr[0:-2], i, j-2, target_score, iter+1, memo)
                    memo[i][j-2] = a2

            #delete first and last
            s3 = arr[0] + arr[-1]
            if s3 == target_score:
                if memo[i+1][j-1] !=0:
                    a3 = memo[i+1][j-1]
                else:
                    a3 = compute_score(arr[1:-1], i+1, j-1, target_score, iter+1, memo)
                    memo[i+1][j-1] = a3
            return max(a1, a2, a3)         
        

        if len(nums)==2:
            return 1
        
        s1 = nums[0]+nums[1]
        a1 = compute_score(nums[2::], 2, n-1, s1, 1, memo)

        s2 = nums[-2]+nums[-1]
        a2 = compute_score(nums[0:-2],0, n-3, s2, 1, memo)

        s3 = nums[0]+nums[-1]
        a3 = compute_score(nums[1:-1],1, n-2, s3, 1, memo)

        return max(a1, a2, a3)


        
54768