Matrix Multiplication Order 4
Time limit1sMemory limit512 MB
Given N matrices whose row and column counts never increase, find the fewest multiplications needed to multiply them in their fixed order, with N up to 1,000,000.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Greedy
- Solved
- No attempts yet
Problem
Multiplying an matrix by an matrix takes multiplications in total. The number of multiplications needed to multiply matrices depends on the order in which they are multiplied.
For example, suppose is , is , and is , and consider computing the product .
- If is computed first and then multiplied by , computing takes multiplications.
- If is computed first and then is multiplied by it, computing takes multiplications.
The same product needs a different number of multiplications depending on the order.
Given the sizes of matrices, write a program that finds the minimum number of multiplications needed to multiply all of them. The order of the input matrices cannot be changed.
Input
The first line contains the number of matrices ().
From the second line to line , line contains the size and of the -th matrix. ()
The input always contains sizes that can be multiplied in order.
The sizes of the given matrices are guaranteed to be monotonically non-increasing. That is, for , and .
Output
Print the minimum number of multiplications needed to multiply the given matrices on the first line. The answer is a natural number less than or equal to . Also, even when the matrices are multiplied in the worst order, the number of multiplications is less than or equal to .