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. oracle直接调用web services

    oracle调用C#开发web services 1,  去oracle官网上下载dbws-callout-utility-10131.zip 地址:https://oracle-base.com/a ...

  2. canvas画圆环%显示

    我: JS代码:  function circleProgress(id,value,average){  var canvas = document.getElementById(id);  var ...

  3. ajax传出数组到后台

    var vote = new Array();    $("input[name='option_name']").each(function(i){        if($(th ...

  4. MySQL开发规范与使用技巧总结

    命名规范 1.库名.表名.字段名必须使用小写字母,并采用下划线分割. a)MySQL有配置参数lower_case_table_names,不可动态更改,Linux系统默认为 0,即库表名以实际情况存 ...

  5. Django 基本使用

    Django 基本使用 Django 安装 pip install django Django 创建项目 django-admin startproject 项目名称 Django 创建应用 pyth ...

  6. rsync同步基本用法

                                                                                                        ...

  7. [NLP] Adaptive Softmax

    1. Overview Adaptive softmax算法在链接1中的论文中提出,该算法目的是为了提高softmax函数的运算效率,适用于一些具有非常大词汇量的神经网络. 在NLP的大部分任务中,都 ...

  8. [UWP]用Win2D实现镂空文字

    1. 前言 之前用PointLight做了一个番茄钟,效果还不错,具体可见这篇文章: [UWP]使用PointLight并实现动画效果 后来试玩了Win2D,这次就用Win2D实现文字的镂空效果,配合 ...

  9. 模块-time模块

    time模块 ​time翻译过来就是时间,这个模块是与时间相关的模块,那么言外之意,如果我们在工作中遇到了对时间的需求(比如获取当前时间,获取时间戳等等)就要先想到time模块. time模块中对于时 ...

  10. PostGIS 用Navicat工具添加自增

    1.新建查询,新增序列(从66开始,每次自增1): CREATE SEQUENCE seq_area INCREMENT 1 MINVALUE 66 MAXVALUE 999999999 START ...