Under construction

Still being built. Things may change

Shredding Company

Interview

Time limit2sMemory limit512 MB

Summary
Split a digit string into contiguous pieces so their sum is as large as possible without exceeding a target, reporting rejection on ties and error when even the smallest sum is too big.
Level

Medium6 of 10

Topics
Backtracking, Brute force, Recursion, Implementation
Solved
No attempts yet

Problem

You have just been put in charge of developing a new shredder for the Shredding Company. A normal shredder cuts sheets of paper into pieces small enough that the contents cannot be read. This new shredder works differently.

  • The shredder takes a target number and a sheet of paper with a number written on it.
  • It cuts the sheet into pieces, and every piece carries one or more digits.
  • The sum of the numbers on the pieces is as close to the target number as possible without going over it.

Suppose the target number is 50 and the sheet of paper has the number 12346. The shredder cuts the sheet into four pieces: 1, 2, 34, and 6. Their sum is 43 (= 1 + 2 + 34 + 6), which is the closest to 50 among all combinations that do not exceed 50. Cutting into 1, 23, 4, and 6 is not valid, because that sum is 34 (= 1 + 23 + 4 + 6), which is smaller than 43. Cutting into 12, 34, and 6 is not valid either, because that sum is 52 (= 12 + 34 + 6), which is greater than the target number 50.

Figure 1. Shredding a sheet of paper with the number 12346 when the target number is 50

Three special rules apply.

  • If the target number is the same as the number on the sheet, the paper is not cut. When the target number is 100 and the number on the sheet is also 100, the paper stays whole.
  • If no combination has a sum less than or equal to the target number, the display prints error. When the target number is 1 and the number on the sheet is 123, the combination with the smallest sum is 1, 2, 3, whose sum is 6. That is larger than the target number, so error is printed.
  • If more than one combination reaches the closest sum without going over the target number, the display prints rejected. When the target number is 15 and the number on the sheet is 111, two combinations reach the largest possible sum 12: (a) 1 and 11, and (b) 11 and 1. So rejected is printed.

To build the shredder, you decide to write a program that simulates the behavior and the rules above. Given a target number and the number on the sheet of paper, work out how the shredder should cut the second number.

Input

The input has several test cases, one per line.

t1 num1
t2 num2
...
tn numn
0 0

Each test case is two positive integers separated by one space. The first integer tit_i is the target number. The second integer numi\text{num}_i is the number on the paper to be shredded.

Neither integer has 0 as its first digit, so 123 is allowed and 0123 is not. Both integers are at most 6 digits long. A line holding two zeros ends the input.

Output

For each test case, print one line in one of these three forms.

  • sum\text{sum} part1\text{part}_1 part2\text{part}_2 ...
  • rejected
  • error

In the first form, partj\text{part}_j and sum\text{sum} mean the following.

  • partj\text{part}_j is the number on one piece of shredded paper. The pieces follow the order of the original digits on the sheet.
  • sum\text{sum} is the total of the numbers after shredding, so sum=part1+part2+⋯\text{sum} = \text{part}_1 + \text{part}_2 + \cdots.

Separate the numbers with one space.

Print error when no combination is possible, and rejected when more than one combination reaches the closest sum.

Do not print extra characters, including spaces, at the start of a line or at the end of a line.

Examples3

  1. Example 1

    Input
    50 12346
    376 144139
    927438 927438
    18 3312
    9 3142
    25 1299
    111 33333
    103 862150
    6 1104
    0 0
    
    Expected output
    43 1 2 34 6
    283 144 139
    927438 927438
    18 3 3 12
    error
    21 1 2 9 9
    rejected
    103 86 2 15 0
    rejected
    
  2. Example 2

    Input
    100 100
    1 123
    15 111
    0 0
    
    Expected output
    100 100
    error
    rejected
    
  3. Example 3

    Input
    1 1
    9 9
    5 9
    9 1
    10 19
    0 0
    
    Expected output
    1 1
    9 9
    error
    1 1
    10 1 9