fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int a[30003]={0};
  4. int dp[30005][350];
  5. int n,d;
  6. int func(int curr,int l)
  7. {
  8. if(dp[curr][(abs(l-d))]!=-1)
  9. {
  10. return dp[curr][(abs(l-d))];
  11. }
  12. int tmp=0,tmp1=0,tmp2=0,tmp3=0;
  13. if(curr+l-1<30001 && l>1)
  14. tmp=func(curr+l-1,l-1);
  15. if(curr+l<30001)
  16. tmp1=func(curr+l,l);
  17. if(curr+l+1<30001)
  18. tmp2=func(curr+l+1,l+1);
  19. tmp3=max(max(tmp1,tmp2),tmp);
  20. return dp[curr][(abs(l-d))]=tmp3+a[curr];
  21. }
  22. int main()
  23. {
  24. int tmp;
  25. scanf("%d%d",&n,&d);
  26. for(int i=0;i<n;i++)
  27. {
  28. scanf("%d",&tmp);
  29. //maxi=max(tmp,maxi);
  30. a[tmp]++;
  31. }
  32. //cout<<a[55]<<a[67]<<a[78];
  33. memset(dp,-1,sizeof(dp));
  34. int ans=func(d,d)+a[0];
  35. cout<<ans;
  36. return 0;
  37. }
Runtime error #stdin #stdout 0.05s 52344KB
stdin
Standard input is empty
stdout
Standard output is empty