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