I have coded the problem as described in the hint but the result is TLE

I have coded the problem as described in the hint but the result is TLE