Hide

Problem A
Compression

You have a list x1,,xN of natural numbers and would like to encode it in a particular compressed format while minimizing the size of the compressed file. The compressed format consists of a string of bits (0 or 1), which is divided into contiguous blocks of variable length. Each block encodes a subarray xi,,xj of x1,,xN, and when concatenated, the blocks encode the full list.

The encoding of each block consists of a header and some data. The header stores three parameters: a bit size s, a block size k, and a mode m, using c bits in total. You do not need to be concerned with how these values are encoded, just that the header takes c bits. It is required that 0s30, 1kN, and m{1,2}. The original subarray xi,,xi+k1 for the block can be decompressed from the block data as follows. The block data elements yi,,yi+k1 are always encoded using s bits each, but how they are encoded depends on the mode m. In mode 1, the block data exactly encodes the original subarray, i.e., xj=yj for j=i,,i+k1. In this case we must have 0yj<2s because the elements yj need to fit in s bits each. In mode 2, the block data encodes a signed offset from the previous original data element, i.e., xj=xj1+yj for j=i,,i+k1. In this case the yj are stored in s-bit two’s complement form, so we must have 2s1yj<2s1. For the purposes of decoding the first block, is assumed that x0=0.

So, a block with parameters s, k, and m requires c+sk bits in total. By carefully choosing the number of blocks and the values s, k, and m for each block, you can minimize the number of bits needed to store the list in this compressed format.

Input

The first line contains two integers, N and c, with 1N105 and 0c103. The next N lines contain integers x1,xN, with 0xi109.

Output

Output the minimum number of bits needed to encode x1,,xN in the specified format.

Explanation

In Sample Input 3, the optimal compression splits the data into three blocks. First, 0,1,3,6,10,15,21,28 is encoded using mode 2 with a bit size of 4 into the block data 0,1,2,3,4,5,6,7, which uses 16+48=48 bits. The second block encodes the next 7 numbers, 1036,0,1035,1,1034,2,1033 using mode 1 with a bit size of 11 (since 1036<211), which uses 16+117=93 bits. The final block encodes the numbers 1045,1055,1066,1078,1060,1091 using mode 2 and a bit size of 6 into the block data 12,10,11,12,18,31, which uses 16+66=52 bits. The three blocks use a total of 48+93+52=193 bits.

Sample Input 1 Sample Output 1
7 5
1
2
3
4
5
3
1
19
Sample Input 2 Sample Output 2
8 3
0
99
3
2
2
2
2
2
23
Sample Input 3 Sample Output 3
21 16
0
1
3
6
10
15
21
28
1036
0
1035
1
1034
2
1033
1045
1055
1066
1078
1060
1091
193
Hide

Please log in to submit a solution to this problem

Log in