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

#PS0000191. Kefa và nhóm bạn (Kefa and Company)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Kefa và nhóm bạn (Kefa and Company)

Kefa and Company

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

Kefa plans a celebration with friends. Friend ii has money mim_i and friendship value sis_i. Select a group in which no two friends differ in money by dd or more. Kefa’s own money is irrelevant. Maximize the sum of friendship values.

Input

The first line contains n,dn,d. Each of the next nn lines contains integers mi,sim_i,s_i.

Output

Print the maximum total friendship value.

Subtasks

  • Subtask 1 (20 points): 1≤n≤5001\le n\le 500; 1≤d≤1091\le d\le10^9; 0≤mi,si≤1090\le m_i,s_i\le10^9.
  • Subtask 2 (30 points): 1≤n≤1000001\le n\le 100000; 1≤d≤1091\le d\le10^9; 0≤mi,si≤1090\le m_i,s_i\le10^9; mi=m1m_i=m_1 for every ii.
  • Subtask 3 (50 points): 1≤n≤1000001\le n\le 100000; 1≤d≤1091\le d\le10^9; 0≤mi,si≤1090\le m_i,s_i\le10^9.

Example 1

Input

4 5
75 5
0 100
150 20
75 1

Output

100

Explanation

Select the second friend alone; the group with money 75 has value 6.

Example 2

Input

5 100
0 7
11 32
99 10
46 8
87 54

Output

111

Explanation

The largest difference is 99, below 100, so all five can be selected.