Comments

It has nothing to do with logic but rather the implementation of the rand() function in C++. As this article (https://mirror.codeforces.com/blog/entry/61587) explains, the rand() function only gives values < RAND_MAX = 32767. TC 20 triggers this flaw. Luckily the post also provides a solution ;)