java solution 10/10, in C is hidden bug somewhere