Consider submissions 151556965 and 151558044. They are the same, except that the first is in C++20 and the second is C++17. The solutions fail because the bounds for the problem are N <= 2e5, but I used MAXN = 20005 for the length of an array (the problem involved reading in an array of length N).
Thus we get RTE on the C++17 submission (as expected) due to accessing memory out-of-bounds. But why does the first one TLE?
You get UB in both cases (as expected).
As for what leads to TLE when I run your solution on my Linux system after compiling it with GCC 11.2.0:
N
anda
are allocated in the .bss section andN
is placed right behinda
. So an off-by-one write toa
overwritesN
and happens to put a really large value there (someting like 1000000000 or 999999998 for the failed testcase). So the loop runs a lot more iterations than expected and this is slow.std::cin >> a[i]
stops writing toa[i]
after reaching the end of the input data from stdin. As a result the program:a
N
with something very largeSomething similar is probably happening on Codeforces with C++20. But UB is unpredictable and you can never expect any specific behaviour.