<p>You are given an integer array <code>nums</code>. You want to maximize the number of points you get by performing the following operation any number of times:</p>

<ul>
	<li>Pick any <code>nums[i]</code> and delete it to earn <code>nums[i]</code> points. Afterwards, you must delete <b>every</b> element equal to <code>nums[i] - 1</code> and <strong>every</strong> element equal to <code>nums[i] + 1</code>.</li>
</ul>

<p>Return <em>the <strong>maximum number of points</strong> you can earn by applying the above operation some number of times</em>.</p>

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

<pre>
<strong>Input:</strong> nums = [3,4,2]
<strong>Output:</strong> 6
<strong>Explanation:</strong> You can perform the following operations:
- Delete 4 to earn 4 points. Consequently, 3 is also deleted. nums = [2].
- Delete 2 to earn 2 points. nums = [].
You earn a total of 6 points.
</pre>

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

<pre>
<strong>Input:</strong> nums = [2,2,3,3,3,4]
<strong>Output:</strong> 9
<strong>Explanation:</strong> You can perform the following operations:
- Delete a 3 to earn 3 points. All 2&#39;s and 4&#39;s are also deleted. nums = [3,3].
- Delete a 3 again to earn 3 points. nums = [3].
- Delete a 3 once more to earn 3 points. nums = [].
You earn a total of 9 points.</pre>

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

<ul>
	<li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>4</sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
</ul>

class Solution:
    def deleteAndEarn(self, nums: List[int]) -> int:
        nums.sort()
        N = len(nums)
        
        # the idea is that we just brute force the take / no take situation
        # if we take a current element, we will take all of it
        # consider the example nums = [x, x, x + 1, x + 1, x + 2]
        # if we take choose to take the number x, we will lose x + 1, the next element we are considering is x + 2, taking x + 2 won't affect taking x (since taking x + 2 means we can't take x + 1 and x + 3, so we might as well take all the x that we can)
        # and if we do take a current element, we will consider from the next bigger element y that is atleast 2 bigger than the current element, i.e., we will consider elements that are y >= x + 2
        # or if we don't take the current element, we just take the next bigger element from x, i.e., y >= x + 1
        @cache
        def dp(i):
            if i >= N:
                return 0

            next_available_idx_after_take = i
            for j in range(i + 1, N):
                if nums[j] >= nums[i] + 2:
                    next_available_idx_after_take = j
                    break

            if next_available_idx_after_take == i:
                next_available_idx_after_take = N

            next_available_idx_without_taking = i
            for j in range(i + 1, N):
                if nums[j] >= nums[i] + 1:
                    next_available_idx_without_taking = j
                    break

            if next_available_idx_without_taking == i:
                next_available_idx_without_taking = N
            
            take = 0
            for j in range(i, N):
                if nums[j] == nums[i]:
                    take += nums[j]
                else:
                    break

            take += dp(next_available_idx_after_take)
            noTake = dp(next_available_idx_without_taking)
            return max(take, noTake)

        return dp(0)
22946