Help me in this question.

This is a practise problem from hackerrank…

link text

I am in doubt that whether should I use dp or greedy approach . If greedy will be used , then plz explain me how to identify when greedy approaches are preffered over dp.
Thanks