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.

**Input:**

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[ ].

**Output:**

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

**Constraints:**

1<= T <=100

1<= N, W <=1000

1<= cost[ ] <=1000** **

**Example:**

**Input:**

2

5 5

20 10 4 50 100

5 5

-1 -1 4 5 -1

**Output:**

14

-1

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.

Sulagna | 117 |

RUTVIKHARIPARA | 116 |

janvidavda105190 | 93 |

harshsiddhapura105461 | 93 |

madhursengar24 | 86 |

PiyushPandey4 | 617 |

ASWATHAMA | 561 |

akhyasharma01 | 547 |

john_wick | 521 |

jagrit_07 | 448 |

blackshadows | 5362 |

Ibrahim Nash | 5242 |

akhayrutdinov | 5111 |

mb1973 | 4929 |

Quandray | 4598 |

Login to report an issue on this page.