This page is still under construction.

Parts of this page are still being built. What you see may change.

Elias Gamma Code

Time limit1sMemory limit128 MB

Summary
Given counts of numbers by binary bit length, choose prefix shifts and optional leading zeros to minimize the total encoded length.
Level

Medium7 of 10

Topics
Dynamic programming, Greedy
Solved
No attempts yet

Problem

The Elias gamma code is a code used to encode a sequence of positive integers. In this problem we use a modified version that can also encode 00.

An integer nn is encoded as follows.

  1. Let kk be the number of bits of nn written in binary. (The number of bits of 00 is taken to be 11.)
  2. Write k−1k-1 zeros, followed by a single 11. (This part is called the prefix.)
  3. Then write nn in binary.

The encodings of the numbers 00 through 88 are shown below.

NumberBinaryBitsPrefixCode
001110
111111
2102010110
3112010111
41003001001100
51013001001101
61103001001110
71113001001111
810004000100011000

To encode a sequence of integers, convert each number to its code as above and concatenate the codes in the order the numbers appear in the sequence.

When decoding a code back to the original number, the prefix in front of the binary representation is essential. While reading the encoded sequence, if you read k−1k-1 zeros before reading a 11, it means the next kk bits are the encoded number.

To shorten the total length of the encoding, consider the following two optimizations.

  1. A prefix normally stands for a fixed bit count kk. If the sequence contains no number whose bit count is exactly kk, this prefix may instead be used to stand for numbers of k+1k+1 bits. If a prefix already stands for k+1k+1 bits, use it for k+2k+2 bits, and so on, shifting up step by step. This shortens the prefix of those numbers.
  2. If you prepend a 00 to every number whose bit count is kk, all of them become numbers of k+1k+1 bits; you can then apply optimization 1. This is effective when there are very few numbers with kk bits but many numbers with more than kk bits.

We want to minimize the total length of the encoded sequence. The actual numbers of the sequence are not given; instead, the count of numbers whose bit count is ii is given as cic_i.

For example, consider c1=2c_1=2, c2=4c_2=4, c3=0c_3=0, c4=1c_4=1. (The sequence 2,1,3,8,0,2,32, 1, 3, 8, 0, 2, 3 is one such case.) Without any optimization the length is 2×(1+1) + 4×(2+2) + 0×(3+3) + 1×(4+4) = 28. Using optimization 1, the prefix 001001 can be used for 44-bit numbers, saving 11 bit. Using optimization 2, prepend a 00 to the 11-bit numbers to make them 22-bit numbers. Applying optimization 1 again, using prefix 11 for 22-bit numbers and prefix 0101 for 44-bit numbers, the length of the encoded string becomes 6×(1+2) + 1×(2+4) = 24.

Both optimizations may be applied multiple times. Write a program that finds the minimum length obtainable by combining the two methods appropriately.

Input

The input consists of several test cases. The first line of each test case contains an integer nn (1≤n≤1281 ≤ n ≤ 128). The second line contains nn values c1c_1 through cnc_n, separated by spaces (0≤ci≤100000 ≤ c_i ≤ 10000). The input ends with a line containing n=0n=0, which should not be processed.

Output

For each test case, print on its own line the minimum possible Elias gamma encoding length for the given sequence.

Examples3

  1. Example 1

    Input
    4
    2 4 0 1
    5
    9 4 2 4 3
    11
    44 56 96 26 73 80 77 50 33 16 78
    0
    
    Expected output
    24
    99
    5494
    
  2. Example 2

    Input
    1
    1
    0
    
    Expected output
    2
    
  3. Example 3

    Input
    1
    0
    0
    
    Expected output
    0