ChefHack WA. Please help.

I had almost the same solution as the one mentioned in the editorial, but I used Java and the sieve of atkin.

I tried several input cases, and they all generated the right answer on my computer. Yet I still got WA after submitting :/.

Could somebody tell me what’s wrong?

http://www.codechef.com/viewsolution/1715077

You should avoid putting so much comments in your code… My advice is if you want to keep a large piece of code save it to a file or something like that… Just comment when you need to keep track of your current code, avoid keeping useless code comments, it just makes your life a lot harder… Now back to your question: I would suggest you change your variable “ans” to type long… In some testcases the result will be a negative number because type int in this particular problem will not be enough to store all possible totals… This is also mentioned in the Editorial in the first paragraph of “Quick Explanation”. This seems to be the main reason for TLE on your code.