Dynamic Programming: Knapsack - Subset Sum
The following code attempts to solve subset sum using space-optimized DP. Identify the subtle bug that causes incorrect results on some inputs:
```python
def subset_sum(nums, S):
dp = [False] * (S + 1)
dp[0] = True
for num in nums:
for w in range(num, S + 1): # forward iteration
dp[w] = dp[w] or dp[w - num]
return dp[S]
```
