#C7326. Maximum Word Score

    ID: 51185 Type: Default 1000ms 256MiB

Maximum Word Score

Maximum Word Score

You are given a list of words and a set of available characters. Your task is to determine the maximum score that can be achieved by forming a valid word from the list using the available characters.

A word is considered valid if each character appears in the available characters with at least the same frequency as it appears in the word.

The score of a word is defined as the square of its length. In mathematical terms, if a word has a length \(L\), its score is given by: $$score = L^2$$.

The input consists of one or more test cases. For each test case, you are given the number of words, the words themselves, and the available characters. The input is terminated by a test case where the number of words is 0.

inputFormat

The input is read from standard input (stdin) and contains multiple test cases. Each test case is formatted as follows:

  • An integer \(n\) denoting the number of words.
  • \(n\) lines, each containing a word.
  • A line containing a string of available characters.

The test cases continue until a line with the integer 0 is read, which signals the end of input.

outputFormat

For each test case, output a single line containing the maximum score achievable from the valid words. The output should be printed to standard output (stdout).

## sample
3
apple
banana
pear
aaplepnbaanr
2
ice
nice
ciniece
0
36

16

</p>