Getting output of dynamic programming, (contiguous sub array, box stacking)
You are computing inductively an array maxWeight. The value of maxWeight at a certain point $i$ is the maximum over several possibilities. Currently you are remembering only the maximum itself. If you also store which possibility was maximum, then you will be able to use this additional information in order to construct an optimal solution.