Live Programming

Time Limit : 5 sec, Memory Limit : 524288 KB

Problem I: Live Programming

A famous Japanese idol group, JAG48, is planning the program for its next live performance. They have $N$ different songs, $song_1$, $song_2$, ..., and $song_N$. Each song has three integer param- eters, $t_i$, $p_i$, and $f_i$: $t_i$ denotes the length of $song_i$, $p_i$ denotes the basic satisfaction points the audience will get when $song_i$ is performed, and $f_i$ denotes the feature value of songi that affects the audience's satisfaction. During the live performance, JAG48 can perform any number (but at least one) of the $N$ songs, unless the total length of the chosen songs exceeds the length of the live performance $T$. 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 that the audience will get. In addition to the basic satisfaction points of each song, the difference between the feature values of the two songs that are performed consecutively affects the total satisfaction points. If there is no difference, the audience will feel comfortable. However, the larger the difference will be, the more frustrated the audience will be.

Thus, the total satisfaction points will be calculated as follows:

  • If $song_x$ is the first song of the live performance, the total satisfaction points just after $song_x$ is equal to $p_x$.
  • If $song_x$ is the second or subsequent song of the live performance and is performed just after $song_y$, $p_x -(f_x -f_y)^2$ is added to the total satisfaction points, because the audience will get frustrated if $f_x$ and $f_y$ are different.

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$
: : :
$t_N$ $p_N$ $f_N$

The first line contains two integers $N$ and $T$: the number of the available $song_s$ $N$ ($1 \leq N \leq 4,000$), and the length of the live performance $T$ ($1 \leq T \leq 4,000$).

The following $N$ lines represent the parameters of the songs. The $i$-th line of them contains three integers, which are the parameters of $song_i$: the length $t_i$ ($1 \leq t_i \leq 4,000$), the basic satisfaction points $p_i$ ($1 \leq p_i \leq 10^8$), and the feature value $f_i$ ($1 \leq f_i \leq 10^4$).

You can assume that there is at least one song whose length is less than or equal to $T$.

Output

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

Sample Input

2 10
10 200 1
10 100 100

Output for the Sample Input

200

Sample Input

3 15
5 100 1
5 100 2
5 100 4

Output for the Sample Input

295

Sample Input

3 10
5 200 200
5 200 201
5 300 1

Output for the Sample Input

399

Sample Input

3 20
5 100 200
5 100 201
5 300 1

Output for the Sample Input

300

Sample Input

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

Output for the Sample Input

103

Source: ACM-ICPC Japan Alumni Group Summer Camp 2015 , Day 4, Tokyo, Japan, 2015-09-14
http://acm-icpc.aitea.net/
http://jag2015summer-day4.contest.atcoder.jp/