This page is still under construction.

Parts of this page are still being built. What you see may change.

Paul the barista picks coffee beans

Time limit1.5sMemory limit64 MB

Summary
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.
Level

Medium7 of 10

Topics
Dynamic programming, Segment tree, Array, Prefix sum
Solved
No attempts yet

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 i≥2i \ge 2.

  • Ai−1≡Ai(modk)A_{i-1} \equiv A_i \pmod k
  • Ai−1−d≤Ai≤Ai−1+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. (1≤N≤5×1051 \le N \le 5 \times 10^5, 1≤k,d≤5×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. (1≤Si≤5×1051 \le S_i \le 5 \times 10^5)

Output

Print the largest number of beans in a good extraction.

Examples3

  1. Example 1

    Input
    9 7 2
    1 5 12 10 8 6 4 4 3
    
    Expected output
    8
    
  2. Example 2

    Input
    1 3 1
    7
    
    Expected output
    1
    
  3. Example 3

    Input
    5 1 1
    1 100 2 500000 3
    
    Expected output
    5