forked from Mohammed-Shoaib/Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathE0016.cpp
More file actions
36 lines (32 loc) · 627 Bytes
/
E0016.cpp
File metadata and controls
36 lines (32 loc) · 627 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
35
36
// Problem Code: MAXDIFF
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <cmath>
using namespace std;
int maximumDifference(int K, vector<int> W){
int son, chef;
sort(W.begin(), W.end());
if(K > W.size()/2)
K = W.size() - K;
son = accumulate(W.begin(), W.begin()+K, 0);
chef = accumulate(W.begin()+K, W.end(), 0);
return chef-son;
}
int main()
{
int T, N, K, num;
vector<int> W;
cin >> T;
for(int i=1 ; i<=T ; i++){
cin >> N >> K;
for(int j=0 ; j<N ; j++){
cin >> num;
W.push_back(num);
}
cout << maximumDifference(K, W) << endl;
W.clear();
}
return 0;
}