Charm Bracelet

来源:互联网 发布:期货农产品数据库建立 编辑:程序博客网 时间:2024/05/18 17:26

Description

Bessie has gone to the mall's jewelry store and spies a charmbracelet. Of course, she'd like to fill it with the best charmspossible from the N (1 ≤ N ≤ 3,402) available charms.Each charm i in the supplied list has a weightWi (1 ≤ Wi ≤ 400), a'desirability' factor Di (1 ≤Di ≤ 100), and can be used at most once. Bessiecan only support a charm bracelet whose weight is no more thanM (1 ≤ M ≤ 12,880).

Given that weight limit as a constraint and a list of the charmswith their weights and desirability rating, deduce the maximumpossible sum of ratings.

Input

* Line 1: Two space-separated integers: N andM
* Lines 2..N+1: Line i+1 describes charm iwith two space-separated integers: Wi andDi

Output

* Line 1: A single integer that is the greatest sum of charmdesirabilities that can be achieved given the weightconstraints

Sample Input

4 61 42 63 122 7

Sample Output

23

Source

USACO 2007 December Silver
 
 
题意:裸的0-1背包问题。
考虑一个物品放或者不放,本来要滚动数组动规的,因为有可能多次取一物品,那么就可以从后往前修改f值就可以避免这种错误了。
 
AC CODE
programpku_3624;
var f:array[0..12880] of longint;
   n,c,i,j,v,w:longint;
begin
  readln(n,c);
  for i:=1 to n do
  begin readln(v,w);
    for j:=cdownto v do if f[j]<f[j-v]+w thenf[j]:=f[j-v]+w;
  end; writeln(f[c]);
end.
0 0
原创粉丝点击