Problem:
Score : 400 points

Problem Statement

You are given a sequence A_1, A_2, ..., A_N and an integer K.

Print the maximum possible length of a sequence B that satisfies the following conditions:

B is a (not necessarily continuous) subsequence of A.

For each pair of adjacents elements of B, the absolute difference of the elements is at most K.

Constraints

1 \leq N \leq 300,000

0 \leq A_i \leq 300,000

0 \leq K \leq 300,000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
A_1
A_2
:
A_N

Output

Print the answer.

Sample Input 1

10 3
1
5
4
3
8
6
9
7
2
4

Sample Output 1

7

For example, B = (1, 4, 3, 6, 9, 7, 4) satisfies the conditions.

It is a subsequence of A = (1, 5, 4, 3, 8, 6, 9, 7, 2, 4).

All of the absolute differences between two adjacent elements (|1-4|, |4-3|, |3-6|, |6-9|, |9-7|, |7-4|) are at most K = 3.

Solution in PyPy3 (7.3.0) (judge verdict: Runtime Error)

n,k=list(map(int,input().split()))
a=[int(input()) for i in range(n)]

#ide_ele:単位元を設定
ide_ele=-float("INF")
#segfunc:演算を定義
def segfunc(x,y):
  return max(x,y)
#num:n以上の最小の2のべき乗
#seg:セグメンテーション木
num =2**(n-1).bit_length()
seg=[ide_ele]*2*num
#init:引数を初期化する
def init(init_val):
  for i in range(n):
    seg[i+num-1]=init_val[i]
  for i in range(num-2,-1,-1):
    seg[i]=segfunc(seg[2*i+1],seg[2*i+2])
#update(a,b):init[a]の値をbに更新したときどうなるか
def update(k,x):
  k+=num-1
  seg[k]=x
  while k>0:
    k=(k-1)//2
    seg[k]=segfunc(seg[2*k+1],seg[2*k+2])
#query(p,q):[p,q)についてsegfuncしたものを返す
def query(p,q):
  if q<=p:
    return ide_ele
  p+=num-1
  q+=num-2
  res=ide_ele
  while q-p>1:
    if p%2==0:
      res=segfunc(res,seg[p])
      p=p+1
    if q%2==1:
      res=segfunc(res,seg[q])
      q=q-1
    p=(p-1)//2
    q=(q-1)//2
  if p==q:
    res=segfunc(res,seg[p])
  else:
    res=segfunc(segfunc(res,seg[p]),seg[q])
  return res

dp=[0]*(max(a)+k+1)
init(dp)
for i in range(n):
  dp[a[i]]=query(a[i]-3,a[i]+4)+1
  update(a[i],dp[a[i]])

print(max(dp))