Batch Output the Right Way
Buffer with a list and join to print fast.
Output Can Be Slow Too
Just like reading, writing matters. Calling print() thousands of times flushes repeatedly and can quietly cost you the time limit.
Collect, Then Print
The fix is to gather your answers in a list first, then send them out in a single write at the end.
out = []All lessons in this course
- input() vs sys.stdin Showdown
- Parse Many Numbers on One Line
- Batch Output the Right Way
- Read N Lines and Edge Cases