Given a sequence of n integers, you have to find out the non-decreasing subsequence of length k with minimum sum. If no sequence exists output -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 K where N denotes the size of the array and K is the subsequence length for which we have to find the minimum sum,

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

**Output:**

Print the minimum sum for each test case in a new line. If no such answer exists print "-1".

**Constraints:**

1<= N <=1000

1<= K <=1000

1<= A[ ] <=100000

**Example:**

**Input:**

2

10 3

58 12 11 12 82 30 20 77 16 86

10 4

58 12 11 12 82 30 20 77 16 86

**Output:**

39

125

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.

sabka_din_aayega | 317 |

maggiiiii | 248 |

thanuvinu94 | 211 |

Savage_19 | 206 |

Anugrah_kumar | 172 |

Bishnu Dev Panda | 803 |

sabka_din_aayega | 628 |

Anugrah_kumar | 562 |

okayboss | 561 |

maggiiiii | 531 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4925 |

Quandray | 4547 |

Login to report an issue on this page.