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

Polycarp wants to know answers on qq queries. The jj -th query is described as integer number bjbj . The answer to the query is the minimum number of coins that is necessary to obtain the value bjbj using some subset of coins (Polycarp can use only coins he has). If Polycarp can't obtain the value bjbj , the answer to the jj -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 nn and qq (1≤n,q≤2⋅1051≤n,q≤2⋅105 ) — the number of coins and the number of queries.

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

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

Output

Print qq integers ansjansj . The jj -th integer must be equal to the answer on the jj -th query. If Polycarp can't obtain the value bjbj the answer to the jj -th query is -1.

Example

Input
5 4
2 4 8 2 4
8
5
14
10
Output
1
-1
3
2
题解:有n个数,q次询问,对于要询问的数x,找出数组中能组成x的最小个数,若不能输出-1.这道题很明显是贪心,每一次从最大的值开始判断是否选择。由于数组中的每一个数都是2的幂,所以可以二进制优化的思想进行遍历判断,还需要使用
map容器进行一个映射,来判断当前值是否存在,然后再判断当前值与x的大小即可。
 #include <iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<stack>
#include<queue>
#include<deque>
#include<map>
#include<algorithm>
#define PI acos(-1.0)
using namespace std;
typedef long long ll;
ll m,n,k;
int dp[];
map<int,int>::iterator it;
ll fac(ll x)
{
ll ans=;
for(ll i=;i<=x;i++)
{
ans*=;
}
return ans;
}
int main()
{
cin>>m>>n;
map<ll,ll>mp;
mp.clear();
ll i;
for(i=;i<m;i++)
{
ll p;
cin>>p;
mp[p]++;
}
int str[]={};
for(i=;i<=;i++)
{
str[i]=str[i-]*;
}
while(n--)
{
ll m,k,p,s;
cin>>m;
ll sum=,l=;
while(l>=)
{ k=str[l--];
if(mp.count(k)==)
continue;
p=min(mp[k],m/k);
sum+=p;
m-=p*k; }
if(m)
sum=-;
printf("%lld\n",sum);
}
return ;
}

Coins and Queries(codeforce 1003D)的更多相关文章

  1. CF1003D Coins and Queries 贪心

    Coins and Queries time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  2. CF 1003D Coins and Queries【位运算/硬币值都为2的幂/贪心】

    Polycarp has n coins, the value of the i-th coin is ai. It is guaranteed that all the values are int ...

  3. Codeforces Round #494 (Div. 3) D. Coins and Queries(贪心

    题目链接 题目大意:给你n个物品,第iii个物品价值aia_iai​,询问q次,问你能不能凑出价值为qiq_iqi​的物品. 小贪心吧.从大到小找,能拿就拿就行了. #include<bits/ ...

  4. Coins and Queries(map迭代器+贪心)

    题意 n个硬币,q次询问.第二行给你n个硬币的面值(保证都是2的次幂!).每次询问组成b块钱,最少需要多少个硬币? Example Input 5 42 4 8 2 4851410 Output 1- ...

  5. Codeforces Round #494 (Div. 3) D. Coins and Queries (贪心,数学)

    题意:给你一组全是\(2^d\ (d\ge0)\)的数,询问q次,每次询问一个数,问这个数是否能够由原数组中的数相加得到,如果能,输出最少用多少个数,否则输出\(-1\). 题解:首先贪心得出结论:如 ...

  6. 7.24-Codeforces Round #494 (Div. 3)

    链接:http://codeforces.com/contest/1003 A. Polycarp's Pockets 题型:模拟 题意:把初始集合拆分,要求相同的数不在同一个集合中,求出需要的集合个 ...

  7. codeforces1003D(贪心)

    D. Coins and Queries time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  8. Codeforces Round #494 (Div 3) (A~E)

    目录 Codeforces 1003 A.Polycarp's Pockets B.Binary String Constructing C.Intense Heat D.Coins and Quer ...

  9. ACM java写法入门

    打2017icpc沈阳站的时候遇到了大数的运算,发现java与c++比起来真的很赖皮,竟然还有大数运算的函数,为了以后打比赛更快的写出大数的算法并且保证不错,特意在此写一篇博客, 记录java的大数运 ...

随机推荐

  1. docker 国内加速器配置

    配置镜像加速器 阿里云 登录到 阿里云获取到专属加速地址bqr1dr1n.mirror.aliyuncs.com 找到服务 deamon.js 所在目录C:\ProgramData\docker\co ...

  2. 条款5.了解c++默默编写并且调用了哪些函数。

    如果想在一个内含reference成员的class内支持赋值操作,必须自己定义copy assignment操作符.而且面对“内含有const成员的”class,编译器的反应也是相同的,由于更改con ...

  3. Foundations of Qt Development 学习笔记 Part1 Tips1-50

    1. 信号函数调用的时候仅仅会发送出信号,所以不需要执行 ,所以对于信号声明就行,但是不需要进行定义. 2. 只有槽函数可以声明为public,private,或者是protected的,而信号不行. ...

  4. Python基础学习(第1天)

    写这篇随笔,是看Vamei大大写的Python快速教程的学习笔记,以此自勉,链接:http://www.cnblogs.com/vamei/archive/2012/09/13/2682778.htm ...

  5. 安装nodejs+npm的体验

    NODEJS.NPM安装配置步骤(WINDOWS版本) 1.windows下的NodeJS安装是比较方便的(v0.6.0版本之后,支持windows native),只需要登陆官网(http://no ...

  6. beego配置文件

    关于App配置: #App配置 for Api AppName = ApiService RunMode = dev RouterCaseSensitive = true ServerName = A ...

  7. IDEA导出想要的sql供H2数据库使用

    通过Database连接远程oracle数据库. 选择对应的数据库 双击该数据库需要查询的表,进行自动查询,展示结果. 在查询结果中选择某条数据,右键,选择Data Executer,选择对应的方式. ...

  8. hibernate 多对多(many-to-many)

    多对多(many-to-many):在操作和性能方面都不太理想,所以多对多的映射使用较少,实际使用中最好转换成一对多的对象模型:hibernate会为我们创建中间关联表,转换成两个一对多. 1. E- ...

  9. Windows编程

    本文整理自百科.知乎与 科学家的世界 问题一:为什么开发windows应用程序不用c 而用.net,java,c++? 用 c+windows API  开发windows 应用程序  比用.net, ...

  10. JavaWeb中验证码的实现

    在Web程序中,验证码是经常使用的技术之一.Web程序永远面临未知用户和未知程序的探测.为了防止恶意脚本的执行,验证码技术无疑是首选方案之一.本文将讨论如何在JSP和Servlet中使用验证码技术. ...