WA in INOI2001

I was solving INOI2001 and my approach was the same as in the editorial but I am getting WA in a few testcases. Could someone help me?
Problem: INOI2001
My solution: solution

Thank you