# input: v[0..N) and w[0..N) arrays of natural numbers, N>=0, and C>=0## output: Maximum retribution by choosing elements with individual# retribution and weight in v[0..N) and w[0..N), respectively,# to carry in a knapsack with weight capacity C## exponential (in N ans C) naive solutiondefks_naive(v,w,C):returnks_aux(v,w,len(v),C)defks_aux(v,w,n,c):ans=Noneifn==0:ans=0else:ans=ks_aux(v,w,n-1,c)ifw[n-1]<=c:ans=max(ans,v[n-1]+ks_aux(v,w,n-1,c-w[n-1]))returnans
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.