Simple matrix chaining dynamic programming