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.

hardikJain5 | 258 |

CodeBuddy | 250 |

bhatabhi554 | 232 |

Akkki111 | 197 |

clone | 174 |

KshatriyaYash | 1932 |

nikhil_sojan | 1374 |

lonecoder | 1236 |

SumitSingh27 | 1127 |

mazumderrohit8 | 1124 |

blackshadows | 5327 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4591 |

Quandray | 4444 |

Login to report an issue on this page.