luogu#P8065. [BalkanOI 2012] The Best Teams
[BalkanOI 2012] The Best Teams
Background
You are the national team coach, and you need to choose the best team to take part in the BOI World Cup.
Problem Description
You have players, numbered . Player has an age and a skill value .
The quality of a team is the sum of the skill values. However, you believe it is not acceptable to put two players with similar skill values in the same team, because they will cause conflicts.
- We say that two players have similar skill values if and only if there does not exist a third player such that .
The team will play matches. Each match has an age limit , which means that for any player taking part in this match, . In addition, each match has a team size limit , meaning the number of participating players .
A player may participate multiple times.
Input Format
The first line contains an integer , representing the number of players.
Each of the next lines contains two space-separated integers .
The next line contains an integer , representing the number of matches.
Each of the next lines contains two integers , representing the age limit and the team size limit for each match.
Output Format
For each match, output the quality of the team you choose.
7
17 21
24 36
14 19
27 20
21 50
18 5
33 7
4
20 3
50 2
99 5
10 2
45
71
95
0
Hint
Constraints
Subtask #0 is the sample.
, .
For all , we have .
Sample Explanation
There are pairs of players with similar skill values: , , , , , .
In the first match, players are selected.
In the second match, players are selected.
In the third match, players are selected.
In the fourth match, no players can be selected, because there is no player with age .
Translated by ChatGPT 5