CHEFADV - Need help

I read the editorial and implemented code for this problem which passed all the given test cases but still gives WA. Can anyone help in pointing out my mistake:
My solution:
https://www.codechef.com/viewsolution/20341562