Writing an Efficient Code in Codeforces is important for a problem to be Accepted. We will compare your different code runtimes by implementing them in simple standard libraries and then implementing the same code using a buffer reader, creating the necessary String Token, and changing the int variables to Long. Then, we will understand why the Buffered one is faster than the other.
Why does Scanner take longer compared to BufferedReader and DataInputStream? The primary reason is that the Scanner reads smaller bits of information from the input stream one at a time without buffering. This means that the process of reading from the input stream, which is inherently slow, must be repeated multiple times. Each read operation involves significant overhead, making the process inefficient. In contrast, BufferedReader and DataInputStream use buffering to enhance performance. Buffered input works by reading large chunks of data from the file into a buffer in one go. The program then reads from this Buffer, significantly reducing the time it needs to access the slow input stream.
This method minimizes the bottleneck caused by the input stream read operations, making the overall reading process much faster.
I have made two sample codes, one without implementing Buffer and token and the other being implemented using Buffer and token. Check the equivalent code, and then I will attach a screenshot of their runtime.