def knapsack(weights, values, W):
 optimal_vals = ([0] * (W + 1))
 for j in range(0, len(weights)):
  for w in range(W, (weights[j] - 1), (-1)):
   if ((optimal_vals[(w - weights[j])] + values[j]) > optimal_vals[w]):
    optimal_vals[w] = (optimal_vals[(w - weights[j])] + values[j])
 return optimal_vals[(-1)]

def knapsack ( weights , values , W ) : optimal_vals = ( [ 0 ] * ( W + 1 ) ) for j in range ( 0 , len ( weights ) ) : for w in range ( W , ( weights [ j ] - 1 ) , ( - 1 ) ) : if ( ( optimal_vals [ ( w - weights [ j ] ) ] + values [ j ] ) > optimal_vals [ w ] ) : optimal_vals [ w ] = ( optimal_vals [ ( w - weights [ j ] ) ] + values [ j ] ) return optimal_vals [ ( - 1 ) ]
knapsack without repetition .