unable to find the bug in the code for DRGHTS

i have been working on this problem(DRGHTS Problem - CodeChef) for about a week now, i thought a lot and tested this code many times with different inputs, and get correct result each time, but when i submit it, the result is WA,somebody plz hint where am i going wrong
heres my code with an example input : 72P7ol - Online C++0x Compiler & Debugging Tool - Ideone.com.
thanx for ur time