Minimum-Cost Prefix-Free Language
Time limit1sMemory limit128 MB
Given n and d character costs, find the minimum total cost of a prefix-free set of exactly n words; multiple test cases end with 0 0.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Greedy, Tree, Combinatorics
- Solved
- No attempts yet
Problem
You must build a language of words over an alphabet of characters . The language must be prefix-free: no word may be a prefix of another, i.e. there is no pair of words in which is a prefix of .
Each character has a usage cost . The cost of a word equals the sum of the costs of its characters. For example, if , , , and , then the cost of the word aba is .
The cost of a language equals the sum of the costs of all its words. For example, the language ab, bbb, baaa costs .
Among all prefix-free languages that contain exactly words, find the minimum possible total cost.
When , the single word may be the empty word, whose cost is .
Input
The input contains several test cases. Each test case begins with a line holding two integers and (, ). The next line holds nonnegative integers . The input ends with a line containing two zeros, which is not a test case.
Output
For each test case, output a single line with the minimum cost of a prefix-free language of words built from the given characters.