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?

Input

The first line contains two integers n and L (1 ≤ n ≤ 30; 1 ≤ 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

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

Example

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

Note

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.

题解:

因为 2n−1×2=2n2n−1×2=2n ,所以我们可以想到小物品组成大物品是否可以带来更小的花费。

于是从小到大扫一遍计算出组成当前大小为 2i2i 所需要的最小花费,记为 aiai 。

然后针对大小 LL ,我们可以将其转换为二进制,从高位往低位开始枚举,

用 nownow 记录已访问的高位中所需要的花费,若当前位为 11 , now+=a[i]now+=a[i] ,因为我们不能通过这一位组合出大小大于 LL 的货物,

若当前位为 00 ,记录 now+a[i]now+a[i] ,因为此时我们只需要将该位填充为 11 即可组出大于 LL 的货物。

然后找最小值即可。

AC代码为:

#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
using ll = long long;
ll cost[50];
int main() 
{
int n, l;
cin >> n >> l;
for (int i = 1; i <= n; ++i)
{
cin >> cost[i];
}

for (int i = 1; i <= 30; ++i) 
{
if (i + 1 > n || cost[i] * 2 < cost[i + 1])
cost[i + 1] = cost[i] * 2;
}

for (int i = 30; i >= 1; --i) 
{
if (cost[i + 1] < cost[i])
cost[i] = cost[i + 1];
}
ll ans = 0;
ll res = 1e18;
for (int i = 30; i >= 0; --i)
{
if (l & (1ll << i))
ans += cost[i + 1];

res = min(ans + cost[i + 1], res);
}
cout << min(res, ans) << endl;
}

CoderForces-913-C的更多相关文章

  1. Discuz!NT 3.9.913 Beta DIY过程

    前提: 论坛的源码版本为dnt_3.9.913_sqlserver_beta.zip,以下例子都以这个版本为原型修改 dnt_3.9.913数据字典:下载 目前(2013年10月21日)官网的asp. ...

  2. [LeetCode] 913. Cat and Mouse 猫和老鼠

    A game on an undirected graph is played by two players, Mouse and Cat, who alternate turns. The grap ...

  3. leetcode 293.Flip Game(lintcode 914) 、294.Flip Game II(lintcode 913)

    914. Flip Game https://www.cnblogs.com/grandyang/p/5224896.html 从前到后遍历,遇到连续两个'+',就将两个加号变成'-'组成新的字符串加 ...

  4. 【LeetCode】913. Cat and Mouse 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 参考资料 日期 题目地址:https://leetc ...

  5. coderforces #387 Servers(模拟)

    Servers time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...

  6. coderforces #384 D Chloe and pleasant prizes(DP)

    Chloe and pleasant prizes time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  7. coderforces 731c

    题目大意:给出m组数据,每组数据包括两个数Li与Ri,分别表示左右袜子的索引(下标),表示这一天要穿的袜子:而我们要使得每天穿的这两只袜子的颜色相同,所以可以改变袜子的颜色,每次只能改变一只袜子的颜色 ...

  8. coderforces 721b

    题目描述: B. Passwords time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  9. UESTC 913 握手 Havel定理+优先队列

    给定一个非负整数序列{dn},若存在一个无向图使得图中各点的度与此序列一一对应,则称此序列可图化.进一步,若图为简单图,则称此序列可简单图化. 此题因为是无自环无重边,所以是简单图.用判定简单图可图化 ...

  10. CoderForces 280B(记忆化搜索)

    题目大意:一个纸牌游戏,52张纸牌排成一列,每张纸牌有面值和花色两种属性.每次操作可以用最后一张纸牌将倒数第二张或者倒数第四张替换,但前提是两张牌的花色或者面值相同.问最终能否只剩一张牌. 题目分析: ...

随机推荐

  1. PHP数组与xml互相转换

    1.数组转xml function arrayToXml($arr) { $xml = "<xml>"; foreach ($arr as $key => $va ...

  2. 发送大数据时,PDU的问题?

    昨天发现通过 Ice发送请求传递一个大块数据时,当请求的体积大于1.2M后,直接抛出异常Connection Lost,对方peer或是断开了.通过防火墙配置排查,以及对同一网络同一机器的php服务p ...

  3. 学习记录:《C++设计模式——李建忠主讲》1.设计模式

    1.学习目标 1)理解松耦合设计思想: 2)掌握面向对象设计原则: 3)掌握重构技法改善设计: 4)掌握GOF核心设计模式: 2.定义 每个设计模式描述了一个在我们周围不断重复发生的问题,以及该问题解 ...

  4. 《Java基础教程》第一章学习笔记

    Java 是什么呀! 计算机语言总的来说分成机器语言,汇编语言,高级语言.其中Java一种高级计算机语言,它是一种可以编写跨平台应用软件,完全面向对象的程序设计语言. Java划分为三个技术平台,Ja ...

  5. Redis为什么是单线程、及高并发快的3大原因详解

    Redis的高并发和快速原因 1.redis是基于内存的,内存的读写速度非常快: 2.redis是单线程的,省去了很多上下文切换线程的时间: 3.redis使用多路复用技术,可以处理并发的连接.非阻塞 ...

  6. 使用lib-flexible.js适配移动端UI设计750px设计图

    最近在和设计沟通关于设计图尺寸大小和前端实际页面尺寸大小不一致的情况,我们的UI设计是使用的iPone6的,(iphone6:    375px*667px  实际像素:750px*1334px)如果 ...

  7. golang开发环境配置

    下载安装 从 https://golang.org/dl/ 下载最新的安装包. windows直接执行exe,按指示进行安装(默认安装目录是:C:\Go) linxu解压后复制到你喜欢目录就行(一般放 ...

  8. 豆瓣 URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:719)>

    import urllib.request as urlrequest #import ssl#ssl._create_default_https_context = ssl._create_unve ...

  9. Session.run() & Tensor.eval()

    如果有一个Tensor t,在使用t.eval()时,等价于: tf.get_defaut_session().run(t) t = tf.constant(42.0) sess = tf.Sessi ...

  10. HTML5的一些验证挺方便的

    一些基本的验证都可以很简单的实现,节省了很多繁琐的步骤.