fork(35) download
  1. #include <cstdio>
  2. #include <algorithm>
  3.  
  4. using namespace std;
  5.  
  6. typedef long long ll;
  7.  
  8. const int M = 30001;
  9.  
  10. int dp[M][250*2];
  11. bool used[M][250*2] = {};
  12.  
  13. int d;
  14. int gem[M] = {};
  15.  
  16. int solve(int i, int j) {
  17. int jj = j-(d-250);
  18. if (i >= M) return 0;
  19. if (used[i][jj]) return dp[i][jj];
  20. used[i][jj] = true;
  21. int res;
  22. if (j == 1) {
  23. res = gem[i] + max(solve(i+j, j), solve(i+j+1, j+1));
  24. } else {
  25. res = gem[i] + max(max(solve(i+j-1, j-1), solve(i+j, j)), solve(i+j+1, j+1));
  26. }
  27. dp[i][jj] = res;
  28. return res;
  29. }
  30.  
  31. int main() {
  32. int n;
  33. scanf("%d %d", &n, &d);
  34. for (int i = 0; i < n; i++) {
  35. int x;
  36. scanf("%d", &x);
  37. gem[x]++;
  38. }
  39. printf("%d\n", solve(d, d));
  40. return 0;
  41. }
Runtime error #stdin #stdout 0s 76480KB
stdin
Standard input is empty
stdout
Standard output is empty