1625: [Usaco2007 Dec] Gemstone Bracelet
Time Limit:5 Sec Memory limit:64 MB
submit:919 solved:618
[Submit] [Status]
Description
When Bessie was wandering around the jewellery shop, she bought a favorite bracelet. Naturally, she wanted to choose the best ones from the N (1 <= n <= 3,402) Gems She had collected on the bracelet. For the block I gem, it weighs w_i (1 <= w_i <= 400), and Bessie knows that it can add a charm value to itself after the bracelet is inlaid d_i (1 <= d_i <= 100). Since Bessie can only tolerate bracelets that weigh no more than M (1 <= m <= 12,880), she may not be able to set all her favorite gems. So Bessie finds you, tells you all the attributes of her gems and the weight she can tolerate, and I hope you can help her calculate how much her charm will increase by the most reasonable set of gems.
Input
* Line 1th: 2 integers separated by a space: N and M
* 2nd. N+1: Section i+1 Acts 2 integers separated by spaces: w_i, D_i, respectively, the weight of the jewel of Block I and the charm value that can be added to Bessie
Output
* Line 1th: Output 1 integers, indicating that Bessie can add up to the most attractive values according to the mosaic requirements
Sample Input
4 6
1 4
2 6
3 12
2 7
Input Description:
Bessie collected 4 jewels, and she could tolerate a bracelet with a maximum weight of 6.
Sample Output
23
Output Description:
Bessie put bracelets on all the remaining gems except the second gem, so that she could increase
The charm value of the 4+12+7=23, and the weight of all gemstones is 1+2+3 <= 6, also meet the requirements.
HINT
Source
Silver
Problems: Obvious 01 knapsack problem, one months ago saw "backpack Nine", inside the space compression Dafa will be sent in handy spicy!!! boil haha haha ... (PS: In fact, the full backpack as long as the M Downto b[i,1] into b[i,1] to M, think about why * ^_^ *)
1 var2 I,j,k,l,m,n:longint;3A:Array[0..20000] ofLongint;4B:Array[0..10000,1..2] ofLongint;5 functionMax (x,y:longint): Longint;6 begin7 ifX>y ThenMax:=xElsemax:=y;8 End;9 Ten One begin A readln (n,m); - fori:=1 toN Do -READLN (B[i,1],b[i,2]); thel:=0; - fori:=1 toN Do - forJ:=mDowntoB[i,1] Do - begin +A[j]:=max (A[j],a[j-b[i,1]]+b[i,2]); -l:=Max (l,a[j]); + End; A Writeln (l); at End.
1625: [Usaco2007 Dec] Gemstone Bracelet