<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()
        count = Counter(nums)
        num = list(set(nums))
        dp = {}
        def dfs(i):
            if i in dp:return dp[i]
            if i>=len(num):
                return 0
            res =0 
            E =dfs(i+1)
            I = 0
            if i+1<len(num) and num[i]+1 == num[i+1]:
                I = dfs(i+2) + count[num[i]]*num[i]
            else:
                I = dfs(i+1) + count[num[i]]*num[i]
            res = max(I,E)
            dp[i] = res
            return res
        return dfs(0)
















        
        nums.sort()
        count = Counter(nums)
        num = list(set(nums))
        dp = {}
        def dfs(i):
            if i in dp:return dp[i]
            if i>=len(num):
                return 0
            E = dfs(i+1)
            I = 0
            if i+1<len(num) and num[i]+1 == num[i+1]:
                I = dfs(i+2) + num[i]*count[num[i]]
            else:
                I = dfs(i+1)+num[i]*count[num[i]]
            res = max(I,E)
            dp[i] = res
            return res
        return dfs(0)















        
        nums.sort()
        num = list(set(nums))
        count = Counter(nums)
        dp = {}
        def dfs(i):
            if  i in dp:return dp[i]
            if i>=len(num):return 0
            E = dfs(i+1)
            I = 0
            if i+1<len(num) and num[i]+1 == num[i+1]:
                I = dfs(i+2) + num[i]*count[num[i]]
            else:
                I = dfs(i+1) + num[i]*count[num[i]]
            res = max(I, E)
            dp[i] = res
            return res
        return dfs(0)












        nums.sort()
        num = list(set(nums))
        count = Counter(nums)
        dp = {}
        def dfs(i):
            if i in dp:return dp[i]
            if i>=len(num):
                return 0
            E = dfs(i+1)
            I = 0
            if i+1<len(num) and num[i]+1 == num[i+1]:
                I = dfs(i+2) + count[num[i]]*num[i]
            else:
                I = dfs(i+1) + count[num[i]]*num[i]
            res = max(I,E)
            dp[i] = res
            return res
        return dfs(0)
            
















        nums.sort()
        num = list(set(nums))
        count = Counter(nums)
        dp = {}
        def dfs(i):
            if  i in dp:return dp[i]
            if i>=len(num):return 0
            E = dfs(i+1)
            I = 0
            if i+1<len(num) and num[i]+1 == num[i+1]:
                I = dfs(i+2) + num[i]*count[num[i]]
            else:
                I = dfs(i+1) + num[i]*count[num[i]]
            res = max(I, E)
            dp[i] = res
            return res
        return dfs(0)
            













        nums.sort()
        num = list(set(nums))
        count = Counter(nums)
        dp = {}
        def dfs(i):
            if i in dp:return dp[i]
            if i>=len(num):return 0
            E= dfs(i+1)
            I = 0
            if i+1<len(num) and num[i]+1 == num[i+1]:
                I =dfs(i+2) + num[i]*count[num[i]]
            else:
                I =dfs(i+1) + num[i]*count[num[i]]
            res = max(I,E)
            dp[i] = res
            return res
        return dfs(0)













        nums.sort()
        num = sorted(set(nums))
        counter = Counter(nums)
        dp= {}
        def dfs(i):
            if i>=len(num):
                return 0
        
            E = dfs(i+1)
            I = 0
            if i+1<len(num) and num[i]+1 == num[i+1]:
                I = dfs(i+2) + num[i]*counter[num[i]]
            else:
                I = dfs(i+1) + num[i]*counter[num[i]]
            res = max(I,E)
            return res
        return dfs(0)



















        nums.sort()
        num = list(set(nums))
        counter = Counter(nums)
        dp = {}
        def dfs(i):
            if  i in dp:return dp[i]
            if i>=len(num):
                return 0
            E = dfs(i+1)
            I = 0
            if i+1<len(num) and num[i] +1 == num[i+1]:
                I += counter[num[i]]*num[i] + dfs(i+2)
            else:  
                I+= counter[num[i]]*num[i] + dfs(i+1)
            dp[i] = max(I,E)
            return dp[i]
        return dfs(0)


















        nums.sort()
        num = list(set(nums))
        counter = Counter(nums)
        def dfs(i):
            if i>=len(num):
                return 0
            E = dfs(i+1)
            I = 0
            if i+1<len(num) and num[i]+1 == num[i+1]:
                I = num[i]*counter[num[i]] + dfs(i+2)
            else:
                I = num[i]*counter[num[i]] + dfs(i+1)
            res = max(I,E)
            return res
        return dfs(0)
















        nums.sort()
        counter = Counter(nums)
        num = list(set(nums))
        dp = {}
        def dfs(i):
            if i>=len(num):return 0
            E = dfs(i+1)
            I = 0
            if i+1<len(num) and num[i]+1 == num[i+1]:

                I = dfs(i+2) + num[i]*counter[num[i]]
            else:
                I = dfs(i+1) + num[i]*counter[num[i]]
            res = max(I,E)
            return res
        return dfs(0)























        nums.sort()
        count = Counter(nums)
        num = list(set(nums))
        dp = {}
        def dfs(i):
            if i in dp:return dp[i]
            if i>=len(num):return 0
            E = dfs(i+1)
            I = 0 
            if i+1<len(num) and num[i]+1 == num[i+1]:
                I = num[i]*count[num[i]] + dfs(i+2)
            else:
                I = num[i]*count[num[i]] + dfs(i+1)
            res = max(I,E)
            dp[i] = res
            return res
        return dfs(0)





















        nums.sort()
        counter = Counter(nums)
        num=list(set(nums))
        earn2 = 0
        earn1 = 0
        for i in range(len(num)):
            E = earn1
            I = 0
            if i-1>=0 and num[i] == num[i-1]+1:
                I = earn2 + num[i]*counter[num[i]]
            else:
                I = earn1 + num[i]*counter[num[i]]
            res = max(I,E)
            earn2 = earn1
            earn1 = res
        return earn1



        nums.sort()
        counter = Counter(nums)
        num=list(set(nums))
        dp = [0]*len(num)
        for i in range(len(num)):
            E = dp[i-1]
            I = 0
            if i-1>=0 and num[i] == num[i-1]+1:
                I = dp[i-2] + num[i]*counter[num[i]]
            else:
                I = dp[i-1] + num[i]*counter[num[i]]
            dp[i] = max(I,E)
        return max(dp)




        nums.sort()
        counter = Counter(nums)
        num=list(set(nums))
        dp = {}
        def dfs(i):
            if i in dp:return dp[i]
            if i>=len(num):return 0
            E = dfs(i+1)
            I = 0
            if i+1<len(num) and num[i] == num[i+1]-1:
                I = dfs(i+2) + num[i]*counter[num[i]]
            else:
                I = dfs(i+1) + num[i]*counter[num[i]]
            res = max(I,E)
            dp[i] = res
            return res
        return dfs(0)


        






















        count = Counter(nums)
        num = list(set(sorted(nums)))
        dp = {}
        def dfs(i):
            if i in dp:return dp[i]
            if i>=len(num):return 0
            E = dfs(i+1)
            I = 0
            if i+1<len(num) and num[i+1] == num[i]+1:
                I = dfs(i+2) + num[i]*count[num[i]]
            else:
                I = dfs(i+1) + num[i]*count[num[i]]
            dp[i] = max(I,E)
            return dp[i]
        return dfs(0)


















        count = Counter(nums)
        num = list(set(sorted(nums)))
        earn2 = 0
        earn1 = 0
        for i in range(len(num)):
            s = num[i]*count[num[i]]
            if i-1>=0 and num[i]==num[i-1]+1:
                tmp = earn2
                earn2 = max(earn1 + s,earn2)
                earn1  = tmp
            else:
                tmp = earn2
                earn2 = earn2 + s
                earn1 = tmp
        return earn2

        count = Counter(nums)
        num = list(set(sorted(nums)))
        dp  = [0]*(len(num))
        for i in range(len(num)):
            E = dp[i-1]
            I = 0
            if  i-1>=0 and num[i] == num[i-1]+1:
                I = dp[i-2] + num[i]*count[num[i]]
            else:
                I = dp[i-1] + num[i]*count[num[i]]
            dp[i] = max(I,E)
        return dp[-1]
        
            













        count = Counter(nums)
        num = list(set(sorted(nums)))
        dp = {}
        
        def dfs(i):
            if  i in dp:return dp[i]
            if i>=len(num):return 0
            E = dfs(i+1)
            I = 0
            if i+1<len(num) and num[i]+1 == num[i+1]:
                I = dfs(i+2) + num[i]*count[num[i]]
            else:
                I = dfs(i+1) + num[i]*count[num[i]]
            dp[i] = max(I,E)
            return dp[i]
        return dfs(0)






















        count = Counter(nums)
        num = list(set(sorted(nums)))
        earn2 = 0
        earn1 = 0
        for i in range(len(num)):
            cur = count[num[i]]*num[i]
            if i>0 and num[i]==num[i-1]+1:
                tmp = earn2
                earn2 = max(earn2,cur + earn1)
                earn1 = tmp
            else:
                tmp = earn2
                earn2 = cur + earn2
                earn1 = tmp
        return earn2


        count = Counter(nums)
        num = list(set(sorted(nums)))
        dp = [0]*len(num)
        for i in range(len(num)):
            if i>0 and num[i]==num[i-1]+1:
                dp[i] = max(dp[i-1],count[num[i]]*num[i] + dp[i-2])
            else:
                dp[i] = count[num[i]]*num[i] + dp[i-1]
            
        return dp[-1]
            










        count = Counter(nums)
        num = list(set(sorted(nums)))
        memo = {}
        def dfs(i):
            if i in memo:return memo[i]
            if i>=len(num):return 0
            E = dfs(i+1)
            I = 0
            if i<len(num)-1 and num[i]+1 == num[i+1]:
                I = dfs(i+2) + num[i]*count[num[i]]
            else:
                I = dfs(i+1) + num[i]*count[num[i]]
            memo[i] = max(I,E)
            return memo[i]
        return dfs(0)






















        
        count = Counter(nums)
        earn1,earn2 = 0,0
        nums= sorted(list(set(nums)))
        for i in range(len(nums)):
            curMax = nums[i]*count[nums[i]]
            if i>0 and nums[i] == nums[i-1]+1:
                tmp = earn2
                earn2 = max(curMax+earn1,earn2)
                earn1 = tmp
            else:
                tmp = earn2
                earn2 = curMax+earn2
                earn1 = tmp
        return earn2

























        count = Counter(nums)
        earn1,earn2 = 0,0
        nums = list(sorted(set(nums)))
        for i in range(len(nums)):
            curMax = nums[i]*count[nums[i]]
            if i>0 and nums[i]==nums[i-1]+1:
                tmp = earn2
                earn2 = max(curMax+earn1,earn2)
                earn1 = tmp
            else:
                tmp = earn2
                earn2 = curMax+earn2
                earn1 = tmp



        return earn2
        count = Counter(nums)
        earn1,earn2 = 0,0
        nums = list(sorted(set(nums)))
        for i in range(len(nums)):
            curMax = nums[i] *count[nums[i]]

            if i>0 and nums[i]==nums[i-1]+1:
                tmp = earn2
                earn2 = max(curMax+earn1,earn2)
                earn1 = tmp
            else:
                tmp = earn2
                earn2 = curMax+earn2
                earn1 = tmp
        return earn2
        
        


















        
            
        


22053