A Fair Jury
Time limit1sMemory limit128 MB
Pick exactly m candidates from a pool, minimizing |total defense minus total prosecution|, then maximizing the combined total among those juries.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Array, Implementation, Brute force
- Solved
- No attempts yet
Problem
In the country of Brutopia, court verdicts are decided by a jury of ordinary citizens. Before each trial a jury must be chosen from a pool of candidates.
For every candidate in the pool, the prosecution assigns a value and the defense assigns a value , each an integer from to . A higher value means that party considers the candidate more suitable.
You must choose a jury of exactly candidates. For a chosen jury define
so is the total defense value and the total prosecution value of the jury.
To keep the trial fair, the jury should favour neither side, so must be as small as possible. Among all juries that reach this smallest possible difference, the jury should be as valuable as possible to both parties, that is should be as large as possible.
Given the pool of candidates, determine these two optimal quantities.
Input
The input consists of several jury-selection rounds.
Each round begins with a line containing two integers and : the number of candidates and the required number of jury members, with , , and .
Each of the next lines contains two integers and (): the prosecution value and the defense value of candidate .
Rounds may be separated by blank lines. The input ends with a round whose line reads 0 0, which must not be processed.
Output
For each round, output three lines.
The first line is Jury #k, where k is the round number counting from 1. The second line reports the smallest achievable difference. The third line reports the largest achievable total among the juries that reach that smallest difference.
Print the two values using exactly this format:
Jury #k
Minimum difference |D(J) - P(J)| is X
Maximum total D(J) + P(J) is Y
Here X is the minimum possible value of |D(J) - P(J)|, and Y is the maximum possible value of D(J) + P(J) over all juries achieving that minimum difference.
Print one blank line after every round.
Note
A jury of exactly members always exists because . A brute-force search over all juries is far too slow for the given limits; an efficient approach, for example dynamic programming over the possible values of , is required.