题意:

%EF%BC%8C%E9%97%AE%E6%9C%80%E5%A4%9A%E5%BE%97%E5%88%B0%E7%9A%84%E5%AE%9D%E8%97%8F%E6%95%B0%E9%87%8F&preview=true)
思路:
%E8%A1%A8%E7%A4%BA%E7%9B%AE%E5%89%8D%E5%9C%A8i%EF%BC%8C%E4%B8%94%E4%B8%8A%E4%B8%80%E6%AD%A5%E8%B7%B3%E4%BA%86j%E6%AD%A5%E6%9D%A5%E8%BD%AC%E7%A7%BB%E7%9A%84%E8%AF%9D%EF%BC%8C%E9%9C%80%E8%A6%8130000*30000%E7%9A%84%E5%86%85%E5%AD%98&preview=true)


%E8%A1%A8%E7%A4%BA%E7%9B%AE%E5%89%8D%E5%9C%A8i%EF%BC%8C%E4%B8%94%E4%B8%8A%E4%B8%80%E6%AD%A5%E8%B7%B3%E4%BA%86d%2Bj%E6%AD%A5%E7%9A%84%E6%9C%80%E5%A4%A7%E5%BE%97%E5%88%86&preview=true)
%20%3D%20max(f(last%2Cj%2Bk))%20%2Ba%5Bi%5D(last%E6%98%AF%E4%B8%8A%E4%B8%80%E6%AD%A5%E7%9A%84%E6%89%80%E5%9C%A8%E5%9C%B0%EF%BC%8Cd%3C%3Dlast%3Ci%2Ck%E5%8F%AF%E4%BB%A5%E6%98%AF-1%2C0%2C1)&preview=true)

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 3e4 +10;
const int inf = 0x3f3f3f3f;
int a[N],n,d,f[N][510];
int base = 250;
int main(){
scanf("%d%d",&n,&d);
for(int i = 0,x;i < n;i++){
scanf("%d",&x);
a[x]++;
}
memset(f,-inf,sizeof(f));
f[d][base] = a[d];
int ans = f[d][base];
for(int i = d;i <= 30000;i++){
for(int j = 0;j <= 500;j++){
int last = i - (d + j - base);
if(last < d || last >= i) continue;
for(int k = -1;k <= 1;k++){
if(j + k >= 0) f[i][j] = max(f[i][j],f[last][j + k] +a[i]);
}
ans = max(ans,f[i][j]);
}
}
printf("%d\n",ans);
return 0;
}