Under construction

Still being built. Things may change

Team Formation

Time limit1sMemory limit1024 MB

Summary
Given N members with skill and target rank, pick disjoint triples whose rank spread is at most K to maximize the total skill of the chosen triples.
Level

Medium6 of 10

Topics
Sorting, Dynamic programming, Sliding window
Solved
No attempts yet

Problem

DoHun posted a notice to encourage DPS members to join ICPC and wants to group the members who do not have a team yet into teams of 3. There are NN members numbered 1 to NN. Member ii has programming skill AiA_i and target rank BiB_i.

To get the best results, DoHun forms teams that satisfy the following conditions.

  • Each team must have exactly 3 members.
  • A member cannot belong to more than one team.
  • In a team, the difference between the highest target rank and the lowest target rank among its three members must be at most KK.
  • Not every member has to be on a team.

The team skill of a team is the sum of the programming skills of its three members. DoHun wants the total team skill to be as large as possible. Find the maximum possible total team skill when the teams are formed under these conditions.

Input

The first line contains the number of members NN and KK, separated by a space. (3≤N≤2000003 \le N \le 200000, 0≤K≤1090 \le K \le 10^9)

The next NN lines each contain the programming skill AiA_i and the target rank BiB_i of member ii, separated by a space. (1≤Ai,Bi≤1091 \le A_i, B_i \le 10^9)

Output

Print the maximum total team skill when the teams are formed under the conditions.

Examples2

  1. Example 1

    Input
    4 4
    10 10
    20 16
    16 11
    13 13
    
    Expected output
    39
    
  2. Example 2

    Input
    6 10
    1 1
    2 1
    3 1
    4 1
    5 1
    6 1
    
    Expected output
    21