Matrix Chain Multiplication (min scalar multiplications)

MEDIUM · C · 2000ms

For a factory-automation problem, Sabbir must decide the cheapest way to multiply a chain of matrices representing sequential transformations. Given the dimensions of N matrices to multiply in order, print the minimum number of scalar multiplications needed to compute the product, using optimal parenthesization.

Input

Line1: N. Line2: N+1 integers (dimensions p0..pN).

Output

Minimum scalar multiplications.

Constraints

1 ≤ N ≤ 100

Sample Input

3
10 20 30 40

Sample Output

18000
main.c
Loading editor…

Write C, then Run (custom I/O) or Submit (sample tests). ⌘/Ctrl+Enter submits.

Matrix Chain Multiplication (min scalar multiplications) · DIU ContestHub