ll@N,@K,@A[N],@C[N],B[N][N][51]={},m,b;rep(i,N)B[i][i][C[i]]=1;rep(l,1,N)rep(i,N){m=i+l;rep(j,i,m)rep(c,51)rep(d,51)B[i][m%N][c]|=b=B[i][j%N][c]*B[(j+1)%N][m%N][d]*(abs(c-d)<=K),B[i][m%N][d]|=b;}wt(max[(i,j),0,N@max[c,0,51](B[i][(i+j)%N][c])](sum[k,i,i+j+1](A[k%N])));