Getting WA for using ArrayList and AC for using array (CHEFPSA)

can anybody help me with the qsn https://www.codechef.com/problems/CHEFPSA

i used ArrayList got WA:https://www.codechef.com/viewsolution/31650529
used array got AC:https://www.codechef.com/viewsolution/31650081
took me a lot of time to figure why I’m getting WA…
thanks

@revanthreddy85 can you please tell what was the mistake. Actually I am also facing same issue in other question.

@pankajpoply45 i got WA coz i used Arraylist ,used array got AC still dont know why…