Input/Output Optimization
cin/cout can be too slow for competitive programming's tight time limits, how ios_base::sync_with_stdio(false) and cin.tie(NULL) fix that, why '\n' beats std::endl inside loops, and when scanf/printf are worth reaching for instead.
Why I/O Speed Matters in Competitive Programming
Everything you've written so far cared about correctness — did the program produce the right answer? Competitive programming (CP) adds a second constraint: a strict time limit, often 1–2 seconds, enforced by an online judge. A perfectly correct solution that reads its input too slowly can still fail with a Time Limit Exceeded verdict.
This matters because CP problems routinely read 10⁵ to 10⁶ integers or more. std::cin and std::cout are convenient — type-safe, overloaded for every type — but by default they carry overhead you don't need once you understand where it comes from.
iostream objects (cin/cout) are kept synchronized with C's stdio (scanf/printf), so a program can freely mix both styles and still see output in the correct order. That safety net costs time on every single I/O operation — and in a loop reading a million integers, it adds up.sync_with_stdio(false) & cin.tie(NULL)
Two lines, placed at the very top of main(), are the standard competitive-programming opener. They remove overhead you don't need as long as you commit to using either cin/cout or scanf/printf — not a mix of both.
#include <iostream> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; long long sum = 0; for (int i = 0; i < n; i++) { int x; cin >> x; sum += x; } cout << sum << "\n"; return 0; }
ios_base::sync_with_stdio(false) unlinks C++'s streams from C's stdio buffers, so cin/cout no longer pay the cost of staying interleaved with scanf/printf. cin.tie(NULL) removes a separate behavior: by default, cin is "tied" to cout, meaning cin automatically flushes cout's buffer before every read. Untying it lets output batch up naturally instead of flushing on every single input.
sync_with_stdio(false), cin/cout and scanf/printf use separate, independent buffers. Mixing them afterward can print output in the wrong order or read input incorrectly. Pick one I/O style for the whole program and stick with it.'\n' vs std::endl
std::endl does two things: it inserts a newline character, and it flushes the output buffer — forcing any buffered output to be written out immediately. '\n' only does the first part. In a loop that prints thousands or millions of lines, that extra flush on every single endl is pure wasted work.
#include <iostream> #include <vector> using namespace std; int main() { vector<int> results = {10, 25, 7, 42, 3}; // Slower in a loop: endl flushes the buffer on EVERY iteration for (int r : results) { cout << r << endl; } // Faster: '\n' just inserts the character, no forced flush for (int r : results) { cout << r << "\n"; } return 0; }
Both loops print the same output. The buffer still gets flushed eventually — automatically when it fills up, and always when the program exits normally — so switching to '\n' never loses output. It simply stops forcing a flush after every single line.
'\n' everywhere in competitive programming, and reach for endl only in the rare case you specifically need output visible on-screen before the program continues doing other work — for example, live debug output while a long computation is still running.scanf & printf as an Alternative
<cstdio>'s scanf/printf are C's I/O functions. They don't carry iostream's type-safety or operator overloading, but they also don't carry its abstraction overhead, so many competitive programmers reach for them directly instead of tuning cin/cout.
#include <cstdio> int main() { int n; scanf("%d", &n); long long sum = 0; for (int i = 0; i < n; i++) { int x; scanf("%d", &x); sum += x; } printf("%lld\n", sum); return 0; }
Notice %lld for long long and &x — scanf needs the address of each variable since it can't use references. This is more error-prone than cin >> x (a wrong format specifier compiles fine but reads garbage), which is the real trade-off: scanf/printf can be raw and fast, but they give up the compiler's type checking.
cin/cout with sync_with_stdio(false) and cin.tie(NULL) is fast enough for the overwhelming majority of CP problems while keeping type safety. Reach for scanf/printf only if a specific problem's I/O volume is so large that every microsecond counts — and then use them exclusively, not alongside cin/cout.General I/O Tips for Tight Loops
A few more habits that matter once input sizes get large:
sync_with_stdio(false); cin.tie(NULL); as the first two lines of main() in every CP solution — it costs nothing and only helps.cin/cout OR scanf/printf for the whole program once you've desynced them — never both.'\n' inside loops. Save endl for the rare case you need an immediate on-screen flush.std::string or ostringstream and printing once at the end reduces the total number of I/O calls.ios_base::sync_with_stdio(false) do?cin.tie(NULL) prevent?'\n' and std::endl?sync_with_stdio(false), is it safe to mix cin and scanf in the same program?endl inside a loop that prints thousands of lines a bad idea in CP?Now build a fast-I/O program. Complete the challenge below in any C++ compiler.
Write a program that:
1. Starts
main() with ios_base::sync_with_stdio(false); and cin.tie(NULL);2. Reads an integer
n, then reads n more integers with cin3. Tracks both the running sum and the maximum value seen, without storing all n values in a vector
4. Prints the sum and the maximum, each on their own line using
'\n' — not endl
Rules: No
endl anywhere in your solution, and no mixing with scanf/printf.
💡 Show hints if you're stuck
- You only need two accumulator variables — a running total and a running max — updated inside the same loop that reads each value
- Initialize the max to the smallest possible value, or to the first value read, before the loop compares anything
- Use
long longfor the sum in case the values are large