Funny Language
Time limit1sMemory limit128 MB
Choose n new words (avoiding the given m words) to maximize the total count of formable-subword matches across all given words, using their letter multisets.
- Level
Hard8 of 10
- Topics
- Combinatorics, Greedy, Math
- Solved
- No attempts yet
Problem
There is a well-known word game. Given a word, you may form other words using only the letters of that word, where each letter may be used at most as many times as it appears in the original word; the order of the letters does not matter. For example, from the word CONTEST you can form NOTE, NET, ON, TEST, SET, and so on.
You are compiling a new dictionary and may add exactly brand-new words to it. You already know the words that you will later play this game with. Choose a set of exactly distinct non-empty words such that no chosen word equals any , in order to maximize
where is the set of chosen words that can be formed from the letters of .
Because many different sets may reach the maximum (and any word may be freely reordered), report only the optimal value of this sum — the largest total number of formable words — rather than the words themselves.
Input
The first line contains two integers and (, ): the number of new words you may add and the number of game words. Each of the next lines contains one word consisting of at most uppercase letters from A to Z.
Output
Print a single integer: the maximum possible value of .