Remembering choices to reconstruct subset sum solutions [00RE]
The above algorithm tells us whether it is possible to add to S via some subset, but it does not tell us what the actual subset is. To compute the actual optimal solution instead of just the optimal value, we can save the choices made at each step.
What information does \mathit {canAddTo} discard? It is precisely the choice of whether to use x_k or not. The Boolean “or” operation will be \mathsf {T} if either one of its inputs is; it does not care which. Therefore we will make another (n+1)\times (S+1) array of booleans called \mathit {use}, where \mathit {use}[k][s] is \mathsf {T} if and only if we should use x_k in a subset to make s. We initialize it to all \mathsf {F} values, and fill it in as shown in the pseudocode below.
If c[n][S] is , then there is some subset of X which adds to S. To reconstruct such an actual subset we can work our way backwards as follows: