eolymp
bolt
Спробуйте наш новий інтерфейс для відправки розв'язків

Mobile

\includegraphics{https://static.e-olymp.com/content/30/30e9cb962a334d19e3d07d58d32b0dac15166764.jpg} You've probably seen mobiles suspended from the ceilings of museums or airports. We'll restrict ourselves to the type suspended from the ceiling by a single wire that is attached to a pivot point on an arm (also made of wire). At each end of the arm is either another wire suspending yet another arm, or a weight (usually in the form of some design). Below is one example, made by Alexander Calder, the best-known mobile artist. Some mobiles are simple and some are quite complex. Besides the artistry, these must balance. Recall that from a pivot point distance \textbf{d_L} from the left and dR from the right, an arm will balance if the product of the weight at the leftend and \textbf{d_L} is equal to the product of the weight at the right end and \textbf{d_R}. (We ignore the weight of the arm and the wires suspending the arms.) For example, consider the mobile drawn below. If weight \textbf{1} weighs \textbf{8} units, then weights \textbf{2}, \textbf{3}, \textbf{4}, and \textbf{5} must weigh \textbf{2}, \textbf{6}, \textbf{4}, and \textbf{4} units respectively. In fact, if you know the structure of the mobile, that is, the arrangement of arms and where the pivot points are on each arm, and the value of one weight, you can determine the values of all the weights. That is your problem here \{ almost. It seems you only have weights that are integer valued. So, you'll be given the desired minimum value of one weight and determine the value of the other weights, so that those values will also be integers. Thus, it's possible that the speci ed minimum valued weight must be raised a little bit to accomplish this. \includegraphics{https://static.e-olymp.com/content/0a/0ab7b71dd224d4b35a8300aa55b8b0a4e185cf34.jpg} \InputFile Input for each test case will start with a line containing the positive integer \textbf{n}, indicating the number of arms in the mobile. These arms are numbered \textbf{1} through \textbf{n}. The next \textbf{n} lines will describe the arms, in order \textbf{1}, \textbf{2}, ..., \textbf{n}, and will be in the form \textbf{d_L} \textbf{d_R} \textbf{type_L type_R n_L n_R} where \textbf{d_L} and \textbf{d_R} are integers ≤ \textbf{20} giving the distances from the pivot point to the left end and right end of the arm,\textbf{type_L} and \textbf{type_R} are each either \textbf{W} or \textbf{A}, indicating that a weight or arm hangs from the left or right ends, and \textbf{n_L}and \textbf{n_R} are the index numbers of the weight or arm on the left and right. The indices for the weights will start at \textbf{1}and be consecutive. The mobile will not have an arm that is hanging further down than 6 arms from the top. In our example above the lowest arm is \textbf{3} arms from the top. Following the description of the arms is a line of the form \textbf{m w}, indicating that weight \textbf{m} weighs at least \textbf{w}, where \textbf{1}≤ \textbf{w} ≤ \textbf{20}. A line containing a \textbf{0} follows the last test case. \OutputFile For each test case output one line giving the minimum total weight of the mobile if weight \textbf{m} is at least \textbf{w}. Use the format given in the Sample Output. You may assume all output values will be less than \textbf{10^9}.
Ліміт часу 1 секунда
Ліміт використання пам'яті 64 MiB
Вхідні дані #1
4
3 1 W W 2 3
4 2 W A 1 3
2 2 A A 1 4
1 1 W W 4 5
1 8
4
2 2 A W 2 5
3 1 W A 4 3
4 1 W A 3 4
2 1 W W 1 2
3 20
0
Вихідні дані #1
Case 1: 24
Case 2: 280
Джерело 2011 East Central Regional Contest