wa in GSS4 spoj

problem = http://www.spoj.com/problems/GSS4/
my soln = http://ideone.com/fyqxJX

Please someone help why am I getting wrong answer

I created 2 binary indexed tree one for sum
and other for finding the next index that is not one