fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. long long dp[500005][2], n, a[500005], cnt = 1;
  5.  
  6. int main()
  7. {
  8. cin >> n;
  9. for (long long i = 1; i <= n; i++)
  10. {
  11. cin >> a[i];
  12. }
  13.  
  14. dp[a[1]][0] = 1;
  15.  
  16. for (long long i = 2; i <= n; i++)
  17. {
  18.  
  19. dp[a[i]][1] = max({dp[a[i]][1], dp[a[i] - 1][1] + 1,dp[a[i] - 2][0] + 2});
  20. dp[a[i]][0] = max({dp[a[i]][0], dp[a[i] - 1][0] + 1,1LL});
  21.  
  22. cnt = max({cnt, dp[a[i]][0], dp[a[i]][1]});
  23. }
  24.  
  25. cout << cnt;
  26. }
Success #stdin #stdout 0s 5324KB
stdin
Standard input is empty
stdout
1