You are on page 1of 1

#include <iostream>

#include<cmath>
using namespace std;
int main()
{
int t,n,k;
cin>>t;
while(t--)
{
cin>>n>>k;
int j,key,a[n],s[n],s1,s2;
s[-1]=0;
for(int i=0;i<n;i++)
{
cin>>a[i];
key=a[i];
j=i-1;
while(j>=0 && a[j]>=key)
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=key;
}
for(int l=0;l<n;l++)
{
s[l]=a[l]+s[l-1];
}
s1=fabs((2*s[k-1]-s[n-1]));
s2=fabs((2*s[n-k-1]-s[n-1]));
if(s1>s2)
cout<<s1<<"\n";
else
cout<<s2<<"\n";
}
return 0;
}

You might also like