You are a rich person, and you think your wallet is too heavy and full now. So you want to give me some money by buying a lovely pusheen sticker which costs p dollars from me. To make your wallet lighter, you decide to pay exactly pp dollars by as many coins and/or banknotes as possible.

For example, if p = 1 and you have two 10 coins, four 5 coins, and eight 1 coins, you will pay it by two 5coins and seven 1 coins. But this task is incredibly hard since you are too rich and the sticker is too expensive and pusheen is too lovely, please write a program to calculate the best solution.

Input Format

The first line contains an integer Tindicating the total number of test cases.

Each test case is a line with 1 integers p, c1, c5, c10, c20, c50, c100, c200, c500, c1000, c2000, specifying the price of the pusheen sticker, and the number of coins and banknotes in each denomination. The number ci means how many coins/banknotes in denominations of ii dollars in your wallet.

1≤T≤20000

0≤p≤109

0≤ci​≤100000

Output Format

For each test case, please output the maximum number of coins and/or banknotes he can pay for exactly p dollars in a line. If you cannot pay for exactly p dollars, please simply output '-1'.

样例输入复制

3
17 8 4 2 0 0 0 0 0 0 0
100 99 0 0 0 0 0 0 0 0 0
2015 9 8 7 6 5 4 3 2 1 0

样例输出复制

9
-1
36

题目来源

ACM Changchun 2015

/*
某些面值的钱分别有若干个,用这些钱来恰好组成某一金额的钱,问最多需要的钱的数目
逆向思维 用总价减去需求=X
那么贪心来用最少的数目凑成X即可(也可能凑不出来)
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <string>
#include <deque>
using namespace std;
#define ll long long
#define N 1009
const ll inf =9e18;
#define gep(i,a,b) for(int i=a;i<=b;i++)
#define gepp(i,a,b) for(int i=a;i>=b;i--)
#define gep1(i,a,b) for(ll i=a;i<=b;i++)
#define gepp1(i,a,b) for(ll i=a;i>=b;i--)
#define mem(a,b) memset(a,b,sizeof(a))
ll val[]={,,,,,,,,,,};
ll num[],a[];
ll ans,p;
ll sum,ret;
int t;
void dfs(int x,ll sum,ll cnt)
{
if(!sum)
{
ans=min(ans,cnt);
return ;
}
if(x<) return ;
a[x]=min(sum/val[x],num[x]);
dfs(x-,sum-val[x]*a[x],cnt+a[x]);
if(a[x]>=)
{
a[x]--;
dfs(x-,sum-val[x]*a[x],cnt+a[x]);
}
/*
如 : 1
150 0 0 0 3 1 1 0 0 0 0
如果没有上面的三行就是无解 -1
但是 dfs(4,60,0) 因为 用了50,后面的就无法凑成10
但是后面的20*3可以凑成60,因此a[x]可一每次都减1,再去dfs
这样才能考虑到所有的情况!
*/
}
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%lld",&p);
mem(a,);
sum=;
ret=;
gep(i,,)
{
scanf("%lld",&num[i]);
ret+=num[i];
sum+=val[i]*num[i];
}
sum-=p;
if(sum<)
{
printf("-1\n");
continue;
}
ans=inf;
dfs(,sum,);
if(ans!=inf)
{
printf("%lld\n",ret-ans);
}
else{
printf("-1\n");
}
}
return ;
}

ACM Changchun 2015 A. Too Rich的更多相关文章

  1. ACM Changchun 2015 L . House Building

    Have you ever played the video game Minecraft? This game has been one of the world's most popular ga ...

  2. ACM Changchun 2015 J. Chip Factory

    John is a manager of a CPU chip factory, the factory produces lots of chips everyday. To manage larg ...

  3. ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 G. Garden Gathering

    Problem G. Garden Gathering Input file: standard input Output file: standard output Time limit: 3 se ...

  4. ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 D. Delay Time

    Problem D. Delay Time Input file: standard input Output file: standard output Time limit: 1 second M ...

  5. ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 I. Illegal or Not?

    I. Illegal or Not? time limit per test 1 second memory limit per test 512 megabytes input standard i ...

  6. ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 K. King’s Rout

    K. King's Rout time limit per test 4 seconds memory limit per test 512 megabytes input standard inpu ...

  7. ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 H. Hashing

    H. Hashing time limit per test 1 second memory limit per test 512 megabytes input standard input out ...

  8. ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 C. Colder-Hotter

    C. Colder-Hotter time limit per test 1 second memory limit per test 512 megabytes input standard inp ...

  9. ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 A. Anagrams

    A. Anagrams time limit per test 1 second memory limit per test 512 megabytes input standard input ou ...

随机推荐

  1. 洛谷1005(dp)

    1.不要贪,缩小区间去dp就好. 2.预处理指数. 3.__int128可还行. #include <cstdio> #include <cctype> #include &l ...

  2. centos下gitlab的简单安装配置

    一.安装前配置yum源 #wget https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh #s ...

  3. JavaScript Allongé 第一呷 :基础函数 (3) 未完

    闭包与域是时候来看下一个带函数的函数是如何工作的: (function (x) { return function (y) { return x } })(1)(2) //=> 1 首先,我们使 ...

  4. Ubuntu安装LAMP

    1.安装apache2 sudo apt-get install apache2 sudo apt-get install apache2 Apache安装成功后,/var/www/默认作为web的根 ...

  5. #52. 【UR #4】元旦激光炮 (交互式题)

    链接:http://uoj.ac/problem/52 刚刚越过绝境长城,只见天空中出现了炫目的光芒 —— 圣诞老人出现了. 元旦三侠立刻进入战斗.生蛋侠.圆蛋侠和零蛋侠分别有 na,nb,ncna, ...

  6. Java 过滤器实现(登录) + 拦截器(两种方法)

    以下是实现未登录不能进入页面的实现 使用了thyemeleaf+SpringBoot+过滤器实现的,过滤器的核心代码如下: @Component @WebFilter(filterName = &qu ...

  7. Android 实现类似于QQ空间相册的点击图片放大,再点后缩小回原来位置

    前几天看到了有人在android5.0上实现了如下图一样的效果,我自己就去搜了下.参考了国外一篇文章和国内的一篇文章,最终实现了想要的效果.具体参考的网址我已经贴到文章末尾,大家可以去英文的那个网站看 ...

  8. ListView与ScrollView冲突的4种解决方案

    问题解决方案1.手动设置ListView高度    经过测试发现,在xml中直接指定ListView的高度,是可以解决这个问题的,但是ListView中的数据是可变的,实际高度还需要实际测量.于是手动 ...

  9. Java文件操作系列[3]——使用jacob操作word文档

    Java对word文档的操作需要通过第三方组件实现,例如jacob.iText.POI和java2word等.jacob组件的功能最强大,可以操作word,Excel等格式的文件.该组件调用的的是操作 ...

  10. 洛谷 P1376 机器工厂

    题目描述 小T开办了一家机器工厂,在N(N<=10000)个星期内,原材料成本和劳动力价格不断起伏,第i周生产一台机器需要花费Ci(1<=Ci<=5000)元.若没把机器卖出去,每保 ...