PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000156. Hai bệ đỡ (Two Platforms)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Hai bệ đỡ (Two Platforms)

Two Platforms

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn distinct points (xi,yi)(x_i,y_i) with integer coordinates. You can place two horizontal platforms, each of exactly length kk with integer endpoints. The platforms may have different heights and may overlap.

All points then fall vertically downward. A point is saved if it touches at least one platform, including its endpoints. Maximize the number of saved points by choosing the two placements.

Input

  • The first line contains n,kn,k.
  • The second line contains nn horizontal coordinates x1,…,xnx_1,\ldots,x_n.
  • The third line contains nn vertical coordinates y1,…,yny_1,\ldots,y_n. All point pairs (xi,yi)(x_i,y_i) are distinct.

Output

Print one integer: the maximum number of points saved by the two platforms.

Subtasks

  • Subtask 1 (20 points): 1≤n≤301\le n\le30; 1≤k≤10001\le k\le1000; 1≤xi,yi≤10001\le x_i,y_i\le1000; all (xi,yi)(x_i,y_i) are distinct.
  • Subtask 2 (30 points): 1≤n≤40001\le n\le4000; 1≤k≤1061\le k\le10^6; 1≤xi,yi≤1061\le x_i,y_i\le10^6; all (xi,yi)(x_i,y_i) are distinct.
  • Subtask 3 (50 points): 1≤n≤2⋅1051\le n\le2\cdot10^5; 1≤k≤1091\le k\le10^9; 1≤xi,yi≤1091\le x_i,y_i\le10^9; all (xi,yi)(x_i,y_i) are distinct.

Examples

Example 1

Input

7 1
1 5 2 3 1 5 4
1 3 6 7 2 5 4

Output

6

Explanation

The seven horizontal coordinates are 1,5,2,3,1,5,41,5,2,3,1,5,4. A platform spanning [1,2][1,2] saves the three points with horizontal coordinates 1,1,21,1,2. A second spanning [4,5][4,5] saves three points with coordinates 4,5,54,5,5. Only the point at 33 is lost, for a total of 66. Two length-11 intervals cannot cover all five distinct integer x-coordinates 1,2,3,4,51,2,3,4,5, so 77 is impossible.