Poj1276--cash Machine (multi-backpack award can be reached)

Source: Internet
Author: User

Cash Machine
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 27804 Accepted: 9915

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.

Source

Southeastern Europe 2002 gives the n currencies, the value and quantity of each, and asks what is the closest thing to the case? Multiple knapsack problem, using binary to convert to 01 backpack. Converted from m to 12,4,2^ (k-1), m-2^k+1; K is obtained by the largest integer of the m-2^k+1>0
#include <cstdio> #include <cstring> #include <algorithm>using namespace std;int dp[110000]; int m[20]    , a[20]; int main () {int c, n, I, J, L, K, MM;        while (scanf ("%d%d", &c, &n)!=eof) {memset (dp,0,sizeof (DP));        Dp[0] = 1;        for (i = 0; i < n; i++) scanf ("%d%d", &m[i], &a[i]);            for (i = 0; i < n; i++) {k = 0;            while ((1<<k) <= m[i]) k++;            k--;            if (k = =-1) continue;            MM = (M[i]-(1<<k) +1) * A[i];            for (j = C; j-mm >= 0; j--) dp[j] + = dp[j-mm];                for (l = 0; l < K; l++) {mm = (1<<l) *a[i];            for (j = C; j-mm >= 0; j--) dp[j] + = dp[j-mm];        }} for (i = c; I >= 0; i--) if (dp[i]) break;    printf ("%d\n", I); } return 0;}

Poj1276--cash Machine (multi-backpack award can be reached)

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.