Dynamic Programming: Knapsack - 0/1 Knapsack Problem
Examine the following Python code snippet for a space-optimized 0/1 Knapsack implementation. Identify the bug that causes incorrect results:
def knapsack_bug(weights, values, W):
n = len(weights)
dp = [0] * (W + 1)
for i in range(n):
for w in range(weights[i], W + 1):
dp[w] = max(dp[w], dp[w - weights[i]] + values[i])
return dp[W]