Problem 1090. -- English Game
1090: English GameTime Limit: 1 Sec Memory Limit: 128 MB   64bit IO Format: %lld
Submitted: 27 Accepted: 10
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.