Chef and Friends Editorial WA

i was trying to solve this question CodeChef: Practical coding for everyone

and used same logic given in editorial https://discuss.codechef.com/questions/84915/chfnfrn-editorial?sort=votes&page=2

still getting WA can anyone help to check my code sRJFio - Online C++ Compiler & Debugging Tool - Ideone.com