Minimum Maximum

3

1 votes
Medium
Problem

Here is your task. Given a sequence A1, A2,.....AN , you have to find the maximum possible difference between sum of all elements of subsequence S of length K such that S1≥S2≥..........SK and sum of all elements of subsequence D of length same as S such that D1≤D2≤.........Dn (i.e (S1+S2+.....SK) - (D1+D2+.......DK)).
NOTE: Elements of both subsequences may be same or different.

INPUT

Input consists of number of test cases T.
Each test case contains size of array i.e N and K length of subsequences.
Next line contains N space separated elements of array.

OUTPUT

You have to print the maximum possible difference between two subsequences. If any of subsequences do not exist print "-1" without the quotes.

CONSTRAINTS

1 <= T <= 100
1 <= N <= 10^4
1 <= a[i] <= 10^5
1 <= k <=100

Time Limit: 5
Memory Limit: 256
Source Limit:
Explanation

In Sample Input
Maximum possible difference between sum of all elements of subsequence S and subsequence D is 45 where elements of Subsequence S are ( 28 , 20 , 16 ) and elements of Subsequence D are ( 3 , 6 , 10 ).

Editor Image

?