1066:0/1 Backpack time limit: 1 Sec Memory Limit: MB
Title Description
A traveler has a backpack with a maximum of M kg, and now has n items, the weights of which are w1,w2,...,wn respectively, and their value is C1,C2,..., Cn. If there is only one piece of each item, the traveler can get the maximum total value.
Input
First line: Two integers, M (backpack capacity, m<=200) and N (number of items, n<=30);
2nd.. N+1 line: Two integer wi,ci per line, indicating the weight and value of each item.
Output
A single row, a number, representing the maximum total value.
Sample input
10 42 13 34 57 9
Sample output
12
Exercises
is 0/1 backpack, but input comparison egg pain, first input m and then enter N.
Review the DP equation for the 0/1 backpack: F[i]=max (F[j-c[i]]+w[i]) J from M Downto C[i].
1#include <bits/stdc++.h>2 using namespacestd;3 intf[700010],n,m,c[510],w[510];4 intMain () {5scanf"%d%d",&m,&n);6 for(intI=1; i<=n;++i) scanf ("%d%d",&c[i],&W[i]); 7 for(intI=0; i<=m;++i) f[i]=0;8 for(intI=1; i<=n;++i)9 for(intj=m;j>=c[i];--j)TenF[j]=max (f[j],f[j-c[i]]+w[i]); Oneprintf"%d\n", F[m]); A return 0; -}
"DP Basics" 01 Backpack-Review