This page is still under construction.

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

Child Play

Time limit1sMemory limit128 MB

Summary
Given domino-like slabs, orient and order them so both rows sum equally, discarding one slab only if necessary and preferring the smallest minimum half.
Level

Medium5 of 10

Topics
Dynamic programming, Greedy, Sorting, Implementation
Solved
No attempts yet

Problem

The people of the tiny island of Tookutoo love mathematics and teach their children several math games. One popular puzzle in Tookutoo is played with ceramic slabs like the ones shown below.

Each slab is like a domino: it is split into two halves, and each half has an integer printed on it. The three slabs above show the values [2, 1], [6, 3], and [3, 1]. A slab [a, b] may be flipped, so it can also be used as [b, a].

A player is given a set of slabs drawn at random from a large, varied pool. Using those slabs, the player must lay them side by side on the table so that the sum of the numbers in the top row equals the sum of the numbers in the bottom row. For the set shown above, one correct arrangement is:

1 6 1
2 3 3

Here the top row and the bottom row both add up to 88.

If no arrangement can use every slab, the player may discard exactly one slab — but the resulting equal sum must then be as large as possible. If several different slabs could be discarded while leaving that same largest sum, the player must discard the slab [a, b] (written with a≤ba \le b) whose value aa is the smallest.

Write a program that, given a set of slabs, reports the equal sum of a valid arrangement, discarding one slab only when necessary.

Input

The input contains several test cases. The first line of each test case holds a single integer NN, the number of slabs (0≤N≤4000 \le N \le 400). Each of the next NN lines holds two integers XiX_i and YiY_i describing one slab (0≤Xi≤10000 \le X_i \le 1000, 0≤Yi≤10000 \le Y_i \le 1000). A line containing N=0N = 0 marks the end of the input and is not a test case.

Output

For each test case, print a single line. If no valid arrangement exists even after discarding one slab, print impossible. Otherwise print the equal sum, followed by a description of the discarded slab: print discard X Y with X≤YX \le Y when a slab had to be discarded, or discard none when every slab was used.

Examples2

  1. Example 1

    Input
    4
    1 4
    2 9
    2 1
    0 4
    2
    8 1
    9 4
    3
    6 3
    1 2
    3 1
    0
    
    Expected output
    10 discard 1 2
    impossible
    8 discard none
    
  2. Example 2

    Input
    3
    2 1
    6 3
    3 1
    0
    
    Expected output
    8 discard none