CodeForces - 1003D
Polycarp has nn coins, the value of the ii-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 jj-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
5 4
2 4 8 2 4
8
5
14
10
1
-1
3
2
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<deque>
#include<map>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0);
const double e=exp();
map<long long,long long> mp;
int main()
{
LL i,p,j,n,q;
LL ans;
scanf("%lld%lld",&n,&q);
for(i=;i<=n;i++)
{
scanf("%lld",&p);
mp[p]++;
}
while(q--)
{
LL x,mid;
ans=;
scanf("%lld",&x);
for(i=<<;i>=;i/=)
{
mid=min(mp[i],x/i);
ans+=mid;
x-=mid*i;
}
if(x!=)
printf("-1\n");
else
printf("%lld\n",ans);
}
return ;
}
CodeForces - 1003D的更多相关文章
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
- CodeForces - 696B Puzzles
http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
- CodeForces - 148D Bag of mice
http://codeforces.com/problemset/problem/148/D 题目大意: 原来袋子里有w只白鼠和b只黑鼠 龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老鼠谁就赢. 王妃每次 ...
随机推荐
- n元一维向量向左循环移位i的几种算法
1.最简单的算法借助于一个n元的中间向量在n步时间内完成 时间复杂度:O(n) 空间复杂度O(n) void shift_easy(int arr[], int _arr[], int n, int ...
- 【第五周】四则运算GUI
这次这个简陋的程序终于发布了,其实发布很简单(在windows平台),因为使用的是vs2008+qt4.7的组合,在微软自家平台上用一用还是很方便的,只需要在release编译生成的exe文件,加上几 ...
- 【BioCode】读文件夹以发现缺失文件
代码说明: 使用单个蛋白质的txt计算PSSM生成的结果为单个的PSSM文件. 但是由于一些原因(如蛋白质序列过长),会导致一些蛋白质txt文件无法计算出pssm,为了找到这些没有计算出pssm的蛋白 ...
- mac下面安装Navicat Premium 12.0.24 for mac已破解中文亲测可用
链接:https://pan.baidu.com/s/17HQ7dsCun2cSJGRpdP9yXA 密码:hwq5 Navicat_Premium_mac 最新版 12.0.24(原版是英文的) ...
- 从理论到实践,全方位认识DNS
从理论到实践,全方位认识DNS 2015-11-23 程序员之家 作者:selfboot 原文:http://segmentfault.com/a/1190000003956853 对于 DNS(Do ...
- PHP中对象的传值方式
对象的传值方式: 为什么对于对象,值传递和引用传递,这个情况下,他们似乎没有区别??? 这要从对象的数据的存储方式来理解: $o1 = new C1(); //这里创建一个对象o1,其存储结果如图所示 ...
- Viewpoint Meta标签
移动web Viewpoint常用得设置方式: [布局viewpoint] = [设备宽度] = [度量viewpoint] <meta name="viewport" co ...
- python OCR 图形识别
1.pip install pyocr 2.pip install PIL 3.安装tesseract-ocr http://jaist.dl.sourceforge.net/project/tess ...
- java 常量 jvm在编译期能确定常量的 所以相等 但不能确定变量的范围 所以变量相加会产生新的
- AFNetworking你最最最起码要知道的基本使用~
AFNetworking是一个在iOS开发中,使用非常多的一个开源库 适用于iOS以及Mac OS X, 它构建于在(Apple iOS开发文档)NSURLConnection, NSOperatio ...