Do a Binary Search to figure out the maximum value of N such that sum of 0 - N <= S. Find out the extra portion by subtracting Sum of 0 - N from S. Check whether the remaining amount evenly divisible by K or not. Print the desired output.
Pay special attention to the Corner Cases -
Dataset is too strict. While doing binary search set the higher limit in such a that the desired sum of MID value does not cross the S.
Take a good care about Corner Cases.