derivation from dynamic programming
add thoughts for sol
sol of q121