What answer does your program give for
1 4 2 3
? What should it be?
Read the question properly please.
It is saying that Chef can purchase fruits ‘at most’ k times and NOT ‘exactly’ k times. You are finding the answer for ‘exactly’ k times.
Hope this helps