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.

narahc321 | 170 |

agness | 163 |

lonecoder | 153 |

2016uec1342 | 142 |

HuHu | 140 |

lonecoder | 1088 |

nikhil_sojan | 1082 |

KshatriyaYash | 1066 |

mazumderrohit8 | 928 |

SumitSingh27 | 832 |

blackshadows | 5327 |

Ibrahim Nash | 5215 |

akhayrutdinov | 5111 |

mb1973 | 4504 |

Quandray | 4444 |

Login to report an issue on this page.