Wrong answer in java

i was solving a problem . first i solved it in java and it gives wrong answer for all test cases but i was sure that approach is correct so i code it in c++ and it got accepted . why this is happening and even now i am trying to submit in java …it WA. Any solution?

that is from contest … and even i check the both code with same input it gives same output…

I had a similar problem in this contest my java solution gave wrong answer but my python answer gave the correct answer