Under construction

Still being built. Things may change

Feeding Geese

Time limit5sMemory limit1024 MB

Summary
Choose which geese to feed, given each goose's waiting window and speed level, to maximize the total cuteness of the fed geese.
Level

Hard8 of 10

Topics
Dynamic programming, Segment tree, Sorting, Intervals
Solved
No attempts yet

Problem

There are NN adorable geese on the KAIST campus. To celebrate them, KAIST will host the KAIST Geese Show for students. The main part of the show is feeding the geese and enjoying their delightful reactions.

You have been chosen as the representative feeder. Your job is to feed the geese optimally so the show is as cute as possible. The ii-th goose approaches you at time TiT_i and waits for food for a duration of LL. Precisely, the ii-th goose can eat food during the interval Ti≤x≤Ti+LT_i\le x\le T_i+L. After time Ti+LT_i+L, the goose loses interest and leaves.

The ii-th goose has speed level AiA_i and cuteness CiC_i. If you throw food at any time, the fastest waiting goose (the one with the highest AiA_i) takes it. That goose then displays its cuteness, adding CiC_i to the total cuteness score of the show. After eating, the goose is satisfied and leaves. No two geese have the same speed level. Some geese are noisy, so CiC_i may be negative.

You may throw as much food as you want, with no limit on frequency or quantity. Find the maximum cuteness score the show can reach.

Input

The first line contains two space-separated integers NN and LL.

Each of the next NN lines contains three space-separated integers AiA_i, CiC_i, and TiT_i.

Output

Print the maximum cuteness score of the show.

Constraints

  • 1≤N≤300 0001\le N\le 300\,000
  • 1≤L≤1091\le L\le 10^9
  • 1≤Ai≤N (1≤i≤N)1\le A_i\le N\ (1\le i\le N)
  • Ai≠AjA_i\neq A_j if i≠j (1≤i,j≤N)i\neq j\ (1\le i,j\le N)
  • −109≤Ci≤109 (1≤i≤N)-10^9\le C_i\le 10^9\ (1\le i\le N)
  • 0≤Ti≤109 (1≤i≤N)0\le T_i\le 10^9\ (1\le i\le N)
  • All values in the input are integers.

Examples1

  1. Example 1

    Input
    6 5
    6 -1 7
    4 -5 9
    1 3 11
    5 -4 13
    2 4 14
    3 6 7
    
    Expected output
    9