# Dynamic programming for counting knapsack solutions

Suppose the usual dynamic programming algorithm for the knapsack problem. If we swap the max with an addition, does the modified algorithm compute all the solutions with benefit $\leq W$? I described the problem very briefly but I think anyone familiar with these notion wouldn't find any difficulty understanding what I ask. I think the complexity stays $nW$. Is it right?

• You mention that you think that everything works. If so, it seems like you already know the answer. Do you have any specific doubts? – Yuval Filmus Mar 10 '15 at 19:53
• My main doubt is that the idea appears too simple. More precisely, if the optimization problem can be computed exactly in $nW$ it seems like we get the answer to the counting problem for free. – Paramar Mar 10 '15 at 19:57
• This is often the case in dynamic programming, for the reason you mention (you can replace $\max$ or $\lor$ with $+$). – Yuval Filmus Mar 10 '15 at 20:08
• Answers that allow only "yes" as an answer are not good for the site. If you have a specific question about your approach, please flesh out your post. – Raphael Mar 11 '15 at 7:15
• In addition -- what have you tried, and where did you get stuck? The obvious way to answer your question on your own is to try to prove the algorithm correct (say, by induction). Have you tried that? If not, you should try it -- we expect you to make a significant effort before asking, and to show us what you've tried. – D.W. Mar 11 '15 at 16:37