ASHIGIFT problem | Contest 4 | WA for sub task 2

This is my code for the problem - CodeChef: Practical coding for everyone
I have tried all test cases that occurred to me but I cant figure out the problem . I also went through the editorial and the solution codes didn’t do anything different than what I have done (afaik). Can someone point out the error? I keep getting WA on subtask 2. Even though the answer for the examples (given in problem description) is correct .

It’s probably because of sorting that you have implemented. You can try the solution by removing it and it would help you immensely.