Problem A
Assembly Line
The last worker in a production line at the factory of
Automated Composed Machinery is worried. She knows that her job
hangs in the balance unless her productivity increases. Her
work consists of assembling a set of pieces in a given
sequence, but the time spent on assembling pieces
In order to aid her, you need to find the optimal way to
assemble all components. The input to your program will be a
set of symbols representing (types of) pieces, and a so-called
assembly table representing the time it takes to assemble them,
as well as the type of the resulting component. For instance,
we may have two symbols
|
|
|
|
3- |
5- |
|
6- |
2- |
This means, for example, that two pieces of type
For a sequence of components labelled
-
with time . -
with time .
So the result for this case would be a piece of type
Input
The input consists of a single test case. This test case
begins with a line containing a natural number
After the single test case follows a line containing 0, which should not be processed.
Output
Print
Sample Input 1 | Sample Output 1 |
---|---|
2 a b 3-b 5-b 6-a 2-b 2 aba bba 0 |
9-b 8-a |
Sample Input 2 | Sample Output 2 |
---|---|
2 m e 5-e 4-m 3-e 4-m 1 eme 0 |
7-m |