{"trustable":true,"sections":[{"title":"","value":{"format":"HTML","content":"\u003cp\u003eKeeping track of all the cows can be a tricky task so Farmer John has installed a system to automate it. He has installed on each cow an electronic ID tag that the system will read as the cows pass by a scanner. Each ID tag\u0027s contents are currently a single string with length \u003ci\u003eM\u003c/i\u003e (1 ≤ \u003ci\u003eM\u003c/i\u003e ≤ 2,000) characters drawn from an alphabet of \u003ci\u003eN\u003c/i\u003e (1 ≤ \u003ci\u003eN\u003c/i\u003e ≤ 26) different symbols (namely, the lower-case roman alphabet).\u003c/p\u003e\u003cp\u003eCows, being the mischievous creatures they are, sometimes try to spoof the system by walking backwards. While a cow whose ID is \"abcba\" would read the same no matter which direction the she walks, a cow with the ID \"abcb\" can potentially register as two different IDs (\"abcb\" and \"bcba\").\u003c/p\u003e\u003cp\u003eFJ would like to change the cows\u0027s ID tags so they read the same no matter which direction the cow walks by. For example, \"abcb\" can be changed by adding \"a\" at the end to form \"abcba\" so that the ID is palindromic (reads the same forwards and backwards). Some other ways to change the ID to be palindromic are include adding the three letters \"bcb\" to the begining to yield the ID \"bcbabcb\" or removing the letter \"a\" to yield the ID \"bcb\". One can add or remove characters at any location in the string yielding a string longer or shorter than the original string.\u003c/p\u003e\u003cp\u003eUnfortunately as the ID tags are electronic, each character insertion or deletion has a cost (0 ≤ \u003ci\u003ecost\u003c/i\u003e ≤ 10,000) which varies depending on exactly which character value to be added or deleted. Given the content of a cow\u0027s ID tag and the cost of inserting or deleting each of the alphabet\u0027s characters, find the minimum cost to change the ID tag so it satisfies FJ\u0027s requirements. An empty ID tag is considered to satisfy the requirements of reading the same forward and backward. Only letters with associated costs can be added to a string.\u003c/p\u003e"}},{"title":"Input","value":{"format":"HTML","content":"Line 1: Two space-separated integers: \u003ci\u003eN\u003c/i\u003e and \u003ci\u003eM\u003c/i\u003e\r\u003cbr\u003eLine 2: This line contains exactly \u003ci\u003eM\u003c/i\u003e characters which constitute the initial ID string\r\u003cbr\u003eLines 3..\u003ci\u003eN\u003c/i\u003e+2: Each line contains three space-separated entities: a character of the input alphabet and two integers which are respectively the cost of adding and deleting that character."}},{"title":"Output","value":{"format":"HTML","content":"Line 1: A single line with a single integer that is the minimum cost to change the given name tag."}},{"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\u003e3 4\r\nabcb\r\na 1000 1100\r\nb 350 700\r\nc 200 800\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003e900\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n"}},{"title":"Hint","value":{"format":"HTML","content":"If we insert an \"a\" on the end to get \"abcba\", the cost would be 1000. If we delete the \"a\" on the beginning to get \"bcb\", the cost would be 1100. If we insert \"bcb\" at the begining of the string, the cost would be 350 + 200 + 350 \u003d 900, which is the minimum."}}]}