C++ dp hashtable
\nclass Solution {\npublic:\n    vector<int> ht;\n    vector<vector<int>> dp;\n    vector<int> z;\n    int maxSelectedElements(vector<int>& nums) {\n        z =
```\nclass Solution {\npublic:\n    vector<int> ht;\n    vector<vector<int>> dp;\n    vector<int> z;\n    int maxSelectedElements(vector<int>& nums) {\n        z = nums;\n        ht = vector<int>((*max_element(nums.begin(), nums.end())) + 3);\n        dp = vector<vector<int>>\n            (2, vector<int>((*max_element(nums.begin(), nums.end())) + 3, -1));\n        for (auto& n : nums) ++ht[n];\n        int ans = 0, i = 1;\n        for (; i < ht.size(); ++i)\n            ans = max(ans, dfs(i, ht[i - 1] > 0));\n        return ans;\n    }\n    int dfs(int i, bool extra){\n        if (i == ht.size()) return 0;\n        if (dp[extra][i] != -1) return dp[extra][i];\n        dp[extra][i] = 0;\n        if (ht[i] > 1){\n            dp[extra][i] = 1 + dfs(i + 1, true);\n        }\n        else if (ht[i] == 1){\n            dp[extra][i] = 1 + dfs(i + 1, extra);\n        }\n        else{\n            if (extra)\n                dp[extra][i] = 1 + dfs(i + 1, false);\n        }\n        return dp[extra][i];\n    }\n};\n```
[]