Dynamic programming with matrix multiplication.