why does NOT using Scanner class in Java give TLE?

I submitted two solution for a problem which only differ by two lines of seemingly inconsequential nature. One solution gives TLE but other one passes. The solution that gives TLE is NOT using Scanner class. The solution that passes has two additional lines which import and initialize a Scanner class. The additional lines are given below. If anyone can provide a reason for this, it will help a lot. Thanks.

import java.util.Scanner;
Scanner in = new Scanner(System.in);


Solution which initializes Scanner obj (passes):

Solution without Scanner obj (gives TLE):

This is interesting. Is the behavior consistent on the judge? Also can it be reproduced locally?

For this problem, I tried multiple submissions and the behavior is consistent. I couldn’t reproduce this locally. Other problems I have solved didn’t have this issue.