{"trustable":false,"sections":[{"title":"","value":{"format":"MD","content":"Suppose you have to evaluate an expression like A*B*C*D*E where A,B,C,D and E are matrices.\nSince matrix multiplication is associative, the order in which multiplications are performed is arbitrary.\nHowever, the number of elementary multiplications needed strongly depends on the evaluation order\nyou choose.\nFor example, let A be a 50*10 matrix, B a 10*20 matrix and C a 20*5 matrix. There are two\ndifferent strategies to compute A*B*C, namely (A*B)*C and A*(B*C).\nThe first one takes 15000 elementary multiplications, but the second one only 3500.\nYour job is to write a program that determines the number of elementary multiplications needed\nfor a given evaluation strategy.\n\n##Input\n\nInput consists of two parts: a list of matrices and a list of expressions.\nThe first line of the input file contains one integer n (1 ≤ n ≤ 26), representing the number of\nmatrices in the first part. The next n lines each contain one capital letter, specifying the name of the\nmatrix, and two integers, specifying the number of rows and columns of the matrix.\nThe second part of the input file strictly adheres to the following syntax (given in EBNF):\nSecondPart \u003d Line { Line } \u003cEOF\u003e\nLine \u003d Expression \u003cCR\u003e\nExpression \u003d Matrix | \"(\" Expression Expression \")\"\nMatrix \u003d \"A\" | \"B\" | \"C\" | ... | \"X\" | \"Y\" | \"Z\"\n\n##Output\n\nFor each expression found in the second part of the input file, print one line containing the word ‘error’\nif evaluation of the expression leads to an error due to non-matching matrices. Otherwise print one\nline containing the number of elementary multiplications needed to evaluate the expression in the way\nspecified by the parentheses.\n\n##Sample Input\n\n9\nA 50 10\nB 10 20\nC 20 5\nD 30 35\nE 35 15\nF 15 5\nG 5 10\nH 10 20\nI 20 25\nA\nB\nC\n(AA)\n(AB)\n(AC)\n(A(BC))\n((AB)C)\n(((((DE)F)G)H)I)\n(D(E(F(G(HI)))))\n((D(EF))((GH)I))\n\n##Sample Output\n\n0\n0\n0\nerror\n10000\nerror\n3500\n15000\n40500\n47500\n15125"}}]}