C. Party Lemonade

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.

Examples

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.

题意

有n种不同规格的饮料,容量分别为2i-1L,并给出每种饮料的价格。

现在题目要求至少购买 l升的饮料,最少需要多少钱。

思路

仔细一看,以为是背包,又读了一遍题,貌似是个sb贪心题

但是这个贪心又有点dp的感觉,因为不能只贪最便宜的,每次贪心要对两种状态进行比较,不断地在两种状态间转换

首先按照每个饮料的单价(每升多少钱)升序排序,然后会有两种状态:

  1. 全部买最便宜的那种饮料,直到买的数量大于等于L
  2. 最便宜的饮料买的尽可能的多,但是不要超过L,剩下的部分作为L返回第一步

代码

 1 #include <bits/stdc++.h>
2 #define ll long long
3 #define ull unsigned long long
4 #define ms(a,b) memset(a,b,sizeof(a))
5 const int inf=0x3f3f3f3f;
6 const ll INF=0x3f3f3f3f3f3f3f3f;
7 const int maxn=1e6+10;
8 const int mod=1e9+7;
9 const int maxm=1e3+10;
10 using namespace std;
11 struct wzy
12 {
13 ll bigness;
14 ll money;
15 double price;
16 }p[maxn];
17 bool cmp(wzy u,wzy v)
18 {
19 return u.price<v.price;
20 }
21 int main(int argc, char const *argv[])
22 {
23 #ifndef ONLINE_JUDGE
24 freopen("/home/wzy/in.txt", "r", stdin);
25 freopen("/home/wzy/out.txt", "w", stdout);
26 srand((unsigned int)time(NULL));
27 #endif
28 ios::sync_with_stdio(false);
29 cin.tie(0);
30 int n;
31 ll l;
32 cin>>n>>l;
33 for(int i=1;i<=n;i++)
34 {
35 cin>>p[i].money;
36 p[i].bigness=(1LL<<(i-1));
37 p[i].price=1.0*p[i].money/p[i].bigness;
38 }
39 sort(p+1,p+1+n,cmp);
40 ll L=l;
41 ll ans=INF;
42 ll res1=0,res2=0;
43 while(L>0)
44 {
45 for(int i=1;i<=n;i++)
46 {
47 // 全买这个饮料
48 res1=res2+ceil(1.0*L/p[i].bigness)*p[i].money;
49 ans=min(ans,res1);
50 // 多余的部分买别的
51 res2+=L/p[i].bigness*p[i].money;
52 L-=(p[i].bigness*(L/p[i].bigness));
53 }
54 }
55 ans=min(ans,res2);
56 cout<<ans<<endl;
57 #ifndef ONLINE_JUDGE
58 cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<" s."<<endl;
59 #endif
60 return 0;
61 }

Codeforces 913C:Party Lemonade(贪心)的更多相关文章

  1. Codeforces 913C - Party Lemonade

    913C - Party Lemonade 思路:对于第i个话费cost[i],取min(cost[i],2*cost[i-1]),从前往后更新,这样就可以保证第n个的话费的性价比最高,那么从最高位开 ...

  2. codeforces 704B - Ant Man 贪心

    codeforces 704B - Ant Man 贪心 题意:n个点,每个点有5个值,每次从一个点跳到另一个点,向左跳:abs(b.x-a.x)+a.ll+b.rr 向右跳:abs(b.x-a.x) ...

  3. CodeForces - 913C(二进制)

    链接:CodeForces - 913C 题意:给出 n 瓶饮料的花费 C 数组,每瓶的体积是 2^(i-1) 升,求至少买 L 升的最少花费. 题解:二进制数的组合可以表示任何一个数.第 i 的饮料 ...

  4. CodeForces - 50A Domino piling (贪心+递归)

    CodeForces - 50A Domino piling (贪心+递归) 题意分析 奇数*偶数=偶数,如果两个都为奇数,最小的奇数-1递归求解,知道两个数都为1,返回0. 代码 #include ...

  5. CodeForces - 913C (贪心)

    点完菜,他们发现好像觉得少了点什么? 想想马上就要回老家了某不愿透露姓名的林姓学长再次却陷入了沉思......... 他默默的去前台打算点几瓶二锅头. 他发现菜单上有n 种不同毫升的酒. 第 i 种有 ...

  6. Codeforces 161 B. Discounts (贪心)

    题目链接:http://codeforces.com/contest/161/problem/B 题意: 有n个商品和k辆购物车,给出每个商品的价钱c和类别t(1表示凳子,2表示铅笔),如果一辆购物车 ...

  7. CodeForces 176A Trading Business 贪心

    Trading Business 题目连接: http://codeforces.com/problemset/problem/176/A Description To get money for a ...

  8. Codeforces Gym 100803C Shopping 贪心

    Shopping 题目连接: http://codeforces.com/gym/100803/attachments Description Your friend will enjoy shopp ...

  9. Codeforces 486C Palindrome Transformation(贪心)

    题目链接:Codeforces 486C Palindrome Transformation 题目大意:给定一个字符串,长度N.指针位置P,问说最少花多少步将字符串变成回文串. 解题思路:事实上仅仅要 ...

随机推荐

  1. javaSE高级篇4 — 反射机制( 含类加载器 ) — 更新完毕

    反射机制 1.反射机制是什么?----英文单词是:reflect.在java.lang包下---这才是java最牛逼的技术 首先提前知道一句话----在java中,有了对象,于是有了类,那么有了类之后 ...

  2. Android editttext只能输入不能删除(选中后被软键盘遮住)

    感谢https://www.dutycode.com/post-20.html: 解决方法:在布局外外嵌一层scrollview.

  3. Python计算期权隐含波动率

    更多精彩内容,欢迎关注公众号:数量技术宅,也可添加技术宅个人微信号:sljsz01,与我交流. Black-Scholes 将期权价格描述为标的价格.行权价.无风险利率.到期时间和波动性的函数.  V ...

  4. Netty之Channel*

    Netty之Channel* 本文内容主要参考**<<Netty In Action>> ** 和Netty的文档和源码,偏笔记向. 先简略了解一下ChannelPipelin ...

  5. oracle(创建数据库对象)

    1 --创建数据库 2 --1.SYSDBA系统权限 3 startup:--启动数据库. 4 shutdown:--关闭数据库. 5 alter database[mount]|[open]|[ba ...

  6. InnoDB的行锁模式及加锁方法

    MYSQL:InnoDB的行锁模式及加锁方法 共享锁:允许一个事务度一行,阻止其他事务获取相同数据集的排他锁. SELECT * FROM table_name WHERE ... LOCK IN S ...

  7. HashMap 和 HashSet

    对于HashSet而言,系统采用Hash算法决定集合元素的存储位置,这样可以保证快速存取集合元素: 对于HashMap,系统将value当成key的附属,系统根据Hash算法来决定key的存储位置,这 ...

  8. Project Reactor工厂方法和错误处理

    工厂方法创建流 Backpressure : the ability for the consumer to signal the producer that the rate of emission ...

  9. excel数据导入mySql数据库

    1.将excel数据保存好 2.打开数据库,在表上点击右键,选择导入向导 3.点击下图中红色部门,点击下一步 4.选择excel文件的位置,下方的表空间内,会出现excel中的sheet页,选择要导入 ...

  10. Linux系统的文件复制移动删除与VIM编辑

    目录 今日内容概要 内容详细 复制文件 移动文件 删除文件 系统别名(针对 rm 改别名) vim编辑器 今日内容概要 复制文件 移动文件 删除文件 vim编辑器 内容详细 复制文件 # 命令: cp ...