I think the test cases are not correct.
Problem: Buying Candies
My Solution: C++ code
Yes I think so, even if your logic is wrong it should give WA but not RE. So, there K must be 0 somewhere in the test cases, so it’s their fault. They should have mentioned 0\leq K \leq 10^9 .
Refer to this submission for AC.
UPD : Check this