Polycarp has n coins, the value of the i-th coin is ai. It is guaranteed that all the values are integer powers of 2 (i.e. ai=2d for some non-negative integer number d).

Polycarp wants to know answers on q queries. The j-th query is described as integer number bj. The answer to the query is the minimum number of coins that is necessary to obtain the value bj using some subset of coins (Polycarp can use only coins he has). If Polycarp can't obtain the value bj, the answer to the j-th query is -1.

The queries are independent (the answer on the query doesn't affect Polycarp's coins).

Input

The first line of the input contains two integers n and q (1≤n,q≤2⋅105) — the number of coins and the number of queries.

The second line of the input contains n integers a1,a2,…,an — values of coins (1≤ai≤2⋅109). It is guaranteed that all ai are integer powers of 2 (i.e. ai=2d for some non-negative integer number d).

The next q lines contain one integer each. The j-th line contains one integer bj — the value of the j-th query (1≤bj≤109).

Output

Print q integers ansj. The j-th integer must be equal to the answer on the j-th query. If Polycarp can't obtain the value bj the answer to the j-th query is -1.

Example

Input

5 4

2 4 8 2 4

8

5

14

10

Output

1

-1

3

2

  1. #include<cstdio>
  2. #include<string>
  3. #include<cstdlib>
  4. #include<cmath>
  5. #include<iostream>
  6. #include<cstring>
  7. #include<set>
  8. #include<queue>
  9. #include<algorithm>
  10. #include<vector>
  11. #include<map>
  12. #include<cctype>
  13. #include<stack>
  14. #include<sstream>
  15. #include<list>
  16. #include<assert.h>
  17. #include<bitset>
  18. #include<numeric>
  19. #define debug() puts("++++")
  20. #define gcd(a,b) __gcd(a,b)
  21. #define lson l,m,rt<<1
  22. #define rson m+1,r,rt<<1|1
  23. #define fi first
  24. #define se second
  25. #define pb push_back
  26. #define sqr(x) ((x)*(x))
  27. #define ms(a,b) memset(a,b,sizeof(a))
  28. #define sz size()
  29. #define be begin()
  30. #define pu push_up
  31. #define pd push_down
  32. #define cl clear()
  33. #define lowbit(x) -x&x
  34. #define all 1,n,1
  35. #define rep(i,x,n) for(int i=(x); i<(n); i++)
  36. #define in freopen("in.in","r",stdin)
  37. #define out freopen("out.out","w",stdout)
  38. using namespace std;
  39. typedef long long ll;
  40. typedef unsigned long long ULL;
  41. typedef pair<int,int> P;
  42. const int INF = 0x3f3f3f3f;
  43. const ll LNF = 1e18;
  44. const int N = 1e7 + 20;
  45. const int maxm = 1e6 + 10;
  46. const double PI = acos(-1.0);
  47. const double eps = 1e-8;
  48. const int dx[] = {-1,1,0,0,1,1,-1,-1};
  49. const int dy[] = {0,0,1,-1,1,-1,1,-1};
  50. int dir[4][2] = {{0,1},{0,-1},{-1,0},{1,0}};
  51. const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  52. const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  53. /*
  54. n个硬币,q次询问。第二行给你n个硬币的值(保证都是2的次幂)
  55. 每次询问组成x块钱至少需要多少硬币
  56. */
  57. int n,q;
  58. int a[N]; //之前1e3RE..
  59. map<int,int> m;
  60. int x;
  61. int main()
  62. {
  63. while(~scanf("%d%d",&n,&q))
  64. {
  65. int t;
  66. m.clear();
  67. rep(i,0,n)
  68. {
  69. scanf("%d",&a[i]);
  70. m[a[i]]++;
  71. }
  72. while(q--)
  73. {
  74. int ans = 0;
  75. scanf("%d",&x); //2^30 2^29 ... 2^3=8
  76. for(int i=(1<<30); i>=1; i/=2) //由大向小贪心 枚举硬币值(二次方
  77. {
  78. int t = min(m[i], x/i); //选择需要的个数和有的个数的较小数
  79. ans += t;
  80. x -= t * i;
  81. //cout<<"i="<<i<<" "<<"m[i]="<<m[i]<<" "<<"x/i="<<x/i<<" "<<"t="<<t<<" "<<"ans="<<ans<<" "<<"x="<<x<<endl;
  82. }
  83. if(x)
  84. {
  85. printf("-1\n");
  86. }
  87. else cout<<ans<<endl;
  88. }
  89. }
  90. }
  91. /*
  92. 5 4
  93. 2 4 8 2 4
  94. 8
  95. 5
  96. 14
  97. 10
  98. 1
  99. -1
  100. 3
  101. 2
  102. */

