Pick waves with no wait-time overlap so the sum of fun points is as large as possible.
Medium5Dynamic programmingSortingBinary searchInterviewNo attempts yetTime limit4sMemory limit256 MBYou have taken up surfing in Florida, and you have the full schedule of today's waves. For each wave you know the minute it arrives, the fun points you earn by riding it, and how long you have to wait afterwards before you can ride again. The wait covers the ride itself plus the paddle back out to where the waves break.
If you ride the wave that arrives at minute mi, the next wave you ride has to arrive at minute mi+wi or later. Anything that arrives earlier is gone before you are back in position.
Taking the wave with the most fun points is not always best. Consider these four waves:
| Minute | Fun points | Wait time |
|---|---|---|
| 2 | 80 | 9 |
| 8 | 50 | 2 |
| 10 | 40 | 2 |
| 13 | 20 | 5 |
Riding the waves at minutes 8, 10 and 13 earns 110 fun points. Riding the wave at minute 2 keeps you out of position until minute 11, so the only wave left is the one at minute 13, for a total of 100 fun points. The best total here is 110.
Given the complete list of waves for the day, find the largest total of fun points you can earn.
The first line contains one integer n (1≤n≤300000), the number of waves for the day.
Each of the next n lines contains three space separated integers mi, fi and wi (1≤mi,fi,wi≤106): the minute the i-th wave arrives, its fun points, and its wait time.
No two waves arrive at the same minute. The waves are not necessarily listed in chronological order.
Print one integer on a single line, the maximum total of fun points you can earn.