Let A = .
Call Matrix-Chain-Order then Matrix-Chain-Multiply, defined below.
Matrix-Chain-Multiply(A, s, i, j) if i < j then x = Matrix-Chain-Multiply(A, s, i, s[i,j]) y = Matrix-Chain-Multiply(A, s, s[i,j]+1, j) return Matrix-Multiply(x, y) else return Ai