CF 1003D Coins and Queries【位运算/硬币值都为2的幂/贪心】的更多相关文章

  1. 关于C/C++中的位运算技巧

    本篇文章讲述在学习CSAPP位运算LAB时的一些心得. 移位运算的小技巧 C/C++对于移位运算具有不同的策略,对于无符号数,左右移位为逻辑移位,也就是直接移位:对于有符号数,采用算术移位的方式,即左 ...

  2. 【BZOJ3668】[NOI2014] 起床困难综合症(位运算思想)

    点此看题面 大致题意: 给定一些位运算操作,让你在\(0\sim m\)范围内选一个初始值,使其在经过这些运算后得到的结果最大. 前置技能:关于位运算 作为一道位运算的题,如果你不知道什么是位运算,那 ...

  3. Java 位运算超全面总结

    1.原码.反码.补码 关于原码.反码.补码的相关知识作者不打算在这里长篇大论,相关知识已有别的大佬总结很好了,还请老铁自行 Google,不过有篇知乎回答是作者学编程以来见过对相关知识最通俗易懂,生动 ...

  4. Coins and Queries(codeforce 1003D)

    Polycarp has nn coins, the value of the i-th coin is aiai . It is guaranteed that all the values are ...

  5. JAVA:二进制(原码 反码 补码),位运算,移位运算,约瑟夫问题(5)

    一.二进制,位运算,移位运算 1.二进制 对于原码, 反码, 补码而言, 需要注意以下几点: (1).Java中没有无符号数, 换言之, Java中的数都是有符号的; (2).二进制的最高位是符号位, ...

  6. Java学习第五篇:二进制(原码 反码 补码),位运算,移位运算,约瑟夫问题

    一.二进制,位运算,移位运算 1.二进制 对于原码, 反码, 补码而言, 需要注意以下几点: (1).Java中没有无符号数, 换言之, Java中的数都是有符号的; (2).二进制的最高位是符号位, ...

  7. Vus the Cossack and Strings(Codeforces Round #571 (Div. 2))(大佬的位运算实在是太强了!)

    C. Vus the Cossack and Strings Vus the Cossack has two binary strings, that is, strings that consist ...

  8. Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range

    在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...

  9. 简简单单学会C#位运算

    一.理解位运算 要学会位运算,首先要清楚什么是位运算?程序中的所有内容在计算机内存中都是以二进制的形式储存的(即:0或1),位运算就是直接对在内存中的二进制数的每位进行运算操作 二.理解数字进制 上面 ...

随机推荐

  1. 【bzoj3362/3363/3364/3365】[Usaco2004 Feb]树上问题杂烩 并查集/树的直径/LCA/树的点分治

    题目描述 农夫约翰有N(2≤N≤40000)个农场,标号1到N,M(2≤M≤40000)条的不同的垂直或水平的道路连结着农场,道路的长度不超过1000.这些农场的分布就像下面的地图一样, 图中农场用F ...

  2. Hibernate技术入门

    ORM原理 ORM(Object Relational Mapping)是对象到关系的映射,它的作用是在关系数据库和对象之间做一个自动映射,将数据库中的数据表映射成对象(持久化类),对关系型数据库以对 ...

  3. ExtJS 4.1 TabPanel动态加载页面并执行脚本【转】

    ExtJS 4.1 TabPanel动态加载页面并执行脚本 按照官方示例,可以动态加载页面,可是脚本不执行,于是查SDK.google,发现scripts需要设置为true,于是设置该属性,整个代码如 ...

  4. [POJ2777] Count Color

    \[Count Color\] Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 50865 Accepted: 15346 Des ...

  5. C. Annoying Present SB题

    C. Annoying Present time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  6. 使用eclipse插件创建一个web project

    使用eclipse插件创建一个web project 首先创建一个Maven的Project如下图 我们勾选上Create a simple project (不使用骨架) 这里的Packing 选择 ...

  7. java字符串 64位编码

    byte[] encodeBase64 = Base64.encodeBase64("到了是是是是".getBytes("UTF-8")); System.ou ...

  8. Spring - IoC(9): @Resoure & @Autowired

    @Resource 和 @Autowired 都是用来装配依赖的,它们之间有些异同. @Resoure @Resource 是 JSR-250 规范的注解. @Resource 可以标注在字段.方法上 ...

  9. Web.xml过滤器配置及执行顺序概念

    第一个过滤器 @Overridepublic void doFilter(ServletRequest request, ServletResponse response,FilterChain ch ...

  10. 【洛谷 P1363】幻想迷宫(搜索)

    这题其实可以很简单. 题目叫做"幻想迷宫",那么我们就幻想一个迷宫. 借用一下@FancyDreams的图片 只有左上角第一个\(5*4\)的迷宫是真的, 其他都是我们幻想出来的. ...