Hari Merdeka
Time limit3sMemory limit256 MB
Choose a string over priced letters with total cost within the budget to maximize the summed scores of all occurrences of the given words.
- Level
Hard8 of 10
- Topics
- Dynamic programming, String matching
- Solved
- No attempts yet
Problem
Malaysia's Independence Day is called Hari Merdeka. It falls on 31 August every year and marks the independence of the Federation of Malaya from British colonial rule in 1957. Through the whole month of August many Malaysians raise the national flag on the balcony of their home and on every vehicle they own, baby strollers included. At the celebration people shout "Merdeka!" seven times.
For next year's Independence Day, IIUM will hang a long banner around its main campus. The organising committee wants to write words on the banner that encourage the students and the professors. Two rules apply. All words go on one single line with no spaces between them. Words are allowed to overlap each other. For example, WORDER contains the words WORD and ORDER. Whatever ends up on the banner is called the text.
The committee compiled a list of words and gave each word a score. The score of the text is the sum of the score of each word once for every occurrence of that word in the text. If WORD is worth 5 and ORDER is worth 8, then the text WORDER scores . In the text WORDWORDER the word WORD occurs twice, so it scores .
The committee also wants every character in the text handcrafted. Crafting one character costs money and the committee's budget is fixed.
Find the text with the highest score whose cost stays within the budget. Do not print the text itself, only its score.
Input
The first line contains an integer , the number of cases. ()
Each case starts with a line holding three integers , and : the number of characters, the number of words, and the committee's budget. (, , )
The next lines each contain one character from A to Z and one integer , the cost of writing one in the text. () All are distinct.
The following lines each contain one word and one integer , the score of that word. (, )
Every character used in the words appears among the . The text may use only those characters, and the cost of the text is the sum of the cost of every character written in it.
Output
For each case print one line in the form Case #X: Y, where is the case number starting from 1 and is the highest score obtainable in that case. If no text within the budget contains any word, print 0.
Hint
In the first case of the first example the best text is AAAAA. It contains AA four times and AAA three times, so the score is . Writing AAAAA needs five copies of A at a cost of 2 each, so the total cost is 10.