Pokemon Hunt
Time limit1sMemory limit512 MB
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.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Intervals
- Solved
- No attempts yet
Problem
Branimirko 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. houses stand on one side of the street, numbered through . The competition starts at house .
Before the competition Branimirko looked at the map and found Pokemon. Pokemon sits at house , is worth candy, and can be caught only before seconds have passed since the start. The moment 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 , he stands at house . When he reaches a house at a time smaller than the 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.
Input
The first line contains the number of houses , the starting house number and the number of Pokemon (, ).
Each of the next lines contains , and of one Pokemon (, , ).
The Pokemon are given in increasing order of house number .
Output
Print the largest amount of candy Branimirko can get, on one line.
Note
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.