CF700D Huffman Coding on Segment
Description
Alice wants to send an important message to Bob. Message $ a=(a_{1},...,a_{n}) $ is a sequence of positive integers (characters).
To compress the message Alice wants to use binary Huffman coding. We recall that binary Huffman code, or binary prefix code is a function $ f $ , that maps each letter that appears in the string to some binary string (that is, string consisting of characters '0' and '1' only) such that for each pair of different characters $ a_{i} $ and $ a_{j} $ string $ f(a_{i}) $ is not a prefix of $ f(a_{j}) $ (and vice versa). The result of the encoding of the message $ a_{1},a_{2},...,a_{n} $ is the concatenation of the encoding of each character, that is the string $ f(a_{1})f(a_{2})...\ f(a_{n}) $ . Huffman codes are very useful, as the compressed message can be easily and uniquely decompressed, if the function $ f $ is given. Code is usually chosen in order to minimize the total length of the compressed message, i.e. the length of the string $ f(a_{1})f(a_{2})...\ f(a_{n}) $ .
Because of security issues Alice doesn't want to send the whole message. Instead, she picks some substrings of the message and wants to send them separately. For each of the given substrings $ a_{li}...\ a_{ri} $ she wants to know the minimum possible length of the Huffman coding. Help her solve this problem.
Input Format
N/A
Output Format
N/A
Explanation/Hint
In the first query, one of the optimal ways to encode the substring is to map $ 1 $ to "0", $ 2 $ to "10" and $ 3 $ to "11".
Note that it is correct to map the letter to the empty substring (as in the fifth query from the sample).