Delete and earn

Write codeMedium20 min

Your task

You are given an integer array nums. In one operation you pick any value x present in the array, gain x points, and must then delete every occurrence of x as well as every element equal to x-1 and x+1. You repeat until the array is empty. Return the maximum total points you can earn. Values satisfy 1 <= nums[i] <= 10^4 and the array may be empty.

Examples

delete_and_earn(nums: list[int]) -> int
Example 1
Inputdelete_and_earn([3, 4, 2])
Output6