How to come up with the recurrence relation for this problem?

The recurrence relation is quite simple. Here is my code for the same.

    #include <bits/stdc++.h>
    #define int long long 
    using namespace std;
    int32_t main() 
   {
        int a, b, arr[100001]={0}, ans=0, dp[100001]={0}; 
        cin >> a;
        for(int i = 0 ; i < a ; i++)
           cin >> b , arr[b]++;
        dp[1] = arr[1] , ans = arr[1];
        for(int i = 2 ; i <= 100000 ; i++)
            dp[i] = max(dp[i-1], (arr[i]*i)+dp[i-2]) , ans=max(ans,dp[i]);
        cout << ans;
    }