It’s not neccessary and harder to intuitively visualise. The reason we have to go from total value to 0 is we are using the previous dp[i][j-object[i].value] . I don’t want this value to have already used object[i] because then I will be double counting.
can you explain it with example ??
In my dp[i][j] ,I stored the minimum weight required to calculate j value using i items
n,w = map(int , input().split())
values = []
weights = []
ans= 0
totalvalues = 0
for i in range(n):
a,b = map(int ,input().split())
weights.append(a)
values.append(b)
totalvalues += b
dp = [ [0 for i in range(totalvalues+1)] for j in range(n+1) ]
for i in range(totalvalues+1):
dp[0][i] = float(“inf”)
for i in range(n+1):
dp[i][0] = 0
for i in range(1,n+1):
for j in range(1,totalvalues+1):
if values[i-1] <= j :
dp[i][j] = min(dp[i-1][j], dp[i-1][j-values[i-1]] + weights[i-1])
else :
dp[i][j] = dp[i-1][j]
for i in range(1,n+1):
for j in range(1,totalvalues + 1):
if dp[i][j] <= w:
ans = j