Live Programming

Time limit5sMemory limit512 MB

Summary
Choose a subset of songs within total length T and order them to maximize the sum of basic points minus squared feature differences between consecutive songs.
Level

Hard8 of 10

Topics
Dynamic programming, Sorting, Math, Greedy
Solved
No attempts yet

Problem

A famous Japanese idol group, JAG48, is planning the program for its next live performance. They have NN different songs, song_1\mathit{song}\_1, song_2\mathit{song}\_2, ..., and song_N\mathit{song}\_N. Each song has three integer parameters, t_it\_i, p_ip\_i, and f_if\_i: t_it\_i is the length of song_i\mathit{song}\_i, p_ip\_i is the basic satisfaction points the audience gets when song_i\mathit{song}\_i is performed, and f_if\_i is the feature value of song_i\mathit{song}\_i that affects the audience's satisfaction. During the live performance, JAG48 can perform any number (but at least one) of the NN songs, unless the total length of the chosen songs exceeds the length of the live performance TT. They can decide the order of the songs to perform, but they cannot perform the same song twice or more.

The goal of this live performance is to maximize the total satisfaction points the audience gets. In addition to the basic satisfaction points of each song, the difference between the feature values of two songs performed consecutively affects the total satisfaction points. If there is no difference, the audience feels comfortable. The larger the difference, however, the more frustrated the audience feels.

Thus, the total satisfaction points are calculated as follows:

  • If song_x\mathit{song}\_x is the first song of the live performance, the total satisfaction points just after song_x\mathit{song}\_x is p_xp\_x.
  • If song_x\mathit{song}\_x is the second or a later song and is performed just after song_y\mathit{song}\_y, p_x−(f_x−f_y)2p\_x - (f\_x - f\_y)^2 is added to the total satisfaction points, because the audience feels frustrated when f_xf\_x and f_yf\_y differ.

Help JAG48 find a program with the maximum total satisfaction points.

Input

The input is formatted as follows.

$N$ $T$

$t_1$ $p_1$ $f_1$

$\ldots$

$t_N$ $p_N$ $f_N$

The first line contains two integers NN and TT: the number of available songs NN (1≤N≤4,0001 \le N \le 4{,}000), and the length of the live performance TT (1≤T≤4,0001 \le T \le 4{,}000).

The following NN lines give the parameters of the songs. The ii-th of them contains three integers, the parameters of song_i\mathit{song}\_i: the length t_it\_i (1≤t_i≤4,0001 \le t\_i \le 4{,}000), the basic satisfaction points p_ip\_i (1≤p_i≤1081 \le p\_i \le 10^8), and the feature value f_if\_i (1≤f_i≤1041 \le f\_i \le 10^4).

You can assume that at least one song has length at most TT.

Output

Output the maximum total satisfaction points the audience can get during the live performance.

Examples5

  1. Example 1

    Input
    2 10
    10 200 1
    10 100 100
    
    Expected output
    200
    
  2. Example 2

    Input
    3 15
    5 100 1
    5 100 2
    5 100 4
    
    Expected output
    295
    
  3. Example 3

    Input
    3 10
    5 200 200
    5 200 201
    5 300 1
    
    Expected output
    399
    
  4. Example 4

    Input
    3 20
    5 100 200
    5 100 201
    5 300 1
    
    Expected output
    300
    
  5. Example 5

    Input
    5 61
    14 49 7
    31 46 4
    30 55 5
    52 99 1
    34 70 3
    
    Expected output
    103