-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMAXDIFF.cpp
More file actions
34 lines (30 loc) · 720 Bytes
/
MAXDIFF.cpp
File metadata and controls
34 lines (30 loc) · 720 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<bits/stdc++.h>
int arr[100002] = {0};
int main(void) {
int T;
std::cin >> T;
while (T--) {
int N, K, temp;
std::cin >> N;
std::cin >> K;
long sum = 0;
// int arr[N] = {0};
for (int i=0; i < N; i++) {
std::cin >> temp;
arr[i] = temp;
arr[i+1]=0;
sum += temp;
}
std::sort(arr, arr+N);
// std::cout << "SORT" <<std::endl;
long subsum = 0;
if (K > (N-K)) {
K = N-K;
}
for (int i=0; i < K; i++) {
subsum += arr[i];
// std::cout << arr[i] << " ";
}
std::cout << (sum - 2* subsum) << "\n";
}
}