Inverting Huffman

No attempts yetTime limit1sMemory limit128 MB

Problem

Static Huffman coding is an encoding algorithm used mainly for text compression. Given a text made of NN different characters, the algorithm chooses NN codes, one for each different character, and the text is compressed with those codes. To choose the codes, the algorithm builds a binary rooted tree having NN leaves. For N2N \ge 2 the tree is built as follows.

  1. For each different character in the text build a tree containing just a single node, and assign to it a weight equal to the number of occurrences of the character within the text.
  2. Build a set ss containing the above NN trees.
  3. While ss contains more than one tree:
    1. Choose t1st_1 \in s with minimum weight and remove it from ss.
    2. Choose t2st_2 \in s with minimum weight and remove it from ss.
    3. Build a new tree tt with t1t_1 as its left subtree and t2t_2 as its right subtree, and assign to tt the sum of the weights of t1t_1 and t2t_2.
    4. Include tt into ss.
  4. Return the only tree that remains in ss.

At steps 3.1 and 3.2 the set ss may contain several trees with minimum weight, so the text alone does not fix the shape of the tree. In the text abracadabra the character a occurs 5 times, b and r occur twice each, and c and d occur once each. One run of the algorithm gives code lengths 1, 2, 3, 4, 4 for a, b, r, c, d, and another run gives 1, 3, 3, 3, 3.

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 the same as the number of internal nodes in the path.

Given the lengths of the NN codes chosen by the algorithm, find the minimum size (total number of characters) that the text can have so that the generated codes have those NN lengths.

Input

The first line contains an integer NN, the number of different characters that appear in the text. (2N502 \le N \le 50)

The second line contains NN integers LiL_i, the lengths of the codes the algorithm chose for the different characters. (1Li501 \le L_i \le 50, i=1,2,,Ni = 1, 2, \dots, N)

At least one tree built as described above produces codes with the given lengths.

Output

Print one line with an integer, the minimum size (total number of characters) that the text can have so that the generated codes have the given lengths.