A Fair Jury

Time limit1sMemory limit128 MB

Summary
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 ii in the pool, the prosecution assigns a value pip_i and the defense assigns a value did_i, each an integer from 00 to 2020. A higher value means that party considers the candidate more suitable.

You must choose a jury JJ of exactly mm candidates. For a chosen jury JJ define

D(J)=∑k∈JdkP(J)=∑k∈JpkD(J) = \sum_{k \in J} d_k \qquad P(J) = \sum_{k \in J} p_k

so D(J)D(J) is the total defense value and P(J)P(J) the total prosecution value of the jury.

To keep the trial fair, the jury should favour neither side, so ∣D(J)−P(J)∣|D(J) - P(J)| 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 D(J)+P(J)D(J) + P(J) 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 nn and mm: the number of candidates and the required number of jury members, with 1≤n≤2001 \le n \le 200, 1≤m≤201 \le m \le 20, and m≤nm \le n.

Each of the next nn lines contains two integers pip_i and did_i (0≤pi,di≤200 \le p_i, d_i \le 20): the prosecution value and the defense value of candidate ii.

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 mm members always exists because m≤nm \le n. A brute-force search over all (nm)\binom{n}{m} juries is far too slow for the given limits; an efficient approach, for example dynamic programming over the possible values of D(J)−P(J)D(J) - P(J), is required.

Examples2

  1. Example 1

    Input
    4 2
    1 2
    2 3
    4 1
    6 2
    
    0 0
    
    Expected output
    Jury #1
    Minimum difference |D(J) - P(J)| is 2
    Maximum total D(J) + P(J) is 10
    
    
  2. Example 2

    Input
    3 1
    5 5
    0 20
    10 12
    
    0 0
    
    Expected output
    Jury #1
    Minimum difference |D(J) - P(J)| is 0
    Maximum total D(J) + P(J) is 10