C. Party Lemonade
time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

A New Year party is not a New Year party without lemonade! As usual, you are expecting a lot of guests, and buying lemonade has already become a pleasant necessity.

Your favorite store sells lemonade in bottles of n different volumes at different costs. A single bottle of type i has volume 2i - 1 liters and costs ci roubles. The number of bottles of each type in the store can be considered infinite.

You want to buy at least L liters of lemonade. How many roubles do you have to spend?


The first line contains two integers n and L (1 ≤ n ≤ 301 ≤ L ≤ 109) — the number of types of bottles in the store and the required amount of lemonade in liters, respectively.

The second line contains n integers c1, c2, …, cn (1 ≤ ci ≤ 109) — the costs of bottles of different types.


Output a single integer — the smallest number of roubles you have to pay in order to buy at least L liters of lemonade.

4 12
20 30 70 90
4 3
10000 1000 100 10
4 3
10 100 1000 10000
5 787787787
123456789 234567890 345678901 456789012 987654321

In the first example you should buy one 8-liter bottle for 90 roubles and two 2-liter bottles for 30 roubles each. In total you’ll get 12 liters of lemonade for just 150 roubles.

In the second example, even though you need only 3 liters, it’s cheaper to buy a single 8-liter bottle for 10 roubles.

In the third example it’s best to buy three 1-liter bottles for 10 roubles each, getting three liters for 30 roubles.



  1. Note that for bottle type i, if its cost is larger than twice the cost of bottle type i – 1, we could always buy 2 bottles of type i – 1 instead of 1 bottle of type i. To achieve this, we replace the cost of bottle type i with twice the price of bottle type i – 1 in our cost array c[].
  2. Because of last point, we never need to but more than 1 bottle of  type i. Because we can always buy 1 bottle of type i + 1 and it is guaranteed that the cost will never be higher than twice the cost of type i.
  3. For the previous two reasons, for any L amount of lemonade we need to purchase, we could always have enough lemonade by buying bottle i if the i th bit of L is one. We also need to buy more than one bottle of the n th bottle if the highest non-zero bit of L is beyond n th bit.
  4. Sometimes it is less costly for us to buy more than L amount of lemonade. Let this amount of lemonade be M. There exist a j th bit, for which M’s j th bit is 1 and L’s is 0. Every bit higher than j th is the same in both M and J. Thus, every bit lower than j th in M is zero. Otherwise, we are just buying extra lemonade for no reason.
  5. In the implementation, searching for this j th bit only need O(n). Line 22 strips off the highest i bits. Line 25 considers the case which we buys M amount of lemonade. The (L > 0 ) evaluates to 1 when we have non-zero bits lower than i th bit.  Tourist’s implementation is so awesome.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s