Matrix Multiplication Order 2
Time limit1sMemory limit512 MB
Given N matrix sizes in a fixed order, find the minimum number of scalar multiplications needed to compute their chain product.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Greedy
- Solved
- No attempts yet
Problem
A matrix of size and a matrix of size need multiplications to multiply. Multiplying matrices takes a different number of multiplications depending on the order in which they are multiplied.
For example, suppose is , is , and is , and consider computing the product .
- Multiplying first and then , takes multiplications.
- Multiplying first and then , takes multiplications.
The same product can need different numbers 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 must not be changed.
Input
The first line contains the number of matrices ().
From the second line on, each of the next lines contains the size and of a matrix. ()
Only sizes that can be multiplied in order are given.
Output
Print the minimum number of multiplications needed to multiply the input matrices on the first line. The answer is a positive integer no greater than . Even the worst order of operations uses no more than multiplications.