eolymp
bolt
Попробуйте наш новый интерфейс для отправки задач
Задачи

Inverting Huffman

Inverting Huffman

Static Huffman coding is an encoding algorithm used mainly for text compression. Given a text of certain size made of \textbf{N} different characters, the algorithm chooses \textbf{N} codes, one for each different character. The text is compressed using these codes. To choose the codes, the algorithm builds a binary rooted tree having \textbf{N} leaves. For \textbf{N} ≥ \textbf{2} the tree can be built as follows. \begin{enumerate} \item For each different character in the text build a tree containing just a single node, and assign to it a weight coincident with the number of occurrences of the character within the text. \item Build a set s containing the above \textbf{N} trees. \item While s contains more than one tree: \begin{itemize} \item (a) Choose \textbf{t_1} \includegraphics{https://static.e-olymp.com/content/a0/a01d6fe5baf86fd274dfc5600a30f76cc672dd3c.jpg} \textbf{s} with minimum weight and remove it from \textbf{s}. \item (b) Choose \textbf{t_2} \includegraphics{https://static.e-olymp.com/content/a0/a01d6fe5baf86fd274dfc5600a30f76cc672dd3c.jpg} \textbf{s} with minimum weight and remove it from \textbf{s}. \item (c) Build a new tree \textbf{t} with \textbf{t_1} as its left subtree and \textbf{t_2} as its right subtree, and assign to t the sums of the weights of \textbf{t_1} and \textbf{t_2}. \item (d) Include \textbf{t} into \textbf{s}. \end{itemize} \item Return the only tree that remains in \textbf{s}. \end{enumerate} For the text "\textbf{abracadabra}", the tree produced by the above procedure can look like the one on the left of the following picture, where each internal node is labeled with the weight of the subtree rooted at that node. Notice that the obtained tree can also look like the one on the right of the picture, among others, because at steps \textbf{3(a)} and \textbf{3(b)} the set \textbf{s} may contain several trees with minimum weight. \includegraphics{https://static.e-olymp.com/content/53/53a6ca8694d656d2d80d03567daad4f938fd4ad1.jpg} For each different character in the text, its code depends on the path that exists, in the final tree, from the root to the leaf corresponding to the character. The length of the code is the number of edges in that path (which is coincident with the number of internal nodes in the path). Assuming the tree on the left was built by the algorithm, the code for "\textbf{r}" has length \textbf{3} while the code for "\textbf{d}" has length \textbf{4}. Your task is, given the lengths of the \textbf{N} codes chosen by the algorithm, find the minimum size (total number of characters) that the text can have so as the generated codes have those \textbf{N} lengths. \InputFile The first line contains an integer \textbf{N} (\textbf{2} ≤ \textbf{N} ≤ \textbf{50}) representing the number of different characters that appear in the text. The second line contains \textbf{N} integers \textbf{L_i} indicating the lengths of the codes chosen by Huffman algorithm for the different characters (\textbf{1} ≤ \textbf{L_i} ≤ \textbf{50} for \textbf{i = 1}, \textbf{2}, ..., \textbf{N}). You may assume that there exists at least one tree, built as described, that produces codes with the given lengths. \OutputFile Output a line with an integer representing the minimum size (total number of characters) that the text can have so as the generated codes have the given lengths.
Лимит времени 1 секунда
Лимит использования памяти 64 MiB
Входные данные #1
10
8 2 4 7 5 1 6 9 3 9
Выходные данные #1
89
Автор Leopoldo Taravilse
Источник ACM ICPC Regional Latino America 2013