POJ 1276 Cash Machine (multiple backpacks)

Source: Internet
Author: User
Cash Machine
Time Limit:1000 MS   Memory Limit:10000 K
Total Submissions:20470   Accepted:7162

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

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 not fit the exact amount of cash requested. the maximum cash that can be delivered is @ 630. notice that there can be several possibilities to combine the bills in the machine for matching the delivered cash.

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

Source

Southeastern Europe 2002 Multiple backpacks
# Include <stdio. h> # include <iostream> # include <string. h ># include <algorithm> using namespace std; const int MAXN = 20; int dp [100010]; int value [MAXN]; // price of each bag int bag [MAXN]; // Number of bags int nValue, nKind; // 0-1 backpack, price: cost, value obtained is weightvoid ZeroOnePack (int cost, int weight) {for (int I = nValue; I >= cost; I --) dp [I] = max (dp [I], dp [I-cost] + weight);} // complete backpack, cost: cost, value obtained: weightvoid CompletePack (int cost, int weight) {for (int I = cost; I <= NValue; I ++) dp [I] = max (dp [I], dp [I-cost] + weight);} // void MultiplePack (int cost, int weight, int amount) {if (cost * amount> = nValue) CompletePack (cost, weight); else {int k = 1; while (k <amount) {ZeroOnePack (k * cost, k * weight); amount-= k; k <= 1;} ZeroOnePack (amount * cost, amount * weight ); // do not forget this, often dropped} int main () {while (scanf ("% d", & nValue )! = EOF) {scanf ("% d", & nKind); for (int I = 0; I <nKind; I ++) scanf ("% d ", & bag [I], & value [I]); memset (dp, 0, sizeof (dp); for (int I = 0; I <nKind; I ++) multiplePack (value [I], value [I], bag [I]); printf ("% d \ n", dp [nValue]);} 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.