Poj1276--cash machine--Multiple Backpack

Source: Internet
Author: User

Description

Suppose you now have cash cash, there are n kinds of goods, given you their respective price and quantity. If you want to buy as many items as possible, ask for the total value of the goods you can buy the most.

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

Exercises

Recently really in water title Yes WW(incredibly shy to say)

Inexplicably because cash==0| | The situation in n==0 wa two times ... Just don't write it, Qwq.

It's just a multi-pack, Dp[j] represents the maximum amount of money that can be spent when the current capacity is J. Sum[j] Represents the quantity of the current item purchased.

1#include <iostream>2#include <cmath>3#include <cstdio>4#include <algorithm>5#include <cstring>6 using namespacestd;7 Const intmaxn=100009;8 intF[MAXN],G[MAXN];9 intcash,n,w[maxn],num[maxn],sum[maxn],ans=0;Ten intMain () One { A      while(SCANF ("%d%d", &cash,&n)! =EOF) -     { -          for(intI=1; i<=n;i++) thescanf"%d%d",&num[i],&w[i]); -f[0]=0; -Memset (F,0,sizeof(f)); -          for(intI=1; i<=n;i++) +         { -memset (SUM,0,sizeof(sum)); +              for(intj=w[i];j<=cash;j++) A             { at                 if(f[j]<f[j-w[i]]+w[i]&&sum[j-w[i]]+1<=Num[i]) -                 { -sum[j]=sum[j-w[i]]+1; -f[j]=f[j-w[i]]+W[i]; -                 } -             } in         } -printf"%d\n", F[cash]); to     } +     return 0; -}
View Code

Poj1276--cash machine--Multiple Backpack

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.