Data Structures & AlgorithmsVery hardCodeforces Div 2 · ICPC regional

A chain of 4 matrices M1 to M4 has dimensions given by the sequence p = [17, 3, 2, 3, 5], 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. 315
  2. B. 302
  3. C. 459
  4. D. 318

Correct answer: B. 302

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic