The matrix-chain recurrence (type it)
The matrix-chain recurrence (type it)
Answer
m[i,i] = 0 m[i,j] = min over i <= k < j of m[i,k] + m[k+1,j] + p[i-1]*p[k]*p[j] # fill in order of increasing chain length l = 2..n
For every split k, the cost is the two sub-products plus the final multiply p[i-1]·p[k]·p[j]. The fill order (by chain length) guarantees both sub-costs are already known.