Scribble
InterviewTime limit1sMemory limit128 MB
Given seven tiles with letter values and a dictionary of up to 100000 words, find the highest-scoring dictionary word formable from the tiles, or 0 if none is.
- Level
Medium5 of 10
- Topics
- String, Hash map, Brute force, Implementation
- Solved
- No attempts yet
Problem
Nixed, he placed the flong into the calathi halfway through the yuga.
Huh?
Believe it or not, the sentence above is a perfectly valid English sentence. It also has two other qualities: it looks like spam, and its words are very valuable.
Valuable, you say? (For some reason, you keep talking to yourself today.)
Yes — valuable, if you are playing Scribble. In the standard game of Scribble, calathi ("a vase-shaped basket depicted in Greek painting and sculpture") is worth points, nixed ("refused") is worth points, flong ("a compressed mass of paper sheets forming a matrix or mold for stereotype plates") is worth points, and yuga ("any one of the four ages — Krita or Satya, Treta, Dwapara, and Kali — into which Hindu tradition divides the existence of the world") is worth points.
As you may know, each letter in Scribble is worth a fixed number of points, and the goal is to score as many points as possible from a given set of letters.
For this problem we change the rules slightly. You have tiles (letters), and each letter has a score with . Before you play, you may also consult a dictionary of valid words (unlike normal Scribble). Your task is to find the highest-scoring word you can form. The score of a word is the sum of the scores of its letters.
Input
The first line contains an integer (). Each of the next lines contains a triple , where is a letter, is that letter's score, and is the number of tiles bearing that letter. You may assume . For example, the triple a 7 2 means you have two a tiles, each worth points. The next line (line ) contains an integer (). Each of the following lines contains one word (each word has length at least one).
Output
Output a single line containing one integer: the maximum score — the greatest number of points obtainable by using your tiles to form one complete word from the dictionary. If no word can be formed, output .