0/1 KNAPSACK PROBLEM

3.6

5 votes
Algorithms
Problem

Problem

You are given n  objects, a knapsack of capacity  c, array v, and array w. The ith 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 c.

Hint:  Use Dynamic programming 

Input format:

First line: Two integers n and c  denoting the number of objects and capacity of the knapsack ( 1 <= n<= 10^3 and  1 <= C <= 2*10^6 ) .

Second line:  n  integers  (0 <= Vi <= 50)

Third line:  n integers  ( 10 <=Wi <= 2*10^6)

Output format:

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

Time Limit: 5
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

?