A collection of code templates to kickstart the journey of solving interesting problems on Codeforces!


  1. Make sure all inputs are read in in sequence
  2. Use at least 64Bit to prevent integer overflow issue, long in Java, long long in CPP
  3. Dynamic Array over Array

Java Code Template

Test Solution.java with test cases inside input.txt

java Solution.java < input.txt

Basic Data Structure Operation Cheatsheet

Java List

Avoid Potential TLE

  1. If there are many small segments that need to be printed out one by one, or we need to consolidate many small input string pieces into one string. Consolidate them into one string with StringBuilder. Because string in java is Immutable, concatenating 2 strings require us to create a new string. Example: without StringBuilder Buffer, with StringBuilder Buffer
  2. Use BufferedReader, InputStreamReader and StringTokenizer to read in the input, instead of using Scanner

CPP Code Template

Test Solution.cpp with test cases inside input.txt

g++ -o Solution Solution.cpp && ./Solution < input.txt