Comments

The bug is from if(abs(res[i]+res[i-1])<=1), try (-4, 4, 2), that is, abs(8 + (-2)) > 1 and the branch gets false, but actually it should be true. The key to this problem is to find a peak or a valley value, that is, a local maximum or a local minimum value. You may replace the branch judgment with if (abs(1LL * res[i] * res[i - 1]) < 0), it works.

How to check ? I remember I did participate as rated, but I cannot find me in common standings, so strange ...