P2871 [usaco 07dec] Charm Bracelet, p2871usaco 07dec
Description
Bessie has gone to the mall's jewelry store and spies a charm bracelet. of course, she 'd like to fill it with the best charms possible from the N (1 ≤ N ≤3,402) available charms. each charm I in the supplied list has a weight Wi (1 ≤ Wi ≤ 400), a 'inclurability 'factor Di (1 ≤ Di ≤ 100 ), and can be used at most once. bessie can only support a charm bracelet whose weight is no more than M (1 ≤m ≤ 12,880 ).
Given that weight limit as a constraint and a list of the charms with their weights and desirability rating, deduce the maximum possible sum of ratings.
There are N items and a backpack with a capacity of V. The weight of the I-th item is c [I], and the value is w [I]. Solving which items are loaded into a backpack can make the total weight of these items not exceed the size of the backpack, and the total value is the largest.
Input/Output Format
Input Format:
Output Format:
- Line 1: A single integer that is the greatest sum of charm desirabilities that can be achieved given the weight constraints
Input and Output sample
Input example #1:
4 61 42 63 122 7
Output sample #1:
23
Although it is a bare backpack, but this cannot be two-dimensional, it will burst
Then I pushed a one-dimensional one by myself ,,
It seems that it can be shorter...
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 using namespace std; 6 void read(int & n) 7 { 8 char c='+';int x=0;int flag=0; 9 while(c<'0'||c>'9')10 {11 c=getchar();12 if(c=='-')13 flag=1;14 }15 while(c>='0'&&c<='9')16 x=x*10+(c-48),c=getchar();17 flag==1?n=-x:n=x;18 }19 const int MAXN=1000001;20 int n,maxt;21 struct node22 {23 int w;24 int v;25 }a[MAXN];26 int dp[MAXN];27 int main()28 {29 read(n);read(maxt);30 for(int i=1;i<=n;i++)31 {32 read(a[i].w);read(a[i].v);33 }34 for(int i=1;i<=n;i++)35 {36 for(int j=maxt;j>=0;j--)37 {38 if(a[i].w<=j)39 dp[j]=max(dp[j],dp[j-a[i].w]+a[i].v);40 else41 dp[j]=dp[j];42 }43 }44 cout<<dp[maxt];45 return 0;46 }