You can think of each DP state (m,c)
as a vertex of a graph, where the recursive calls to states (m-item_i,c-1)
are edges from (m,c)
to (m-item_i,i)
.
Memorization of your recursion means that you only start the search from a vertex once, and also process its outgoing edges only once. So, your algorithm is essentially a linear search on this graph, and has complexity O(|V|+|E|)
. There are M*C vertices and at most max(K)
edges going out of each one, so you can bound the number of edges by O(M*C*max(K))
.