College Life 4 (March long challenge)

Does anyone solve this question using Recursion with some optimization.
I solve this ques using recursion but at the last test case It give me wrong answer ?
I don’t know why here is my code link View Code.
Please take a look and help me…:slight_smile: