#P4829. 2048‐Style Block Merging
2048‐Style Block Merging
2048‐Style Block Merging
kls has been playing a game similar to 2048. Initially there are n blocks; each block has an integer between 1 and m. kls is allowed to perform two kinds of operations as many times as desired:
- Merging: Choose any two blocks (not necessarily adjacent) that hold the same number x and merge them into a single block with number 2x.
- Decrease: Decrease the number on any block (you may reduce it to any positive integer not exceeding its current value).
The final score is defined as the maximum number present on any block after you finish performing operations. Note that merging preserves the sum of the two blocks if no decreasing is needed; however, if the numbers are not equal you may decrease the larger one before merging – at the cost of some potential value. In other words, if you merge two blocks with values x and y (with x ≤ y) by first reducing y to x, the merge produces a block with value 2x (losing y − x in the process).
Your task is to help kls determine the maximum possible final score. In an optimal strategy you may choose, for each block, an integer not exceeding its initial value. Then, by merging blocks losslessly (i.e. without any unnecessary reduction) you can obtain a final block whose value is exactly the sum of the numbers you decided on. However, because merging is only possible when two blocks have exactly the same number, sometimes you must deliberately choose a lower number for some blocks in order to allow a sequence of merges that preserves the total sum. For example, if the three blocks are initially capable of being set to 1, 1, and 2 then by first merging the two blocks with 1 into 2 (since 1 = 1) and then merging the resulting 2 with the block set to 2 (no decrease needed) you obtain a final block with 4. On the other hand, if all blocks are small (for example, all 1’s) you cannot build an arbitrarily large number by merging; indeed, if all blocks are 1’s the maximum final number equals the largest power of 2 that does not exceed the number of blocks used in an optimal merge.
More formally, given n blocks with capacities a1, a2, …, an (each between 1 and m), you may assign to each block an integer bi with 1 ≤ bi ≤ ai. Then, using the merging operation (merging only blocks that are equal, and possibly reducing one block so that they match) you wish to combine some blocks without “loss” (i.e. without decreasing the sum more than absolutely necessary) in order to maximize the value of the final merged block. It can be shown that an optimal strategy will effectively use a grouping technique: one group of blocks will be assigned a common base value x while one block in the merge sequence is "boosted" (by not being reduced) so that when merged with a block coming from a pair you eventually get a result of the form 2k * x for some k ≥ 0. Your task is to compute the maximum possible final value that can be achieved by first choosing the numbers bi (each ≤ ai) and then merging them optimally.
Input constraints and note: It is guaranteed that an optimal strategy exists and that the answer can be obtained by a careful selection of values and merge order. The operations are allowed in any order and any number of times.
inputFormat
The first line contains two integers n and m (1 ≤ n ≤ 105, 1 ≤ m ≤ 109), representing the number of blocks and the maximum initial value respectively. The second line contains n integers: a1, a2, …, an (1 ≤ ai ≤ m), where ai is the value on the i-th block.
You may assume that the input is given in a format suitable for fast I/O.
outputFormat
Output a single integer, the maximum possible final score that can be attained after performing an optimal sequence of operations.
sample
3 2
1 1 2
4