<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(reverse=True)
        # stack = [nums[0]]
        # counts = {}
        # for n in nums:
        #     count[n] = counts.get(n, 0) + 1
        
        # # for n in reversed(nums):
        # #     stack.append(n)
        # nums_dist = sorted(list(set(nums)), reverse=True)
        # i = 0
        # res = 0
        # while stack:
        #     curr = stack.pop()
        #     while i < len(nums_dist) and (nums_dist[i] - 1) * counts.get(nums_dist[i] - 1, 0) > nums_dist[i] * counts[nums_dist[i]]:
        #         stack.append(nums_dist[i])

        #         i += 1
        #     counts[]
        #     res += nums_dist[]
        #     stack.append(nums_dist[i])


        counts = {}
        max_el = float('-inf')
        for n in nums:
            counts[n] = counts.get(n, 0) + 1
            max_el = max(n, max_el)
        # nums_dist = sorted(list(set(nums)), reverse=True)

        dp = [None for i in range(max_el + 1)]
        dp[0] = 0
        dp[1] = counts.get(1, 0)
        for i in range(2, max_el + 1):
            dp[i] = max(dp[i - 1], dp[i - 2] + counts.get(i, 0) * i)
        return dp[max_el]

        # memo = [None for i in range(max_el + 1)]
        # def recurse(n):
        #     if memo[n] is not None:
        #         return memo[n]
        #     elif n == 0:
        #         memo[n] = 0
        #     elif n == 1:
        #         memo[n] = counts.get(n, 0)
        #     else:
        #         memo[n] = max(recurse(n - 1), recurse(n - 2) + counts.get(n, 0) * n)
        #     return memo[n]
        # return recurse(max_el)
18101