DRW Interview Question

A classic DP question, but require output the result by backtracing