<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:

        # lets memoizae it
        dp = [[-1]*(len(nums)+1) for _ in range(len(nums)+1)]
        # first go with recursion
        def dfs(start, end, prevscore, count):
            if start+1>=end:
                return count
            if dp[start][end]!=-1:
                return dp[start][end]
            maxOperations  = count
            if start+1< end:
                score1 = nums[start]+nums[start+1]
                if prevscore == 0 or prevscore == score1:
                    maxOperations = max(maxOperations, dfs(start+2, end, score1, count+1))
            if end-1>start:
                score2 = nums[end-1] + nums[end-2]
                if prevscore == 0 or prevscore == score2:
                    maxOperations = max(maxOperations, dfs(start, end-2, score2, count+1))
            if end>start:
                score3 = nums[start]+nums[end-1]
                if prevscore == 0 or prevscore == score3:
                    maxOperations = max(maxOperations, dfs(start+1, end-1, score3, count+1))
            dp[start][end] = maxOperations
            return dp[start][end]
        return dfs(0,len(nums),0,0)
        
#         Function maxOperationsWithSameScore(nums, start, end, previousScore, operationCount):
#     If start + 1 >= end:
#         Return operationCount

#     maxOperations = operationCount

#     // Perform operation by removing first two elements
#     If start + 1 < end:
#         score1 = nums[start] + nums[start + 1]
#         If previousScore == 0 or score1 == previousScore:
#             maxOperations = max(maxOperations, maxOperationsWithSameScore(nums, start + 2, end, score1, operationCount + 1))
    
#     // Perform operation by removing last two elements
#     If end - 1 > start:
#         score2 = nums[end - 1] + nums[end - 2]
#         If previousScore == 0 or score2 == previousScore:
#             maxOperations = max(maxOperations, maxOperationsWithSameScore(nums, start, end - 2, score2, operationCount + 1))

#     // Perform operation by removing first and last elements
#     If start < end:
#         score3 = nums[start] + nums[end - 1]
#         If previousScore == 0 or score3 == previousScore:
#             maxOperations = max(maxOperations, maxOperationsWithSameScore(nums, start + 1, end - 1, score3, operationCount + 1))

#     Return maxOperations

# Function solve(nums):
#     Return maxOperationsWithSameScore(nums, 0, len(nums), 0, 0)

89646