Neighbouring Enemy

please explain some test cases and some hint …

Pick 2, remove all 1’s and 3’s. Add 2 to your answer.
Pick 2 again, remove all 1’s and 3’s(Though there are no 1’s and 3’s left now). Add 2 to your answer.
Same again 3 times more.
Get answer 10.

Therefore you can do count[ar[i]]*ar[i] , delete all ar[i]-1 and ar[i]+1 and check for further answer. Build a DP solution.