(KLPM) April challenge 2019

Can anyone tell me how to solve this problem using Top down approach.
Thankyou.
Problem link - https://www.codechef.com/APRIL19A/problems/KLPM