#P16022. [ICPC 2021 NAC] The Paladin
[ICPC 2021 NAC] The Paladin
题目描述
A Paladin, a warrior adept at holy magic, needs your help forming spells. Being a Paladin, every spell must also be a Palindrome, meaning that it is the same string when read forwards and backwards. The cost of constructing a new spell is based on rune pair costs (costs of adjacent letters) that occur in the final spell. Rune pairs not presented in the input are not allowed in the final spell.
The total cost of a spell is the sum of all rune pair costs in the spell for each time the pair occurs in the spell. For example, if the spell is abacaba, then the cost is the sum of the costs of .
Determine the smallest cost to make a new Paladin spell of exactly a given length.
输入格式
The first line of input contains two integers () and (), where is the number of rune pairs and is the desired spell length in runes (i.e., letters).
Each of the next lines contains a string ( consists of exactly two lower-case letters, which may be the same or different) and an integer (), where the string is a rune pair, and is the cost of that rune pair. All rune pairs are distinct.
输出格式
Output a single integer, which is the smallest possible cost for constructing a spell of length from the given runes, or if it isn’t possible.
5 9
ab 4
ba 1
bd 3
db 100
bc 4
20