Introduction to Competitive Programming
Competitive programming means writing a program that solves a precisely specified problem for every allowed input, within resource limits. You submit source code to a judge that compiles and runs it on tests you usually cannot see. The goal is a correct algorithm, not merely code that reproduces the sample. This lesson starts with adding two numbers so you can learn the full statement-to-submission workflow before studying harder algorithms.
Prerequisites: None. This is the starting lesson.
Build the idea
1. Read the problem as a contract
Separate the task, input format, output format and constraints. If the statement says two integers a and b, one line of output containing a+b, do exactly that. Input values are supplied by the judge; do not print prompts such as “Enter a.” Spaces and line breaks separate numbers. The sample is one legal input paired with its expected result, not the complete set of tests.
2. Use constraints to plan
If |a| and |b| can reach 10^9, their sum might reach 2×10^9. A wider integer type avoids relying on a narrow range. Time limits bound execution time and memory limits bound storage; an approach that lists a billion possibilities is not made practical by short source code. Later lessons teach systematic operation and memory estimates. First ask whether your chosen operations could exceed the stated limits.
3. Trace the sample before coding
For input 7 5, the required computation is 7+5=12. The program must print 12 and a newline. Also reason about values absent from the example: 0+0, a negative plus a positive, and two values near the largest magnitude. Hidden tests target such cases. An algorithm is justified by the operation it performs for arbitrary legal a,b, not by memorizing one sample.
4. Understand the judge’s verdict
Accepted means all judge checks passed; it is strong evidence, not a proof covering a different problem contract. Wrong Answer means output differs, often due to a logic or formatting error. Time Limit Exceeded means the program did not finish quickly enough. Runtime Error may result from illegal memory access or other execution failure. Compilation Error means the compiler could not build the source; no algorithm test ran. Memory-limit terminology varies by judge.
5. Submit a complete program
The example includes the standard input/output header, main, two long-long variables, input and output. You will learn this syntax next; for now recognize where input becomes a result. Compile with a C++17 compiler, run it on the sample, then test boundaries. Saving a file is not compiling it, and compiling is not running it. Read diagnostics from the first relevant compiler error rather than changing the algorithm at random.
C++17 implementation
This is a complete program. Compile with C++17 and enter the input described below.
#include<iostream>
int main(){
long long a,b;
std::cin>>a>>b; // Read only the values specified by the statement.
std::cout<<a+b<<'\n'; // No extra prompts or explanation in judge output.
return 0;
}
Complexity analysis
O(1) arithmetic time and O(1) auxiliary memory for two bounded integers. Reading/writing a fixed number of machine-sized values is constant work in the usual competitive-programming model. This does not mean every future problem with a short program is constant-time.
Example input and output
Two integers a b with |a|,|b|≤10^9. Print their sum.
Input
7 5
Output
12
The program reads 7 and 5 and performs one addition. For −7 5 it would instead print −2.
Practical applications
Use this workflow for every contest task: translate the statement into an exact computation, inspect constraints, justify the approach, implement, then test. Later algorithms change the computation but not this process. Keep a small list of boundary cases and distinguish a compiler problem from a wrong algorithm.
Check your understanding
What does passing only the sample test establish?
Put it into practice
Test zero, opposite signs, and two large positive inputs.
Show a hint
Write the expected sum before running each case.