#731
Unrated
Ski Slope
시간 제한
2s
메모리 제한
1024MB
제출
0
정답
0
맞힌 사람
0
정답 비율
0.0%

문제

Bessie is going on a ski trip with her friends. The mountain has NN waypoints (1N1051\leq N \leq 10^5) labeled 1,2,,N1, 2, \ldots, N in increasing order of altitude (waypoint 11 is the bottom of the mountain).

For each waypoint i>1i > 1, there is a ski run starting from waypoint ii and ending at waypoint pip_i (1pi<i1\le p_i<i). This run has difficulty did_i (0di1090 \leq d_i \leq 10^9) and enjoyment eie_i (0ei1090 \leq e_i \leq 10^9).

Each of Bessie's MM friends (1M1051\leq M \leq 10^5) will do the following: They will pick some initial waypoint ii to start at, and then follow the runs downward (to pip_i, then to ppip_{p_i}, and so forth) until they get to waypoint 11.

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 sjs_j (0sj1090 \leq s_j \leq 10^9) and courage level cjc_j (0cj100 \leq c_j \leq 10), which limits them to selecting an initial waypoint that results in them taking at most cjc_j runs with difficulty greater than sjs_j.

For each friend, compute the maximum enjoyment they can get.

입력

The first line contains NN.

Then for each ii from 22 to NN, a line follows containing three space-separated integers pip_i, did_i, and eie_i.

The next line contains MM.

The next MM lines each contain two space-separated integers sjs_j and cjc_j.

출력

Output MM 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++).

예제 입력 1

4
1 20 200
2 30 300
2 10 100
8
19 0
19 1
19 2
20 0
20 1
20 2
29 0
30 0

예제 출력 1

0
300
500
300
500
500
300
500

점수

Inputs 2-4: N,M1000N, M\le 1000Inputs 5-7: All cj=0c_j=0Inputs 8-17: No additional constraints.

코드 제출

코드를 제출하려면 로그인이 필요합니다.

로그인
내 제출
제출 내역이 없습니다.
맞은 사람
아직 맞은 사람이 없습니다.
난이도 투표
Unrated0명 투표
로그인 후 AC 받으면 투표할 수 있습니다.
전체 제출
제출 내역이 없습니다.