Codeforces problem

https://codeforces.com/contest/1343/problem/B

https://codeforces.com/contest/1343/submission/78378419

I tried solving this problem using array but i was getting RE?
Later i just printed it and got AC

Can anyone tell me whats exactly went wrong for arrays

https://codeforces.com/contest/1343/submission/78378903

this one got AC