How to get ans for negative values as well ...i am not getting it

Hello @samadsid7,

Can you explain, what exactly are you trying to ask?

BTW, there is no negative values.
Val[i] can have only one negative number i.e. -1, which that packet is not available.
So, you cannot buy that packet.
Thus, you have to ignore that index/packet.

@samadsid7,

Here is your modified code.

You were missing an important part of the question, that you can choose a single packet multiple times as you have an infinite supply of these packets.

Hope, this would help.
Give a like, if you are satisfied.