Unable to solve this problem

Problem Link

I am unable to solve this problem for very long time. I mean I see that picking greedily would help but how do we solve it using that? Please provide intuition in solving this problem.