Pokemon sit on houses along a line, each with a candy value and a deadline; starting at house K, maximize candy collected while walking one house per second.
Medium7Dynamic programmingIntervalsNo attempts yetTime limit1sMemory limit512 MBBranimirko plays Pokemon Go a lot. He recently decided to organize a Pokemon catching competition. It is held in Ilica street in Zagreb, and his friend Slavko sponsors it. The reward is candy, of course.
Ilica is the longest street in Zagreb. N houses stand on one side of the street, numbered 1 through N. The competition starts at house K.
Before the competition Branimirko looked at the map and found M Pokemon. Pokemon i sits at house Ai, is worth Bi candy, and can be caught only before Ti seconds have passed since the start. The moment Ti seconds pass it disappears from the map and cannot be caught any more. No two Pokemon sit at the same house.
Branimirko moves to a neighboring house in one second. At the start, that is at second 0, he stands at house K. When he reaches a house at a time smaller than the Ti of the Pokemon there, he catches that Pokemon and it disappears from the map. Catching takes no time. He may walk over a house he has already visited.
Find the largest amount of candy Branimirko can get.
The first line contains the number of houses N, the starting house number K and the number of Pokemon M (1≤K≤N≤1000, 1≤M≤100).
Each of the next M lines contains Ai, Bi and Ti of one Pokemon (1≤Ai≤N, 1≤Bi≤100, 1≤Ti≤2000).
The Pokemon are given in increasing order of house number Ai.
Print the largest amount of candy Branimirko can get, on one line.
In the first example Branimirko catches the Pokemon at house 3 (5 candy), then the one at house 7 (10 candy), then the one at house 9 (100 candy), for 115 candy in total. He cannot catch the Pokemon at house 1. Walking there from the starting house 5 takes 4 seconds, and that Pokemon disappears the moment 4 seconds pass.