Under construction

Still being built. Things may change

Honest Worker

Interview

Time limit1sMemory limit1024 MB

Summary
Choose non-overlapping contractor jobs, each optionally quit early, to maximize total pay minus fake cover letter costs.
Level

Medium6 of 10

Topics
Dynamic programming, Sorting, Binary search
Solved
No attempts yet

Problem

Rafael lives in an ideal egalitarian society: all jobs offer identical payment rates, and you only have to work at a job that you find personally fulfilling. Unfortunately, even such a utopia cannot make up for the fact that Rafael is not very skilled at anything.

To compensate, Rafael turns to contractor work, where potential employers do not have the time to notice how unqualified he is for the job. Even so, since he lacks the qualifications for these contractor positions, he must spend money on fake cover letters just to get hired.

Rafael has a choice of NN contractor jobs. The ii-th job starts on day ℓi\ell_i, ends at the end of day rir_i, and pays exactly SS gold coins per day. Rafael is a really bad worker and cannot work two jobs at the same time. He can start job ii only on day ℓi\ell_i, but once hired, he may quit at the end of any day, keeping the money from the days he worked (including the last one). After quitting, he can start another job from the next day on, but not on the same day. A fake cover letter for job ii costs cic_i gold coins. Rafael always has enough savings to pay for any number of cover letters, even before working any of the NN jobs.

Given the jobs, what is the maximum profit Rafael can make after subtracting the cover letter costs?

Input

The first line contains two integers NN (1≤N≤1061 \le N \le 10^6) and SS (1≤S≤1091 \le S \le 10^9), the number of jobs and their daily pay. Each of the following NN lines contains three integers ℓi\ell_i, rir_i, and cic_i (1≤ℓi≤ri≤1091 \le \ell_i \le r_i \le 10^9, 1≤ci≤1091 \le c_i \le 10^9), the start date, end date, and cost of the fake cover letter for job ii.

Output

Output a single line with one integer: the amount of money Rafael can make after all the jobs are over.

Examples4

  1. Example 1

    Input
    3 3
    1 5 10
    2 10 4
    5 15 1
    
    Expected output
    37
    
  2. Example 2

    Input
    3 5
    1 1 3
    2 3 4
    3 3 1
    
    Expected output
    8
    
  3. Example 3

    Input
    1 1000
    1 1 654
    
    Expected output
    346
    
  4. Example 4

    Input
    1 5
    1 3 20
    
    Expected output
    0