Ski Slope
시간 제한2초메모리 제한2048 MB
각 정점 i>1은 p_i로 내려가는 간선을 하나 가지며 난이도 d_i와 즐거움 e_i가 있다. 질의 (s, c)마다 난이도가 s보다 큰 간선을 최대 c개 사용해 정점 1까지 내려갈 때 얻는 최대 즐거움 합을 구한다.
문제
Bessie is going on a ski trip with her friends. The mountain has waypoints () labeled in increasing order of altitude (waypoint is the bottom of the mountain).
For each waypoint , there is a ski run starting from waypoint and ending at waypoint (). This run has difficulty () and enjoyment ().
Each of Bessie's friends () will do the following: They will pick some initial waypoint to start at, and then follow the runs downward (to , then to , and so forth) until they get to waypoint .
The enjoyment each friend gets is equal to the sum of the enjoyments of the runs they follow. Each friend also has a different skill level () and courage level (), which limits them to selecting an initial waypoint that results in them taking at most runs with difficulty greater than .
For each friend, compute the maximum enjoyment they can get.
입력
The first line contains .
Then for each from to , a line follows containing three space-separated integers , , and .
The next line contains .
The next lines each contain two space-separated integers and .
출력
Output lines, with the answer for each friend on a separate line.
Note that the large size of integers involved in this problem may require the use of 64-bit integer data types (e.g., a "long long" in C/C++).