## GCC '16 P3 - Hang Gliding Fun

View as PDF

Points: 25 (partial)
Time limit: 1.0s
Java 8 1.4s
Memory limit: 256M

Authors:
Problem types

Bob lives in a city with skyscrapers arranged in a row running north to south. He likes to hang glide over the city. Bob can glide south to a shorter building if all of the passed buildings' heights are smaller than his current building's height.

Each of the buildings has height , view value , and danger value for . Bob has questions for you: Given that he starts at the northmost building (), what is the maximum possible sum of the view values of buildings which he lands on such that none of those buildings has danger value more than ()?

#### Input Specification

The first line contains two space-separated numbers () and (), the number of buildings and queries respectively.
The next lines each contain (), (), and (), the height, view value, and danger value for the northmost building.
The next lines each contain the query. Each line contains two space-separated numbers () and (). It is guaranteed that the danger value of the northmost building is not more than .

#### Output Specification

For each query, output the answer on a new line.

#### Sample Input 1

3 2
3 1 2
3 4 4
1 2 10
2 20
2 4

#### Sample Output 1

6
4

#### Explanation

For the first query, the path which maximizes the sum of view values is hang gliding to the third building, so the answer is . For the second query, the path which maximizes the sum of view values is simply staying put at the second building, so the answer is .