{"trustable":true,"prependHtml":"\u003cscript\u003e window.katexOptions \u003d { disable: true }; \u003c/script\u003e\n\u003cscript type\u003d\"text/x-mathjax-config\"\u003e\n MathJax.Hub.Config({\n tex2jax: {\n inlineMath: [[\u0027$$$\u0027,\u0027$$$\u0027], [\u0027$\u0027,\u0027$\u0027]],\n displayMath: [[\u0027$$$$$$\u0027,\u0027$$$$$$\u0027], [\u0027$$\u0027,\u0027$$\u0027]]\n }\n });\n\u003c/script\u003e\n\u003cscript async src\u003d\"https://mathjax.codeforces.org/MathJax.js?config\u003dTeX-AMS-MML_HTMLorMML\" type\u003d\"text/javascript\"\u003e\u003c/script\u003e","sections":[{"title":"","value":{"format":"HTML","content":"\u003cdiv class\u003d\"panel_content\"\u003eMatrix multiplication problem is a typical example of dynamical programming. \u003cbr\u003e\u003cbr\u003eSuppose you have to evaluate an expression like A*B*C*D*E where A,B,C,D and E are matrices. Since matrix multiplication is associative, the order in which multiplications are performed is arbitrary. However, the number of elementary multiplications needed strongly depends on the evaluation order you choose.\u003cbr\u003eFor example, let A be a 50*10 matrix, B a 10*20 matrix and C a 20*5 matrix.\u003cbr\u003eThere are two different strategies to compute A*B*C, namely (A*B)*C and A*(B*C).\u003cbr\u003eThe first one takes 15000 elementary multiplications, but the second one only 3500. \u003cbr\u003e\u003cbr\u003eYour job is to write a program that determines the number of elementary multiplications needed for a given evaluation strategy. \u003cbr\u003e\u003c/div\u003e"}},{"title":"Input","value":{"format":"HTML","content":"Input consists of two parts: a list of matrices and a list of expressions.\u003cbr\u003eThe first line of the input file contains one integer n (1 \u0026lt;\u003d n \u0026lt;\u003d 26), representing the number of matrices in the first part. The next n lines each contain one capital letter, specifying the name of the matrix, and two integers, specifying the number of rows and columns of the matrix. \u003cbr\u003eThe second part of the input file strictly adheres to the following syntax (given in EBNF): \u003cbr\u003e\u003cbr\u003eSecondPart \u003d Line { Line } \u0026lt;EOF\u0026gt;\u003cbr\u003eLine \u003d Expression \u0026lt;CR\u0026gt;\u003cbr\u003eExpression \u003d Matrix | \"(\" Expression Expression \")\"\u003cbr\u003eMatrix \u003d \"A\" | \"B\" | \"C\" | ... | \"X\" | \"Y\" | \"Z\"\u003cbr\u003e"}},{"title":"Output","value":{"format":"HTML","content":"For each expression found in the second part of the input file, print one line containing the word \"error\" if evaluation of the expression leads to an error due to non-matching matrices. Otherwise print one line containing the number of elementary multiplications needed to evaluate the expression in the way specified by the parentheses. \u003cbr\u003e"}},{"title":"Sample","value":{"format":"HTML","content":"\u003ctable class\u003d\u0027vjudge_sample\u0027\u003e\n\u003cthead\u003e\n \u003ctr\u003e\n \u003cth\u003eInput\u003c/th\u003e\n \u003cth\u003eOutput\u003c/th\u003e\n \u003c/tr\u003e\n\u003c/thead\u003e\n\u003ctbody\u003e\n \u003ctr\u003e\n \u003ctd\u003e\u003cpre\u003e9\r\nA 50 10\r\nB 10 20\r\nC 20 5\r\nD 30 35\r\nE 35 15\r\nF 15 5\r\nG 5 10\r\nH 10 20\r\nI 20 25\r\nA\r\nB\r\nC\r\n(AA)\r\n(AB)\r\n(AC)\r\n(A(BC))\r\n((AB)C)\r\n(((((DE)F)G)H)I)\r\n(D(E(F(G(HI)))))\r\n((D(EF))((GH)I))\r\n\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003e0\r\n0\r\n0\r\nerror\r\n10000\r\nerror\r\n3500\r\n15000\r\n40500\r\n47500\r\n15125\r\n\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n"}}]}