ATMs of a well-known bank of a small country are arranged so that they can not give any amount of money requested by the user. Due to the limited size of the bill dispenser (the device that is directly giving money from an ATM) and some peculiarities of the ATM structure, you can get at most k bills from it, and the bills may be of at most two distinct denominations.

For example, if a country uses bills with denominations 10, 50, 100, 500, 1000 and 5000 burles, then at k = 20 such ATM can give sums 100 000 burles and 96 000 burles, but it cannot give sums 99 000 and 101 000 burles.

Let's suppose that the country uses bills of n distinct denominations, and the ATM that you are using has an unlimited number of bills of each type. You know that during the day you will need to withdraw a certain amount of cash q times. You know that when the ATM has multiple ways to give money, it chooses the one which requires the minimum number of bills, or displays an error message if it cannot be done. Determine the result of each of the q of requests for cash withdrawal.

Input

The first line contains two integers n, k (1 ≤ n ≤ 5000, 1 ≤ k ≤ 20).

The next line contains n space-separated integers ai (1 ≤ ai ≤ 107) — the denominations of the bills that are used in the country. Numbers ai follow in the strictly increasing order.

The next line contains integer q (1 ≤ q ≤ 20) — the number of requests for cash withdrawal that you will make.

The next q lines contain numbers xi (1 ≤ xi ≤ 2·108) — the sums of money in burles that you are going to withdraw from the ATM.

Output

For each request for cash withdrawal print on a single line the minimum number of bills it can be done, or print  - 1, if it is impossible to get the corresponding sum.

Examples
Input
6 20
10 50 100 500 1000 5000
8
4200
100000
95000
96000
99000
10100
2015
9950
Output
6
20
19
20
-1
3
-1
-1
Input
5 2
1 2 3 5 8
8
1
3
5
7
9
11
13
15
Output
1
1
1
2
2
2
2
-1

因为只能取2种面值,所以枚举一种面值,然后搞个map找另一个面值跟它匹配

 #include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<ctime>
