(KLPM) April challenge 2019

Can anyone tell me how to solve this problem using Top down approach.
Thankyou.
Problem link - CodeChef: Practical coding for everyone