How would you solve this problem from ZCO. (Parchment)

I have tried this problem and got an 83/100.

Problem link: CodeChef: Practical coding for everyone