Given in problem that if we choose N then N-1 and N+1 should be deleted. In other way it means if we want to pick i , then maximum sum till 'i' will be maximum of('sum till i-2 + frequency of ith element * i' ,'sum till i-1'). This gives easy DP expression as

**'dp[i] = max(dp[i-1],dp[i-2]+(cnt[i] i))'*

where array 'cnt[]' holds count of 'i' in array 'nums' . i.e 'i' will contribute 'i*cnt[i]' in total sum;

and dp[i] holds maximum sum till i

```
class Solution {
public:
int deleteAndEarn(vector<int>& nums) {
vector<int> cnt(10009,0);
int n = nums.size();
for(int i=0;i<n;i++)cnt[nums[i]]++;// store the frequency of i'th element of nums in array cnt
vector<int> dp(10009,0);
dp[1] = cnt[1]*1;// as maximum sum till 1 will be cnt[1] i.e frequency of 1 in nums[]
for(int i=2;i<=10000;i++)
{
dp[i]=max(dp[i-1],dp[i-2]+(cnt[i]*i));//maximum sum till i'th element according to above dp expression
}
return dp[10000];
}
};
```