<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 {
public:
    int dfs(vector<int>& v, unordered_map<int,int>& mp,vector<int>& dp,int i, int n){
        if(i == n) return 0;
        if(dp[i] != -1) return dp[i];
        int notpick = dfs(v,mp,dp,i+1,n);
        int pick = mp[v[i]]*v[i];
        if(i+1 < n && v[i]+1 == v[i+1])pick += dfs(v,mp,dp,i+2,n);
        else if(i+1 < n && v[i]+1 != v[i+1]) pick += dfs(v,mp,dp,i+1,n);
        return dp[i] = max(notpick,pick);
    }
    int deleteAndEarn(vector<int>& nums) {
        unordered_map<int,int>mp;
        for(auto e : nums){
            mp[e]++;
        }
        vector<int>v;
        for(auto e: mp){
            v.push_back(e.first);
        }
        sort(v.begin(),v.end());
        int n = v.size();
        vector<int>dp(n,-1);
        return dfs(v,mp,dp,0,n);
    }
};
// class Solution {
// public:
//     // Recursive function to calculate max points
//     int dfs(vector<int>& v, unordered_map<int, int>& mp, vector<int>& dp, int i, int n) {
//         if (i == n) return 0;  // Base case: no more elements to process
//         if (dp[i] != -1) return dp[i];  // If the result is already calculated, return it

//         // Option 1: Don't pick the current element, move to the next
//         int notpick = dfs(v, mp, dp, i + 1, n);
//         int pick = mp[v[i]] * v[i];  
//         if (i + 1 < n && v[i] + 1 == v[i + 1]) {  // Check if next element is consecutive
//             pick += dfs(v, mp, dp, i + 2, n);  // Skip the next element (since it's v[i] + 1)
//         } else {
//             pick += dfs(v, mp, dp, i + 1, n);  // If it's not consecutive, move to the next element
//         }

//         return dp[i] = max(notpick, pick);  // Memoize the result and return the max of the two options
//     }

//     int deleteAndEarn(vector<int>& nums) {
//         unordered_map<int, int> mp;  // Map to store the frequency of each element

//         // Step 1: Fill the frequency map
//         for (auto e : nums) {
//             mp[e]++;
//         }

//         vector<int> v;  // Vector to store unique elements from nums
//         // Step 2: Get the unique elements from the map (ignore duplicates in nums)
//         for (auto e : mp) {
//             v.push_back(e.first);
//         }

//         // Step 3: Sort the unique elements
//         sort(v.begin(), v.end());  // Sorting is crucial to ensure consecutive numbers are adjacent

//         int n = v.size();
//         vector<int> dp(n, -1);  // Initialize DP array to -1 for memoization

//         // Step 4: Call the dfs function to compute the maximum points
//         return dfs(v, mp, dp, 0, n);  // Start DFS with the sorted unique elements
//     }
// };

17550