Optimal Matrix Multiplication
P11455

Given two matrices with dimensions n1 × n2 and n2 × n3,
the cost of the usual multiplication algorithm
is Θ(n1n2n3).
For simplicity, let us consider that the cost is exactly n1n2n3.

Suppose that we must compute
M1 × … × Mm,
where every Mi is an ni × ni+1 matrix.
Since the product of matrices is associative,
we can choose the multiplication order.
For example, to compute M1 × M2 × M3 × M4,
we could either choose (M1 × M2) × (M3 × M4),
with cost n1n2n3 + n3n4n5 + n1n3n5,
or either choose M1 × ((M2 × M3) × M4),
with cost n2n3n4 + n2n4n5 + n1n2n5,
or three other possible orders.

Write a program to find the minimum cost of computing
M1 × … × Mm.

Input

Input consists of several cases,
each one with m followed by the m + 1 dimensions.
Assume 2 ≤ m ≤ 100 and 1 ≤ ni ≤ 104.

Output

For every case, print the minimum cost
to compute the product of the m matrices.

About statements

The official statement of a problem is always the one
in the PDF document. The HTML and PNG versions of the statement
are also given to help you, but they may contain some content
that is not well displayed. In case of doubt, always use the PDF.