Contest Setup: Fast I/O
BufferedReader and StringBuilder: fast input/output and a contest template.
Scanner is simple but slow: with hundreds of thousands of numbers it can miss the time limit even when the algorithm is correct. The contest standard for Java is BufferedReader for reading and StringBuilder for output.
• BufferedReader reads in large chunks; StreamTokenizer or split parse the line into numbers;
• StringBuilder accumulates all the output, which is printed with a single call at the end.
Java
The fast template. Enter: 4, then 10 20 30 40
What matters in the template:
• throws Exception on main saves you from mandatory try/catch when reading;
• br.readLine() reads a whole line, StringTokenizer hands out its numbers one by one;
• all the output is collected in a StringBuilder — thousands of println calls are several times slower.
The Java competitor's checklist:
1. The class is named Main.
2. Large values go into long; in products at least one factor is cast to long.
3. Strings are compared with equals, not ==.
4. Lots of data — BufferedReader instead of Scanner, StringBuilder instead of println in a loop.
The course is finished — congratulations!
Open the "Learn" section and start the "Competitive Programming Path" from Level 1: algorithm complexity, implementation problems and basic math. Good luck at the contests!