Optimal Matrix Multiplication - 2#1521

Optimal Matrix Multiplication - 2

1 second
128 megabytes
Very easy

Given two matrices A and B, matrix C = AB can be computed using the standard rules of matrix multiplication:

prb1521

The number of columns in matrix A must be equal to the number of rows in matrix B. Let matrix A have size m \times n and matrix B have size n \times p. Then, matrix C will have size m \times p and to compute C you need to perform m \times n \times p multiplications.

prb1521_4.gif

For example, if A has size 10 \times 20, and B has size 20 \times 15, then 10 \times 20 \times 15 = 3000 multiplications are needed to compute C.

prb1521_3.gif

Multiplication of several matrices can be done in different ways. For example, if we have matrices X, Y, and Z, the product XYZ can be computed either as or as . Suppose X has size 5 \times 10, Y has size 10 \times 20, and Z has size 20 \times 35.

prb1521.gif

Let's calculate the number of multiplications required for each approach:

It is clear that computing requires fewer multiplications.

prb1521_2.gif

Given a sequence of matrices to be multiplied, determine the optimal order of multiplication. The optimal order is the sequence that minimizes the total number of scalar multiplications.

Input

Each test consists of a sequence of matrices, described only by their sizes. Each test contains (n \leq 10), the number of matrices, followed by pairs of integers describing the sizes of the matrices (number of rows and columns), given in the order in which the matrices are multiplied. The last test contains n = 0 and should not be processed.

Output

Let the matrices be numbered A1, A2, ..., An. For each test, output its number and a parenthesized expression indicating the optimal multiplication order. The tests are numbered starting from . The output format must match the example exactly. If there are several optimal orders of multiplication, print any of them.

Examples

Example 1

Loading
Loading