This page is still under construction.

Parts of this page are still being built. What you see may change.

Jury Compromise

Time limit1sMemory limit128 MB

Summary
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 00 to 2020 that expresses how strongly they favour that person: 00 means total rejection, while 2020 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 nn candidates. For each candidate ii you are given the prosecution's grade pip_i and the defence's grade did_i. You must choose a jury of exactly mm people. For a subset J⊆{1,…,n}J \subseteq \{1, \dots, n\} with mm elements, let D(J)=∑k∈JdkD(J) = \sum_{k \in J} d_k and P(J)=∑k∈JpkP(J) = \sum_{k \in J} p_k 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:

  1. The imbalance ∣D(J)−P(J)∣|D(J) - P(J)| must be as small as possible.
  2. Among all juries with that minimum imbalance, the total D(J)+P(J)D(J) + P(J) must be as large as possible, so the jury is as valuable as possible to both sides.
  3. 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 1,5,6,91,5,6,9 comes before 2,3,4,52,3,4,5 because 1<21 < 2, and 1,2,3,5,91,2,3,5,9 comes before 1,2,3,6,91,2,3,6,9 because 5<65 < 6.

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 nn and mm: the number of candidates and the number of jury members, with 1≤n≤2001 \le n \le 200, 1≤m≤201 \le m \le 20, and m≤nm \le n. The next nn lines each contain two integers pip_i and did_i — the prosecution's grade and the defence's grade for candidate ii — where 0≤pi,di≤200 \le p_i, d_i \le 20. 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 rr is the round number (1,2,…1, 2, \dots).

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 mm 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.

Examples4

  1. Example 1

    Input
    4 2
    1 2
    2 3
    4 1
    6 2
    
    0 0
    
    Expected output
    Jury #1
    Best jury has value 6 for prosecution and value 4 for defence:
     2 3
    
  2. Example 2

    Input
    1 1
    5 10
    
    0 0
    
    Expected output
    Jury #1
    Best jury has value 5 for prosecution and value 10 for defence:
     1
    
  3. Example 3

    Input
    4 2
    1 3
    2 2
    2 2
    3 1
    
    0 0
    
    Expected output
    Jury #1
    Best jury has value 4 for prosecution and value 4 for defence:
     1 4
    
  4. Example 4

    Input
    2 1
    0 2
    2 0
    
    0 0
    
    Expected output
    Jury #1
    Best jury has value 0 for prosecution and value 2 for defence:
     1