Grand temple-problem

can somebody suggest some approach or some hints to solve the problem.
link of code: https://online.codingblocks.com/player/21656/content/10111/5185

This problem is based on greedy approach, hint of which is given under greedy algorithm webinar by Prateek Bhaiya, plz watch that webinar for complete hint