Data Structures & AlgorithmsVery hardCodeforces Div 2 · ICPC regional

A chain of 5 matrices M1 to M5 has dimensions given by the sequence p = [8, 7, 16, 17, 9, 9], where Mi has size p[i−1] × p[i] (1-indexed). Multiplying two matrices of sizes a×b and b×c costs a·b·c scalar multiplications. What is the minimum total number of scalar multiplications over all parenthesisations?

  1. A. 4944
  2. B. 5673
  3. C. 5337
  4. D. 4046

Correct answer: D. 4046

The interval DP gives 4046. Multiplying strictly left to right costs 4944; the greedy 'cheapest adjacent pair first' rule gives 5673, which is not optimal.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic