Coins and Queries
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 somenon-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
input

Copy
5 4
2 4 8 2 4
8
5
14
10
output

Copy
1
-1
3
2 题意: 给你n个硬币,每个硬币的价值是2的幂数,然后接下来有m次查询,每次查询问这个数值最少需要多少枚硬币能凑出来? 分析: 我们直接从可能的最大的2的幂数开始枚举,遇到比数值小的就加上,注意每种硬币肯恩有多个,我们加的时候得看所需要加的个数。经过分析可以知道我们要加的是这种硬币的个数和数值所需要的最多这种硬币个数的最小值
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define debug(a) cout << #a << " " << a << endl
using namespace std;
const int maxn = 2e5 + ;
const int mod = 1e9 + ;
typedef long long ll;
int main() {
ll n, m;
while( cin >> n >> m ) {
map<ll,ll> mm;
for( ll i = ; i < n; i ++ ) {
ll a;
cin >> a;
mm[a] ++;
}
while( m -- ) {
ll t;
cin >> t;
ll ans = ;
for( ll i = <<; i >= ; i /= ) {
ans += min( mm[i], t/i );
t -= min( mm[i], t/i ) * i;
}
if( t ) {
cout << - << endl;
} else {
cout << ans << endl;
}
}
}
return ;
}

CF1003D Coins and Queries 贪心的更多相关文章

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

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

  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. 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 ...

  6. $POJ1742\ Coins$ 多重背包+贪心

    Vjudge传送门 $Sol$ 首先发现这是一个多重背包,所以可以用多重背包的一般解法(直接拆分法,二进制拆分法...) 但事实是会TLE,只能另寻出路 本题仅关注“可行性”(面值能否拼成)而不是“最 ...

  7. codeforces1003D(贪心)

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

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

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

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

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

随机推荐

  1. VSTO之PowerPoint(PPT)插件开发常用API汇总

    VSTO简介 VSTO(Visual Studio Tools for Office )是VBA的替代,使得开发Office应用程序更加简单,并且用VSTO来开发office应用程序可以使用Visua ...

  2. spring 的权限控制:security

    下面我们将实现关于Spring Security3的一系列教程. 最终的目标是整合Spring Security + Spring3MVC 完成类似于SpringSide3中mini-web的功能. ...

  3. 解决报错:类型“System.Object”在未被引用的程序集中定义。必须添加对程序集“System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”的引用

    Razor视图引擎中,使用部分视图编译报错 类型“System.Object”在未被引用的程序集中定义.必须添加对程序集“System.Runtime, Version=4.0.0.0, Cultur ...

  4. Drawable 使用详解

    极力推荐文章:欢迎收藏 Android 干货分享 阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android Drawable 是Android 中图像显示的常用方法. 概念:Drawable ...

  5. modbus-tcp协议讲解

    MODBUS功能码简介 代码 中文名称 位操作/字操作 操作数量 01h 读线圈状态 位操作 单个或多个 02h 读离散输入状态(只能读到0或1) 位操作 单个或多个 03h 读保持寄存器(保持寄存器 ...

  6. css3加js做一个简单的3D行星运转效果

    前几天在园子里看到一篇关于CSS3D行星运转的文章,原文在这里,感觉这个效果也太酷炫了,于是自己也就心血来潮的来尝试的做了一下.因为懒得去用什么插件了,于是就原生的JS写,效果有点粗超,还有一些地方处 ...

  7. 爬虫之爬取电影天堂(request)

    #需要通过代码打开https://www.dytt8.net/网站,拿到网站内容 from urllib.request import urlopen #拿到urlopen import re con ...

  8. JAVA-SpringMVC 概述及组件介绍

    一.SpringMVC概述 SpringMVC是一个WEB层.控制层框架,主要用来负责与客户端交互,业务逻辑的调用. SpringMVC是Spring家族中的一大组件,Spring整合SpringMV ...

  9. 100天搞定机器学习|Day33-34 随机森林

    前情回顾 机器学习100天|Day1数据预处理 100天搞定机器学习|Day2简单线性回归分析 100天搞定机器学习|Day3多元线性回归 100天搞定机器学习|Day4-6 逻辑回归 100天搞定机 ...

  10. c# 将dwg文件转化为pdf

    https://blog.csdn.net/mywaster/article/details/50220379 最近做一个项目,要求将dwg文件转化为pdf,开发工具VS2010 + AutoCad ...