#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 ab+ba+ac+ca+ab+baab + ba + ac + ca + ab + ba.

Determine the smallest cost to make a new Paladin spell of exactly a given length.

输入格式

The first line of input contains two integers nn (1n6761 \le n \le 676) and kk (2k1002 \le k \le 100), where nn is the number of rune pairs and kk is the desired spell length in runes (i.e., letters).

Each of the next nn lines contains a string ss (ss consists of exactly two lower-case letters, which may be the same or different) and an integer cc (1c1001 \le c \le 100), where the string ss is a rune pair, and cc 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 kk from the given runes, or 1-1 if it isn’t possible.

5 9
ab 4
ba 1
bd 3
db 100
bc 4
20