Under construction

Still being built. Things may change

The Second Rhythm Game of Goinmul

Time limit2sMemory limit1024 MB

Summary
For each level with gauge cap X and fever length Y, find the maximum total score when fever time is activated each time the gauge fills.
Level

Hard8 of 10

Topics
Dynamic programming, Two pointers, Prefix sum, Sorting
Solved
No attempts yet

Problem

Karuna, a national rhythm game team member and a key figure in the World Cup semifinals, started a new rhythm game to dominate the genre.

The song Karuna plays consists of several notes. Each note must be hit exactly TiT_i seconds after the song starts, and hitting it gives a score AiA_i and energy BiB_i. The final score is the sum of the scores from all notes. AiA_i may be negative, but Karuna ignores that and always hits every note at the exact time.

The game also has a fever system. The fever gauge starts at 0. Each time a note is hit, the gauge rises by the energy from that note. However, if the fever gauge exceeds XX, it is capped at XX.

When the fever gauge reaches XX, Karuna can activate fever time at any moment. Fever time lasts exactly YY seconds, and notes processed during fever time score double. Notes processed at the moment fever time starts or ends also score double. The fever gauge does not rise during fever time, and it resets to 0 right after fever time ends.

The game has several difficulty levels. Each level has its own maximum fever gauge XX and fever time length YY. For each level, find the maximum score Karuna can obtain.

Input

The first line contains the number of notes NN.

Each of the next NN lines contains the time TiT_i, score AiA_i, and energy BiB_i of a note. All TiT_i are guaranteed to be distinct.

The next line contains the number of difficulty levels QQ.

Each of the next QQ lines contains the maximum fever gauge XiX_i and fever time length YiY_i of a level.

Output

Print QQ lines. The ii-th line contains the maximum score Karuna can obtain at the ii-th difficulty level.

Constraints

  • 1≤N≤1,000,0001 \leq N \leq 1,000,000
  • 1≤Q≤101 \leq Q \leq 10
  • 0≤Ti,Bi≤1090 \leq T_i, B_i \leq 10^9
  • −109≤Ai≤109-10^9 \leq A_i \leq 10^9
  • 1≤Xi,Yi≤1091 \leq X_i, Y_i \leq 10^9
  • All numbers in the input are integers.

Examples1

  1. Example 1

    Input
    5
    5 -1 1
    1 3 5
    3 -1 4
    2 -3 1
    4 7 3
    2
    5 3
    5 1
    
    Expected output
    11
    12