You have a list 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 of , 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 , a block size , and a mode , using bits in total. You do not need to
be concerned with how these values are encoded, just that the
header takes bits. It
is required that , ,
and . The
original subarray for the block can be decompressed from the
block data as follows. The block data elements are always
encoded using bits
each, but how they are encoded depends on the mode . In mode , the block data exactly encodes
the original subarray, i.e., for . In this case we must have because the
elements need to
fit in bits each. In
mode , the block data
encodes a signed offset from the previous original data
element, i.e., for . In this case the are stored in -bit two’s complement form, so we
must have . For the purposes of decoding the first
block, is assumed that .
So, a block with parameters , , and requires bits in total. By
carefully choosing the number of blocks and the values
, , and 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, and , with and . The next
lines contain integers
, with
.
Output
Output the minimum number of bits needed to encode
in the
specified format.
Explanation
In Sample Input ,
the optimal compression splits the data into three blocks.
First, is encoded using
mode with a bit size
of into the block data
, which
uses
bits. The second block encodes the next numbers, using mode
with a bit size of
(since ), which uses bits. The final
block encodes the numbers using mode
and a bit size of
into the block data
,
which uses bits. The three blocks use a total of 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
|