Mobile
InterviewTime limit1sMemory limit128 MB
Given a mobile's arm structure and pivot distances, find the smallest integer weights, with a named weight at least w, that keep every arm balanced.
- Level
Medium6 of 10
- Topics
- Tree, Math, Number theory, DFS
- Solved
- No attempts yet
Problem
Consider a mobile hanging from the ceiling. It is suspended by a single wire attached to a pivot point on an arm. At each end of an arm hangs either another wire holding a further arm, or a weight.
A mobile must balance. For an arm whose pivot point sits at distance from its left end and from its right end, the arm balances exactly when
where is the total weight hanging from the left end and is the total weight hanging from the right end. The weight of the arms and wires themselves is ignored.
For example, in one mobile, if weight 1 weighs 8 units, then weights 2, 3, 4, and 5 must weigh 2, 6, 4, and 4 units respectively. In general, once you know the structure of the mobile (how the arms are arranged and where each pivot point sits) and the value of a single weight, the values of all the weights are determined.
There is one extra condition: every weight must be an integer. So you are given the desired minimum value of one weight and must choose the other weights so that all of them are integers. To make this possible, the specified weight may have to be raised slightly above its given minimum.
Input
The input consists of several test cases. Each test case begins with a line containing a positive integer , the number of arms. The arms are numbered from 1 to .
The next lines describe the arms, in order from 1 to , each in the form
dL dR typeL typeR nL nR
- , : the distances from the pivot point to the left end and to the right end (each an integer at most 20).
- , :
Wif a weight hangs from that end, orAif an arm hangs from it. - , : the index number of the weight or arm hanging from the left end and the right end.
Weight indices start at 1 and are consecutive. No arm hangs more than 6 levels below the top.
After the arm descriptions comes a line of the form m w, meaning that weight weighs at least ().
A line containing a single follows the last test case.
Output
For each test case, print one line with the minimum total weight of the mobile given that weight is at least . Print each answer in the form Case k: x, where is the test case number starting from 1 and is the minimum total weight. You may assume every output value is less than .