O(300*n) | DP
Complexity
Time complexity: O(300*n)
Code
# Complexity
- Time complexity: O(300*n)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
class Solution {
public:
    int longestSubsequence(vector<int>& A) {
        int n=A.size(), ans=-1;
        vector<vector<int>>dp(301, vector<int>(300));
        for(int i=n-1;i>=0;i--){
            int num=A[i];
            for(int nn=1;nn<=300;nn++){
                int diff=abs(num-nn);
                dp[num][diff] = max(dp[num][diff], 1+dp[nn][diff]);
            }
            for(int j=1;j<300;j++)dp[num][j]=max(dp[num][j], dp[num][j-1]);
            ans=max(ans, dp[num][299]);
        }
        return ans;
    }
};
```
['Dynamic Programming', 'C++']