Given a sequence of matrices, find the most efficient way to multiply these matrices together. The problem is not actually to perform the multiplications, but merely to decide in which order to perform the multiplications. There are many options to multiply a chain of matrices because matrix multiplication is associative i.e. no matter how one parenthesize the product, the result will be the same.

Example:

if you had four matrices A, B, C, and D, you would have:

```
(ABC)D = (AB)(CD) = A(BCD) = ....
```

However, the order in which one parenthesize the product affects the number of simple arithmetic operations needed to compute the product, or the efficiency.

For example:

```
A: 10 × 30 matrix
B : 30 × 5 matrix
C : 5 × 60 matrix
Then,
(AB)C = (10×30×5) + (10×5×60)
= 1500 + 3000
= 4500 operations
A(BC) = (30×5×60) + (10×30×60)
= 9000 + 18000
= 27000 operations.
```

Given an array **arr[]** which represents the chain of matrices such that the ith matrix Ai is of dimension **arr[i-1] x arr[i]**.

Your task is to write a program that should print the optimal way to multiply the matrix chain such that minimum number of multiplications operations are needed to multiply the chain. Represent first matrix as starting Alphabet of the english dictionary i.e. 'A', and the rest so on.

` `**Input: p[] = {40, 20, 30, 10, 30} **
**Output: Optimal parenthesization is ((A(BC))D)**
There are 4 matrices of dimensions 40x20,
20x30, 30x10 and 10x30. Let the input 4
matrices be A, B, C and D. The minimum
number of multiplications are obtained
by putting parenthesis in following way
(A(BC))D --> 20*30*10 + 40*20*10 + 40*10*30
**Input: p[] = {10, 20, 30, 40, 30} **
**Output: Optimal parenthesization is (((AB)C)D)**
There are 4 matrices of dimensions 10x20,
20x30, 30x40 and 40x30. Let the input 4
matrices be A, B, C and D. The minimum
number of multiplications are obtained by
putting parenthesis in following way
((AB)C)D --> 10*20*30 + 10*30*40 + 10*40*30

**Input:**

The first line of the input contains an integer **T**, denoting the number of test cases. Then **T **test case follows. The first line of each test case contains an integer **N**, denoting the number of elements in the array.

Then next line contains **N **space separated integers denoting the values of the element in the array.

**Output:**

For each test case the print the minimum number of operations needed to multiply the chain.

**Constraints:**

1<=T<=100

2<=N<=27

1<=A[]<=500

**Example:
Input:**

2

5

1 2 3 4 5

3

3 3 3

(((AB)C)D)

(AB)

Author: deactjustCalvin

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

thanuvinu94 | 425 |

the_coder95 | 380 |

RishabhTanwar1 | 276 |

shubham3174 | 204 |

RitikRosan | 202 |

the_coder95 | 1226 |

RishabhTanwar1 | 996 |

thanuvinu94 | 634 |

themanhasnoname | 620 |

tathagat289 | 612 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4885 |

Quandray | 4547 |

Login to report an issue on this page.