Sie sind auf Seite 1von 2

Amdocs Code Mania 2013 Weighty Matters!

Lalu owns a shop that sells weighing scales (see figure alongside) and weights. All the scales in his shop weigh the same ten kilograms. They are of high quality and are well calibrated such that when equal weights are placed on both sides, they balance correctly. During his free time, Lalu imagines a grand tower made of many scales placed on one another as well as weights placed on some of them. He imagines the challenge it would pose to balance the entire tower! Your aim is to write a program to help Lalu balance any tower configuration by adding minimum weights on some scales. The balancing is always done by adding additional weights on lower scales and not by adding additional scales.

Input
The input file will contain several test cases. Each test case begins on a separate line with a single integer N (1<=N<=50) specifying the number of scales used in that input tower configuration. For simplicity, imagine all scales used in any test case are numbered starting from 0. Every scale of the test case is described in a pair of lines. Therefore, scale 0 is explained in lines 1 and 2, scale 1 is explained in lines 3 and 4 and so on. Each pair of lines is formatted as follows: X <scale-list> Y <scale-list> where X and Y depict the weights in kilograms added to left and right sides respectively of that scale and <scalelist> is a list of scales (zero or more) that are on that side of that scale. The end of all the test cases and hence, the end of input is signified by N having a value of E. Consider the input in the example below: There are 4 scales in this test case. Scale 0 has scale 1 on the left side and scale 2 on the right side but no weights on either side. Scale 1 has scale 3 on the right side but no weights on either side. Scale 2 has 3 kilograms weight placed on the left side but nothing on the right side. Scale 3 has nothing placed on either of its sides.

Output
For each test case, your output should be N lines long, each line listing the minimum weight added to the scale (nth line listing the weights added to the nth scale), formatted as: <scale-id> <weight added to left side> <weight added to right side>. The output for each test-case should have lines in the increasing order of scale id. Let us balance this tower configuration. Scale 3 has nothing on it and hence it is already balanced. However it weighs 10 kilograms. Scale 2 has no scales placed on it but has 3 kilograms on the left. To

balance it, Lalu adds 3 kilograms on its right side. Now it is correctly balanced and weighs 16 kilograms. Scale 1 has scale 3 on its right side and since that weighs 10 kilograms, Lalu adds 10 kilograms on its left side. Now, scale 1 weighs 30 kilograms. Finally, scale 0 has scale 1 on its left side weighing 30 kilograms and scale 2 on its right side weighing 16 kilograms. Lalu balances it by adding 14 kilograms to its right side. The output is given below

An example of multiple test cases


Input 4 0 1 0 2 0 0 3 3 0 0 0 E The sample input file would look like this (right-click on the link to download). The sample output file would look like this (right-click on the link to download). Output 0 0 14 1 10 0 2 0 3 3 0 0

Das könnte Ihnen auch gefallen