The Best Teams

Time limit2sMemory limit128 MB

Summary
Given N players each with an age and distinct skill, and forbidden pairs that are adjacent in skill order, answer T queries each asking the maximum sum of at most K players with age at most A.
Level

Hard8 of 10

Topics
Dynamic programming, Sorting, Greedy, Binary search
Solved
No attempts yet

Problem

For each of several tournaments, a selector must pick the strongest possible team. There are NN players available, and each player has an age and a skill. The strength of a team equals the sum of the skills of its players.

However, two players with similar skills must never be placed on the same team, because they would interfere with each other and fail to collaborate. Two players are said to have similar skills if no other player has a skill value strictly between theirs. Since all skill values are distinct, this is exactly the condition that the two players are adjacent in the list of all NN players sorted by skill.

Teams are chosen for TT tournaments. Each tournament has two restrictions:

  • an age limit AA: every chosen player must have age at most AA;
  • a size limit KK: the team may contain at most KK players.

Tournaments are independent, so a player may be used in more than one tournament. Note that the adjacency (the similar skills relation) is always defined over the full sorted list of all NN players, regardless of which players are eligible for a given tournament.

For each tournament, determine the strength (total skill) of the strongest valid team the selector can assemble.

Input

The first line contains an integer NN — the number of players.

Each of the next NN lines contains two space-separated integers Agei\text{Age}_i and Skilli\text{Skill}_i — the age and skill of the ii-th player.

The next line contains an integer TT — the number of tournaments.

Each of the next TT lines contains two integers AiA_i and KiK_i — the age limit and the team-size limit of the ii-th tournament.

Output

For each tournament, print a single integer on its own line: the strength (total skill) of the strongest valid team, in the same order as the tournaments are given.

If no player can be selected, print 0. Use a 64-bit integer type, since the answer can be large.

Constraints

  • 1≤N≤300 0001 \le N \le 300\,000
  • 1≤T≤300 0001 \le T \le 300\,000
  • 1≤Agei, Skilli≤1091 \le \text{Age}_i,\ \text{Skill}_i \le 10^9
  • All skill values are distinct.

Explanation

In the sample, sorting the players by skill gives the order (by input index) 6,7,3,4,1,2,56, 7, 3, 4, 1, 2, 5. Hence the pairs of players with similar skills — those that cannot share a team — are (6,7),(7,3),(3,4),(4,1),(1,2),(2,5)(6,7), (7,3), (3,4), (4,1), (1,2), (2,5).

  • Tournament 1 (A=20A=20, K=3K=3): the best team is players {1,3,6}\{1, 3, 6\} with total skill 21+19+5=4521 + 19 + 5 = 45.
  • Tournament 2 (A=50A=50, K=2K=2): the best team is players {1,5}\{1, 5\} with total skill 21+50=7121 + 50 = 71.
  • Tournament 3 (A=99A=99, K=5K=5): the best team is players {1,3,5,6}\{1, 3, 5, 6\} with total skill 21+19+50+5=9521 + 19 + 50 + 5 = 95.
  • Tournament 4 (A=10A=10, K=2K=2): every player is older than 1010, so no team can be formed and the answer is 00.

Examples3

  1. Example 1

    Input
    7
    17 21
    24 36
    14 19
    27 20
    21 50
    18 5
    33 7
    4
    20 3
    50 2
    99 5
    10 2
    
    Expected output
    45
    71
    95
    0
    
  2. Example 2

    Input
    1
    5 100
    3
    5 1
    4 1
    10 1
    
    Expected output
    100
    0
    100
    
  3. Example 3

    Input
    3
    1 10
    100 20
    1 30
    2
    5 2
    5 1
    
    Expected output
    40
    30