It Roars


Submit solution

Points: 1
Time limit: 1.5s
Memory limit: 256M

Author:
Problem type

Cady Heron is out in the savanna, watching the lions and birds and stuff. With her trusty camera, she wants to take as many impressive photos of animals as possible.

Animals will appear all around her at different times. The animals are quick, so if Cady doesn't take a photo of them at the perfect moment, they will run off.

As a master of nature, Cady knows exactly when and where the animals will appear. This is measured in seconds and degrees relative to her starting position (0 degrees). She can only take a photo of an animal when she is facing it directly, and can only turn around at s degrees per second (in either direction).

Each animal also has a "coolness" value, c, which Cady earns if she successfully snaps a photo of it.

Given the positions and timings of animals on the savanna, how can Cady maximise her total "coolness"?

Input

The first line of each test case contains an integer n \ (1 \leq n \leq 1000), the number of animals that will appear in the savanna.

The second line contains an integer s \ (0 \leq s \leq 360), the number of degrees Cady can turn per second.

The next n lines consist of three integers t, a and c, where t \ (1 \leq t \leq 10^8) is the time an animal appears, a \ (0 \leq a \leq 359) is the angle it appears at, and c \ (1 \leq c \leq 10^8) is the animal's "coolness".

Output

Output the total coolness Cady can get by taking photos of animals.

Clarifications

  • Cady's position is measured in degrees.
  • She can turn clockwise or counterclockwise at s degrees per second.
  • 1 degree clockwise of 0 is 1 degree. 1 degree counterclockwise of 0 is 359 degrees.

Example

Input 1
3
1
5 5 10
10 0 10
10 20 20
Output 1
20
  • Cady can turn around to 5 degrees in 5 seconds and take a photo of the first animal just in time, which has 10 coolness.
  • She can then turn back around to 0 degrees in 5 seconds and take a photo of the second animal just in time, which has 10 coolness.
  • The last animal is not reachable.

Overall, she can collect 20 coolness.

Input 2
4
2
1 0 20
4 5 20
7 350 40
8 10 10
Output 2
60
  • First, Cady waits for 1 second and takes a photo of the first animal, which has 20 coolness.
  • Next, Cady turns 10 degrees in 5 seconds to position herself at 350 degrees. Then, she waits 1 second before taking a photo of the third animal, which has 40 coolness.
  • With this movement, Cady is unable to take a photo of the second and fourth animals.

It can be shown that 60 is the maximum coolness Cady can collect.


Comments

There are no comments at the moment.