Unbounded Knapsack ( Repetition of items allowed ) | Returns the maximum value with knapsack of W capacity ; dp [ i ] is going to store maximum value with knapsack capacity i . ; Fill dp [ ] using above recursive formula ; Driver program
def unboundedKnapsack ( W , n , val , wt ) : NEW_LINE INDENT dp = [ 0 for i in range ( W + 1 ) ] NEW_LINE ans = 0 NEW_LINE for i in range ( W + 1 ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT if ( wt [ j ] <= i ) : NEW_LINE INDENT dp [ i ] = max ( dp [ i ] , dp [ i - wt [ j ] ] + val [ j ] ) NEW_LINE DEDENT DEDENT DEDENT return dp [ W ] NEW_LINE DEDENT W = 100 NEW_LINE val = [ 10 , 30 , 20 ] NEW_LINE wt = [ 5 , 10 , 15 ] NEW_LINE n = len ( val ) NEW_LINE print ( unboundedKnapsack ( W , n , val , wt ) ) NEW_LINE