My code is fine.Butt it is giving wrong ans.I used cin.eof() for break the loop
Why do you believe your code is fine if it gets WA?
Because it works for any testcases.problem is in using cin.eof().If you have any doubt feel free to check the comments on problem page.
Any? I think the WA means there is at least one test case where your code doesn’t work. Your challenge is to find that test case.
1163 people have got AC, so I suspect the test cases are probably correct.