Cash machine POJ (multiple backpack)

Source: Internet
Author: User

E-cash MachineTime limit:1000MS Memory Limit:10000KB 64bit IO Format:%i64d &%i64 U SubmitStatus

Description

A Bank plans to install a machine for cash withdrawal. The machine was 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 have a supp Ly 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 ten 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 so can be effectively delivered according for the available bill supply of the machine.

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

Input

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

Cash N N1 D1 n2 D2 ... nN DN

where 0 <= cash <= 100000 is the amount of cash requested, 0 <=n <= are the number of Bill denominations and 0 <= nk <= are the number of available bills for the DK denomination, 1 <= dk <=, k=1,n. White space s can occur freely between the numbers in the input. The input data is 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 Belo W.

Sample Input

735 3  4  6 5  3 350633 4 ten  6 1  5 0  1735  3

Sample Output

73563000

Hint

The first data set designates a transaction where the amount of cash requested is @735. The machine contains 3 bill denominations:4 bills of @125, 6 bills of @5, and 3 bills of @350. The machine can deliver the exact amount of requested cash.

In the second case, the bill supply of the machine does is not fit the exact amount of cash requested. The maximum cash that can was delivered is @630. Notice that there can is several possibilities to combine the bills of the machine for matching the delivered cash.

In the third case, the machine was empty and no cash is delivered. In the fourth case the amount of cash requested are @0 and, therefore, the machine delivers no cash.

Main topic:

There are different denominations of currencies, each with a different number of denominations, find out the amount of the nearest and less equal to the given number cash that can be made using these currencies. -----Typical Multi-pack

#include <iostream>#include<cstdio>#include<algorithm>#include<cstring>#defineN 600000#defineINF 0x3f3f3f3f#defineLL Long Longusing namespacestd;intV[n], num[n], f[n];intMain () {intN, S;  while(~SCANF ("%d%d", &s, &N)) {intI, J;  for(i =0; I < n; i++) scanf ("%d%d", &num[i], &V[i]); if(s = =0|| n = =0)/*last hint written.*/{printf ("0\n"); Continue; } memset (F,0,sizeof(f));  for(i =0; I < n; i++)        {            if(V[i]*num[i] >= s)/*Full Backpack*/            {                 for(j = v[i]; J <= S; j + +) F[j]= Max (F[j], F[j-v[i]] +V[i]); }            Else            {                /*0-1 Backpack*/                intK =1;  while(K < num[i])/*adopt the idea of binary system*/                {                     for(j = s; J >= K * V[i]; j--) F[j]= Max (F[j], f[j-k * V[i]] + k *V[i]); Num[i]-=K; K<<=1; }                 for(j = s; J >= Num[i]*v[i]; j--) F[j]= Max (F[j], F[j-num[i] * V[i]] + num[i] *V[i]); }} printf ("%d\n", F[s]); }    return 0;}

Cash machine POJ (multiple backpacks)

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.