#define LL long long
#define inf 0x7ffffff
#define pa pair<int,int>
#define mkp(a,b) make_pair(a,b)
#define pi 3.1415926535897932384626433832795028841971
using namespace std;
inline LL read()
{
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,k,q,cnt;
int a[];
struct p{int v,w,k;}b[];
bool operator <(p a,p b){return a.w<b.w||a.w==b.w&&a.k<b.k;}
map<int,pa>mp,mp2;
int main()
{
mp.clear();mp2.clear();
n=read();k=read();
for (int i=;i<=n;i++)
{
a[i]=read();
for (int j=;j<=k;j++)
b[++cnt].v=i,b[cnt].k=j,b[cnt].w=j*a[i];
}
sort(b+,b+cnt+);
for (int i=;i<=cnt;i++)
{
int w=b[i].w,kk=b[i].k,v=b[i].v;
if (mp[w]==mkp(,))mp[w]=mkp(kk,v);
else
{
if (mp[w].first>kk)mp2[w]=mp[w],mp[w]=mkp(kk,v);
else if (mp2[w].first>kk)mp2[w]=mkp(kk,v);
}
}
q=read();
for (int i=;i<=q;i++)
{
int x=read(),ans=;
for (int j=;j<=n;j++)
{
for (int l=;l<=k;l++)
{
int now=l*a[j];
if (now>x)break;
if (now==x){ans=min(ans,l);break;}
pa s=mp[x-now];
if (s.second==j)s=mp2[x-now];
if (s==mkp(,))continue;
ans=min(ans,s.first+l);
}
}
printf("%d\n",ans>k?-:ans);
}
}

cf524C

cf524C The Art of Dealing with ATM的更多相关文章

  1. Codeforces Round VK Cup 2015 - Round 1 (unofficial online mirror, Div. 1 only)E. The Art of Dealing with ATM 暴力出奇迹!

    VK Cup 2015 - Round 1 (unofficial online mirror, Div. 1 only)E. The Art of Dealing with ATM Time Lim ...

  2. codeforce The Art of Dealing with ATM

    题目大意 ATM取款机有n种不同的钱币kind[i],每次取款允许吐出不超过k张钱币,且钱币的种类数不能超过2(一开始没理解2的意思),现在有q次取款,钱数为ques,问ATM能否凑出这样的钱,若能的 ...

  3. CodeForces 524C The Art of Dealing with ATM (二分)

    题意:给定 n 种不同的钞票,然后用q个询问,问你用最多k张,最多两种不同的钞票能不能组成一个值. 析:首先如果要求的值小点,就可以用DP,但是太大了,所以我们考虑一共最多有n * k种钞票,如果每次 ...

  4. Codeforces 524C.The Art of Dealing with ATM(暴力)

    我先采用了智障解法(n * n枚举...刚开始把n看成1000了还以为能过) 理所当然的t了,不过我怀疑优化一下能过?(感觉数据不太行的亚子 然后就是O(n * k * k)的解法,看到好多人快乐二分 ...

  5. [转] Loren on the Art of MATLAB

    http://blogs.mathworks.com/loren/2007/03/01/creating-sparse-finite-element-matrices-in-matlab/ Loren ...

  6. 1179: [Apio2009]Atm

    1179: [Apio2009]Atm Time Limit: 15 Sec  Memory Limit: 162 MBSubmit: 1629  Solved: 615[Submit][Status ...

  7. The Art of Deception

    前言 一些黑客毁坏别人的文件甚至整个硬盘,他们被称为电脑狂人(crackers)或计算机破坏者(vandals).另一些新手省去学习技术的麻烦,直接下载黑客工具侵入别人的计算机,这些人被称为脚本小子( ...

  8. 设计模式(十二):通过ATM取款机来认识“状态模式”(State Pattern)

    说到状态模式,如果你看过之前发布的重构系列的文章中的<代码重构(六):代码重构完整案例>这篇博客的话,那么你应该对“状态模式”并不陌生,因为我们之前使用到了状态模式进行重构.上一篇博客我们 ...

  9. How the problem solved about " Dealing with non-fast-forward errors"

    Recently ,I got confused When I use  git to push one of my project. The problem is below: And I Foun ...

随机推荐

  1. 超全的BAT一线互联网公司内部面试题库

    想进BAT吗?点击上方的蓝色文字关注我们后,马上 告诉你答案!! 欢迎收藏和专注本文,以后我们会陆续的整理和收集其他的公司的面试题,扩大我们的面试库,形成专栏. 这是由乐视网工程师整理的一份一线互联网 ...

  2. @Param注解在dao层的使用

    有时在前台用ajax传过来许多参数,不知道在mybatis如何封装,就要用到@Param注解了,这时就不需要在映射文件写传入参数了,这种方法虽然比较取巧,但还是很实用的,如下图:

  3. 新手 WordPress主题制作全过程

    WordPress主题制作全过程(一):基础准备 前言: 我想大多数使用WordPress的朋友都喜欢去尝试新的主题,但是换来换去,总是找不到那么一款适合自己的,让人很郁闷.于是很多人萌生了修改现有主 ...

  4. Neural Style论文笔记+源码解析

    引言 前面在Ubuntu16.04+GTX1080配置TensorFlow并实现图像风格转换中介绍了TensorFlow的配置过程,以及运用TensorFlow实现图像风格转换,主要是使用了文章A N ...

  5. NOIP模拟赛 篮球比赛1

    篮球比赛1(basketball1.*) Czhou为了提高机房里各种神牛的身体素质,决定在每次训练后举行篮球比赛.为了保持比赛公平,Czhou要将神牛们分成两队.首先神牛们赛前都要排成固定的队伍:然 ...

  6. NOIP复习之1 数学数论

    noip一轮复习真的要开始啦!!! 大概顺序是这样的 1.数学 2.搜索贪心 3.数据结构 4.图论 5.dp 6.其他 数学 1.数论 数论被称为数学皇冠上的明珠,他的重要性主要在于它是其他学习的祖 ...

  7. loc与iloc函数的使用

    Pandas中loc和iloc函数用法详解(源码+实例)  https://blog.csdn.net/w_weiying/article/details/81411257 Pandas中loc,il ...

  8. try_except__异常处理

    try...except.raise 一.try...except 有时候我们写程序的时候,会出现一些错误或异常,导致程序终止.例如,做除法时,除数为0,会引起一个ZeroDivisionError ...

  9. 使用fio测试磁盘I/O性能

    简介: fio是测试IOPS的非常好的工具,用来对硬件进行压力测试和验证,支持13种不同的I/O引擎,包括:sync,mmap, libaio, posixaio, SG v3, splice, nu ...

  10. winServer08上安装SQL时提示“必须使用管理角色安装”或配置microsoft.net framework 3.5

    server 2008安装vs2008后报错,如图: 解决方法: 控制面板—>程序—>打开或关闭Windows功能—>进入服务器管理器选择功能—>添加功能 然后勾选.NET F ...