Poj 1276 cash machine

Source: Internet
Author: User

Cash machine
Time limit:1000 ms   Memory limit:10000 K
Total submissions:26675   Accepted:9419

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 shocould deliver and write a program that computes the maximum amount of cash less than or equal to cash that can be specified tively delivered according to the available bill supply 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 participating 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 350633 4  500 30  6 100  1 5  0 1735 00 3  10 100  10 50  10 10

Sample output

73563000



Multiple backpacks..., written for the first time.

A template has been created.


The AC code is as follows:

#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>using namespace std;int dp[100005];int g[15],z[15];int v;void ZOpack(int a){    int i;    for(i=v;i>=a;i--)        dp[i]=max(dp[i],dp[i-a]+a);}void WQpack(int a){    int i;    for(i=a;i<=v;i++)        dp[i]=max(dp[i],dp[i-a]+a);}void W(int gg,int zz){    if(gg*zz>v)        WQpack(zz);    else{        int k=1;        while(k<gg)        {            ZOpack(k*zz);            gg-=k;            k<<=1;        }        ZOpack(gg*zz);    }}int main(){    int n;    int i,j;    while(~scanf("%d%d",&v,&n))    {        memset(dp,0,sizeof dp);        for(i=1;i<=n;i++)            scanf("%d%d",&g[i],&z[i]);        for(i=1;i<=n;i++)            W(g[i],z[i]);        printf("%d\n",dp[v]);    }    return 0;}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.