Description

A Bank plans to install a machine for cash withdrawal. The machine is able to deliver appropriate @ bills for a requested cash amount. The machine uses exactly N distinct bill denominations, say Dk, k=1,N, and for each denomination Dk the machine has a supply of nk bills. For example,

N=3, n1=10, D1=100, n2=4, D2=50, n3=5, D3=10

means the machine has a supply of 10 bills of @100 each, 4 bills of @50 each, and 5 bills of @10 each.

Call cash the requested amount of cash the machine should deliver and write a program that computes the maximum amount of cash less than or equal to cash that can be effectively delivered according to the available bill supply of the machine.

Notes: 
@ is the symbol of the currency delivered by the machine. For instance, @ may stand for dollar, euro, pound etc. 

Input

The program input is from standard input. Each data set in the input stands for a particular transaction and has the format:

cash N n1 D1 n2 D2 ... nN DN

where 0 <= cash <= 100000 is the amount of cash requested, 0 <=N <= 10 is the number of bill denominations and 0 <= nk <= 1000 is the number of available bills for the Dk denomination, 1 <= Dk <= 1000, k=1,N. White spaces can occur freely between the numbers in the input. The input data are correct.

Output

For each set of data the program prints the result to the standard output on a separate line as shown in the examples below. 

Sample Input

735 3  4 125  6 5  3 350
633 4 500 30 6 100 1 5 0 1
735 0
0 3 10 100 10 50 10 10

Sample Output

735
630
0
0

【题意】给出一个sum,还有一个n代表有n个价值和对应个数;

【思路】多重背包,dp数组表示是否存在该状态,不断更新最大价值

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
struct node
{
int n,v;
} a[];
int dp[];
int main()
{
int sum,n;
while(~scanf("%d%d",&sum,&n))
{
for(int i=; i<=n; i++)
{
scanf("%d%d",&a[i].n,&a[i].v);
}
if(!sum||(!n))
{
printf("0\n");
continue;
}
memset(dp,,sizeof(dp));
dp[]=;
int maxn=,tmp;
for(int i=; i<=n; i++)
{
for(int j=maxn; j>=; j--)
{
if(dp[j])
{
for(int k=; k<=a[i].n; k++)
{
tmp=j+k*a[i].v;
if(tmp>sum) continue;
dp[tmp]=;
if(tmp>maxn) maxn=tmp;
}
} }
}
printf("%d\n",maxn);
}
return ;
}

Cash Machine_多重背包的更多相关文章

  1. poj 1276 Cash Machine_多重背包

    题意:略 多重背包 #include <iostream> #include<cstring> #include<cstdio> using namespace s ...

  2. Poj 1276 Cash Machine 多重背包

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26172   Accepted: 9238 Des ...

  3. POJ1276 - Cash Machine(多重背包)

    题目大意 给定一个容量为M的背包以及n种物品,每种物品有一个体积和数量,要求你用这些物品尽量的装满背包 题解 就是多重背包~~~~用二进制优化了一下,就是把每种物品的数量cnt拆成由几个数组成,1,2 ...

  4. POJ1276:Cash Machine(多重背包)

    Description A Bank plans to install a machine for cash withdrawal. The machine is able to deliver ap ...

  5. Cash Machine(多重背包)

    http://poj.org/problem?id=1276 #include <stdio.h> #include <string.h> ; #define Max(a,b) ...

  6. POJ-1276 Cash Machine 多重背包 二进制优化

    题目链接:https://cn.vjudge.net/problem/POJ-1276 题意 懒得写了自己去看好了,困了赶紧写完这个回宿舍睡觉,明早还要考试. 思路 多重背包的二进制优化. 思路是将n ...

  7. POJ 1276 Cash Machine(多重背包的二进制优化)

    题目网址:http://poj.org/problem?id=1276 思路: 很明显是多重背包,把总金额看作是背包的容量. 刚开始是想把单个金额当做一个物品,用三层循环来 转换成01背包来做.T了… ...

  8. POJ 1276:Cash Machine 多重背包

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 30006   Accepted: 10811 De ...

  9. POJ1276:Cash Machine(多重背包)

    题目:http://poj.org/problem?id=1276 多重背包模板题,没什么好说的,但是必须利用二进制的思想来求,否则会超时,二进制的思想在之前的博客了有介绍,在这里就不多说了. #in ...

随机推荐

  1. 杂记(编程style)----google code style!

    1.文件名 使用小写字母和下划线组合.头文件以.h结尾,定义文件用.cc结尾.例如:my_useful_class.cc 2.类型名 使用大写字母开头,多个单词组合时每个单词的首字母大写.例如:Url ...

  2. HDU----(2157)How many ways??(快速矩阵幂)

    How many ways?? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  3. ios基础篇(五)——UITextField的详细使用

    UItextFieldField通常用于外部数据输入,以实现人机交互. 以下是UItextFieldField的属性及常见用法: 1.textField :设置文本框的默认文本. 2.Placehol ...

  4. MySQL索引实现

    摘自:http://blog.codinglabs.org/articles/theory-of-mysql-index.html 在MySQL中,索引属于存储引擎级别的概念,不同存储引擎对索引的实现 ...

  5. JavaScript 事件委托的技术原理

    如今的 JavaScript 技术界里最火热的一项技术应该是‘事件委托(event delegation)’了.使用事件委托技术能让你避免对特定的每个节点添加事件监听器:相反,事件监听器是被添加到它们 ...

  6. Octopus系列之HttpCustom2.0模板引擎的处理,一个bug的分析

    实现的目标是: 1.实现手机和PC模板请求的区分:使得来自两种不同设备请求的时候,各自路由到不同的目录中去 2.保持只有一个引擎实例对象 最后发现一个bug就是,当我从PC访问时初始化了PC的目录,呈 ...

  7. Windows内核对象

    1. 内核对象 Windows中每个内核对象都只是一个内存块,它由操作系统内核分配,并只能由操作系统内核进行访问,应用程序不能在内存中定位这些数据结构并直接更改其内容.这个内存块是一个数据结构,其成员 ...

  8. C语言中文件的读取和写入

    在C语言中写文件 //获取文件指针 FILE *pFile = fopen("1.txt", //打开文件的名称 "w"); // 文件打开方式 如果原来有内容 ...

  9. html5+js实现刮刮卡效果

    通过Canvas实现的可刮涂层效果. 修改img.src时涂层也会自动适应新图片的尺寸. 修改layer函数可更改涂层样式. 涂层: 可刮效果: <!DOCTYPE html> <h ...

  10. MBProgressHUD.h file not found

    MBProgressHUD框架,怎么我导入MBProgressHUD+MJ.h会报错.(即MBProgressHUD+MJ根本不存在),我看其他人的视屏又可以导入 MBProgressHUD.h fi ...