Paul the barista picks coffee beans

Pick the longest subsequence of the given row so that consecutive picked values are congruent mod k or differ by at most d in absolute value.

Medium7Dynamic programmingSegment treeArrayPrefix sumNo attempts yetTime limit1.5sMemory limit64 MB

Problem

Paul the barista has NN coffee beans laid out in a row. He picks some of them and brews them. The picked beans keep the order they were laid out in.

Each bean carries one integer that names its kind. Write the kinds of the picked beans in order as a sequence AA. The extraction has good quality when at least one of the two conditions below holds for every ii with i2i \ge 2.

  • Ai1Ai(modk)A_{i-1} \equiv A_i \pmod k
  • Ai1dAiAi1+dA_{i-1} - d \le A_i \le A_{i-1} + d

Find the largest number of beans Paul can pick while the quality stays good.

Input

The first line contains NN, kk, and dd, separated by spaces. (1N5×1051 \le N \le 5 \times 10^5, 1k,d5×1051 \le k, d \le 5 \times 10^5)

The second line contains a sequence S1,S2,,SNS_1, S_2, \dots, S_N of length NN giving the kind of each bean in the order the beans are laid out. (1Si5×1051 \le S_i \le 5 \times 10^5)

Output

Print the largest number of beans in a good extraction.