Problem Link
#include <iostream>
#include <algorithm>
using namespace std;
int arr[101];
int main()
{
int t, n, k;
long sum1, sum2, res;
cin >> t;
while(t--)
{
cin >> n >> k;
for(int i = 0; i < n; i++)
{
cin >> arr[i];
}
if(k > n / 2)
{
k = n - k;
}
sort(arr, arr + n);
sum1 = sum2 = 0;
for(int i = 0; i < k; i++)
{
sum1 += arr[i];
}
for(int i = k; i < n; i++)
{
sum2 += arr[i];
}
cout << sum2 - sum1 << endl;
}
return 0;
}
#include <iostream>
#include <algorithm>
using namespace std;
int arr[101];
int main()
{
int t, n, k;
long sum1, sum2, res;
cin >> t;
while(t--)
{
cin >> n >> k;
for(int i = 0; i < n; i++)
{
cin >> arr[i];
}
if(k > n / 2)
{
k = n - k;
}
sort(arr, arr + n);
sum1 = sum2 = 0;
for(int i = 0; i < k; i++)
{
sum1 += arr[i];
}
for(int i = k; i < n; i++)
{
sum2 += arr[i];
}
cout << sum2 - sum1 << endl;
}
return 0;
}
Comments
Post a Comment