# Cheapest Palindrome

Time Limit: 2000MS

Memory Limit: 65536K

[显示标签]

## Input

Line 1: Two space-separated integers: N and M Line 2: This line contains exactly M characters which constitute the initial ID string Lines 3..N+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.

## Output

Line 1: A single line with a single integer that is the minimum cost to change the given name tag.

## Sample Input

3 4
abcb
a 1000 1100
b 350 700
c 200 800

## Sample Output

900

## Hint

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 = 900, which is the minimum.

## Source

USACO 2007 Open Gold