It is always a good idea to find a recursive algorithm first and then turn it into a table.
- $f(C,n)$
- $~~$if(C = $\emptyset$) return 0;
- $~~$else
- $~~~~$opt = 0;infinity;
- $~~~~$for each $c\in C$ do
- $~~~~~~D=\{d\in C:d<c\}$
- $~~~~~~E=\{e-c:e\in D,e>c\}$
- $~~~~~~opt = min\{opt,f(D,c)+f(E,n-c)\}$
- $~~~~$return $opt+n$;
So you may ask: isn't there too many subsets of C to be put in a table? Observe that only 'consecutive' subsets are needed. And there are only $n \choose 2$ of them.(why?) Another problem is: some entries will change value in $E$. We can walk around this by indicating start and end in each $f$ rather than just specifying the length.