After that, he will continue jumping according to the following rule. Let l be the length of the previous jump, that is, if his previous jump was from island prev to island cur, let l?=?cur?-?prev. He will perform a jump of length l?-?1, l or l?+?1 to the east. That is, he will jump to island (cur?+?l?-?1), (cur?+?l) or (cur?+?l?+?1) (if they exist). The length of a jump must be positive, that is, he cannot perform a jump of length 0 when l?=?1. If there is no valid destination, he will stop jumping. Mr. Kitayuta will collect the gems on the islands visited during the process. Find the maximum number of gems that he can collect.
Input The first line of the input contains two space-separated integers n and d (1?¡Ü?n,?d?¡Ü?30000), denoting the number of the gems in the Shuseki Islands and the length of the Mr. Kitayuta's first jump, respectively.
The next n lines describe the location of the gems. The i-th of them (1?¡Ü?i?¡Ü?n) contains a integer pi (d?¡Ü?p1?¡Ü?p2?¡Ü?...?¡Ü?pn?¡Ü?30000), denoting the number of the island that contains the i-th gem.
Output Print the maximum number of gems that Mr. Kitayuta can collect.
Sample test(s) input 4 10
10
21
27
27
output 3
input 8 8
9
19
28
36
45
55
66
78
output 6
input 13 7
8
8
9
16
17
17
18
21
23
24
24
26
30
output 4
Note In the first sample, the optimal route is 0 ?¡ú? 10 (+1 gem) ?¡ú? 19 ?¡ú? 27 (+2 gems) ?¡ú?...
In the second sample, the optimal route is 0 ?¡ú? 8 ?¡ú? 15 ?¡ú? 21?¡ú? 28 (+1 gem) ?¡ú? 36 (+1 gem) ?¡ú? 45 (+1 gem) ?¡ú? 55 (+1 gem) ?¡ú? 66 (+1 gem) ?¡ú? 78 (+1 gem) ?¡ú?...
In the third sample, the optimal route is 0 ?¡ú? 7 ?¡ú? 13 ?¡ú? 18 (+1 gem) ?¡ú? 24 (+2 gems) ?¡ú? 30 (+1 gem) ?¡ú?...
ÕâÌâÖ±½ÓdpºÜ¼òµ¥£¬µ«ÊÇdµÄ·¶Î§ºÜ´ó µ«ÊÇ¿ÉÒÔÖ¤Ã÷×îÖÕÓõ½µÄdµÄȡֵ×î¶à²»³¬¹ý500
½ØÈ¡Ò»¶Îexplanation:
Below is the explanation from yosupo, translated by me.
[From here]
Let m be the number of the islands (that is, 30001). First, let us describe a solution with time and memory complexity of O(m2).
We will apply Dynamic Programming. let dp[i][j] be the number of the gems that Mr. Kitayuta can collect after he jumps to island i, when the length of his previous jump is j (let us assume that he have not collect the gems on island i). Then, you can calculate the values of the table dp by the following:
- dp[i][j]?=?0, if i?¡Ý?m
(actually these islands do not exist, but we can suppose that they exist and when Mr. Kitayuta jumps to these islands, he stops jumping)
- dp[i][j]?=? (the number of the gems on island i) ?+?max(dp[i?+?j][j],?dp[i?+?j?+?1][j?+?1]), if i?
m,?j?=?1
(he cannot perform a jump of length 0)
- dp[i][j]?=? (the number of the gems on island i) ?+?max(dp[i?+?j?-?1][j?-?1],?dp[i?+?j][j],?dp[i?+?j?+?1][j?+?1]), if i?
m,?j?¡Ý?2
This solution is unfeasible in terms of both time and memory. However, the following observation makes it an Accepted solution: there are only 491 values of j that we have to consider, which are d?-?245,?d?-?244,?d?-?243,?...,?d?+?244 and d?+?245.
Why? First, let us