knapsack::Int-- Bag size->Int-- Number of items->ArrayIntInt-- Item weights->ArrayIntInt-- Item values->Int-- The max value we can hold in the bagknapsacksizenumItemsvaluesweights=table!(size,numItems)wheretable=array((0,0),(size,numItems))[(i,fi)|i<-range((0,0),(size,numItems))]f(s,i)|s==0=0-- If bag size is zero |i==0=0-- If we don't take any items|s-weights!(i-1)<0=table!(s,i-1)-- If the item is too big|otherwise=max(values!(i-1)+table!(s-weights!(i-1),i-1))(table!(s,i-1))