This problem had rather high constraints - in most languages, the default method of taking input and printing output will likely time out, and the statement had a note asking participants to use faster methods.
However, there is one more pitfall here, specifically in C++ - and that is the use of endl.
The standard way of speeding up i/o in C++ via adding the lines
ios::sync_with_stdio(0);
cin.tie(0);
speeds up output by ensuring that the output buffer is not flushed every time cin is called (which is what cin.tie(0) does).
However, endl always forces a flush of the buffer, so using it essentially nullifies any benefit you had in the first place.
The workaround is to always use \n instead of endl .