I was getting tle in this. Any efficient approach to solve this ? PS - The contest is over, I just wanted to know the logic.