Knapsacks

3.8

34 votes
Algorithms, Dynamic Programming
Problem

You are given objects, a knapsack of capacity , array , and array . The  object has value v[i] and weight w[i].

Determine the maximum total value that you can get by selecting objects in such a manner that their sum of weights is not greater than the capacity .

Input format

  • First line: Two integers  and  denoting the number of objects and capacity of the knapsack ( and
  • Second line:  integers  ()
  • Third line:  integers  ()

Output format

Print a single integer denoting the maximum value that you can get by selecting the objects.

Sample Input
4 20
10 2 1 3
10 5 10 10
Sample Output
13
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Optimal selection is 1st and 4th object. Total value=(10+3)=13 Total weight=(10+10)=20. Total weight <= Capacity.

Editor Image

?