#C2948. Minimum Additional Mana
Minimum Additional Mana
Minimum Additional Mana
You are given several test cases. In each test case, there are two sets of warriors: Alphas and Betas. Each warrior has a mana value and an expertise level. For a given expertise level, an Alpha can battle a Beta. The rules are as follows:
- If an Alpha’s current mana is at least the Beta’s mana, then the Alpha defeats the Beta by reducing its mana by the Beta’s mana cost.
- If the Alpha's mana is less than the Beta's mana, then the Alpha needs extra mana to defeat the Beta equal to \(\beta - \alpha\) (i.e. the Beta's mana minus the Alpha's mana), and both the Alpha and Beta are removed from the contest.
- If there is no Alpha of a certain level, then for every Beta at that level the additional mana needed is simply the Beta’s mana.
Your task is to compute the minimal additional mana required so that in every duel the Alpha wins over the Beta.
Note: Warriors can only fight opponents of the same expertise level. All calculations and any related formulas should be represented in \(\LaTeX\) if needed.
inputFormat
The first line of input contains an integer \(T\) denoting the number of test cases.
For each test case:
- The first line contains two space-separated integers \(N\) and \(M\) representing the number of Alphas and Betas, respectively.
- The next \(N\) lines each contain two integers: the mana and the expertise level of each Alpha.
- The following \(M\) lines each contain two integers: the mana and the expertise level of each Beta.
Input is read from standard input (stdin).
outputFormat
For each test case, output a single line containing one integer: the minimal additional mana required to ensure each duel is won by an Alpha.
Output should be written to standard output (stdout).
## sample2
2 3
30 1
45 2
25 1
30 2
15 2
1 1
20 3
25 3
0
5
</p>