Jury Compromise
Time limit1sMemory limit128 MB
Pick exactly m candidates minimizing the prosecution minus defence imbalance, breaking ties by the largest total value, then by lexicographically smallest candidate list.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Sorting, Greedy
- Solved
- No attempts yet
Problem
In the far-away country of Frobnia, court verdicts are decided by a jury drawn from the general public. Whenever a trial begins, a jury must be selected as follows. First, a pool of people is drawn at random. For each person in the pool the defence and the prosecution each assign a grade from to that expresses how strongly they favour that person: means total rejection, while means the person is considered ideal for the jury.
Using these two grades, the judge picks the jury. To keep the trial fair, the jury's overall leaning toward the defence or the prosecution should be as balanced as possible, so the jury must be satisfactory to both sides.
Formally, you are given a pool of candidates. For each candidate you are given the prosecution's grade and the defence's grade . You must choose a jury of exactly people. For a subset with elements, let and be the jury's total value for the defence and for the prosecution.
An optimal jury is determined by the following rules, applied in order:
- The imbalance must be as small as possible.
- Among all juries with that minimum imbalance, the total must be as large as possible, so the jury is as valuable as possible to both sides.
- If several juries still tie, choose the one whose list of candidate numbers, written in ascending order, comes first in "pseudo-alphabetic" order — that is, compare the sorted candidate lists as though the numbers were letters. For example comes before because , and comes before because .
Applied together, these three rules always single out exactly one jury. Write a program that carries out this selection.
Input
The input contains several jury-selection rounds. Each round begins with a line containing two integers and : the number of candidates and the number of jury members, with , , and . The next lines each contain two integers and — the prosecution's grade and the defence's grade for candidate — where . A blank line may separate one round from the next.
The input ends with a round whose first line reads 0 0; that terminating round is not processed.
Output
For each round, print a line Jury #r, where is the round number ().
On the next line print the prosecution total and the defence total in exactly this form (prosecution first, defence second):
Best jury has value <P(J)> for prosecution and value <D(J)> for defence:
Then, on the following line, print the numbers of the chosen candidates in ascending order, printing a single space before each number.
Print one blank line between consecutive rounds; there is no blank line before the first round and no blank line after the last.