Beware of using cerr on Codeforces
Many beginners hear this advice:
"Use cerr for debugging, it is ignored by the judge."
This statement is only partially true and can be misleading.
This blog explains when cerr is safe, when it is dangerous, and best practices used by experienced Codeforces users.
What is true about cerr
Output sent to cerr is not checked by the judge
It does not affect correctness
You will not get Wrong Answer because of cerr output
This is why many people recommend using cerr for debugging.
What is often missed (important)
cerr is unbuffered.
This means:
Every cerr << flushes immediately
Flushing is slow
Frequent usage consumes real execution time
So while the judge ignores the output, time is NOT ignored.
How cerr can cause TLE
Using cerr inside loops or for large input sizes can easily lead to Time Limit Exceeded.
Example of dangerous usage:
for (int i = 0; i < n; i++) { cerr << a[i] << " "; }
If n is large, this can be extremely slow.
When cerr is safe to use
Safe cases:
Very small input
Printing a few values
Printing once or twice
Local debugging only
Example (usually safe):
cerr << "Test case started\n";
When cerr should NOT be used
Avoid cerr:
Inside large loops
While printing arrays
During final submission
In performance-critical code
If you forget to remove such logs, you may get TLE even with a correct solution.
Best practice: conditional debugging (recommended)
Use a debug macro so that debug output is completely removed in submissions.
ifdef LOCAL
define debug(x) cerr << x << "\n";
else
define debug(x)
endif
Usage:
debug("current value = " << x);
On Codeforces:
LOCAL is not defined
All debug statements disappear
Zero runtime overhead
How experienced CF users debug
Debug locally
Print only summary values
Remove all debug output before submitting
Avoid printing inside loops
Use logic checks instead of heavy logs
Final takeaway
cerr output is ignored by the judge
cerr execution time is NOT ignored
Excessive cerr usage can cause TLE
Always remove or disable debug logs before submission
Use cerr carefully, especially in contests.




