Help needed in problem Discount on crackers DSA learning series

https://www.codechef.com/LRNDSA08/problems/ACM14KG3
My solution is:https://www.codechef.com/viewsolution/38997122
I know my solution is wrong but how to identify it is graph problem the solution i had coded is giving right answer at sample test case but Giving WA while submitting .
Pls: Tell how to identify this problem can be solved by graph and approach to this problem
Thanks