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.
Input
* Line 1: Two space-separated integers: N and M
* Lines 2. N + 1: Line I + 1 describes charm I with two space-separated integers: Wi and Di
Output
* Line 1: A single integer that is the greatest sum of charm desirabilities that can be achieved given the weight constraints
Sample Input
4 6
1 4
2 6
3 12
2 7 Sample Output
23. Typical 0-1 backpack problem. n items exist. The weight of the item numbered I is w [I] and the value is v [I]. now we need to select some items from these items to a backpack with a capacity of m, so that the value of the objects in the backpack is as big as possible if the total weight of the objects does not exceed m. # include <stdio. h>
# Include <string. h> int max (int a, int B)
{
Return a> B? A: B;
} Int main ()
{
Int n, m, w [40000], d [40000], I, j;
Int dp [40000];
While (~ Scanf ("% d", & n, & m ))
{
Memset (dp, 0, sizeof (dp ));
For (I = 0; I <n; I ++) scanf ("% d", & w [I], & d [I]);
For (I = 0; I <n; I ++)
{
For (j = m; j> = w [I]; j --)
{
Dp [j] = max (dp [j], dp [j-w [I] + d [I]);
}
}
Printf ("% d \ n", dp [m]);
} Return 0;
}