Minimum cost to fill given weight in a bag
Submissions: 2953   Accuracy:


  Difficulty: Medium   Marks: 4

You are given a bag of size W kg and you are provided costs of packets different weights of oranges in array cost[] where cost[i] is basically cost of ‘i’ kg packet of oranges. Where cost[i] = -1 means that ‘i’ kg packet of orange is unavailable

Find the minimum total cost to buy exactly W kg oranges and if it is not possible to buy exactly W kg oranges then print -1. It may be assumed that there is infinite supply of all available packet types.

Note : array starts from index 1.

The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. The first line of each test case contains integers N and W where N denotes the size of array cost[ ] and W is the size of the bag.

The second line of each test case contains N space separated integers denoting elements of the array cost[ ].


Print the minimum cost to buy exactly W kg oranges. If no such answer exists print "-1".


1<= T <=100

1<= N, W <=1000

1<= cost[ ] <=1000 




5 5

20 10 4 50 100

5 5

-1 -1 4 5 -1





** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Sujnesh Mishra
Author: sujnesh

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

Need help with your code? Please use, generate link and share the link here.

to report an issue on this page.