This English game is a simple English words connection game.
The rules are as follows: there are N English words in a dictionary, and every word has its own weight v. There is a weight if the corresponding word is used. Now there is a target string X. You have to pick some words in the dictionary, and then connect them to form X. At the same time, the sum weight of the words you picked must be the biggest.
There are several test cases. For each test, N (1<=n<=1000) and X (the length of x is not bigger than 10000) are given at first. Then N rows follow. Each row contains a word wi (the length is not bigger than 30) and the weight of it. Every word is composed of lowercases. No two words in the dictionary are the same.
For each test case, output the biggest sum weight, if you could not form the string X, output -1.
1 aaaa a 2 3 aaa a 2 aa 5 aaa 6 4 abc a 1 bc 2 ab 4 c 1 3 abcd ab 10 bc 20 cd 30 3 abcd cd 100 abc 1000 bcd 10000应当输出
8 7 5 40 -1
欢迎加入我的知识星球「基你太美」，我会在星球中分享 AucFrame 框架、大厂面经、AndroidUtilCode 更详尽的说明…一切我所了解的知识，你可以通过支付进入我的星球「基你太美」进行体验，加入后优先观看星球中精华的部分，如果觉得星球的内容对自身没有收益，你可以自行申请退款退出星球，也没必要加我好友；如果你已确定要留在我的星球，可以通过扫描如下二维码（备注：基你太美+你的星球昵称）加我个人微信，方便我后续拉你进群(PS：进得越早价格越便宜)。