Can this be solved using DP
Can this question be solved using dp. Also can you please explain why greedy works. Thanks.