#include <bits/stdc++.h>
using namespace std;
using ll=long long;
vector<int>arr;
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n, k;
cin>>n>>k;
for(int i=0; i<n; ++i) {
int a;
cin>>a;
arr.push_back(a);
}
sort(arr.begin(), arr.end());
ll ans=0;
if(k%2==0) {
for(int i=0; i<k/2; ++i) {
ans+=arr[n-i-1];
}
for(int i=0; i<k/2-1; ++i) {
ans-=arr[i];
}
} else {
for(int i=0; i<k/2+1; ++i) {
ans+=arr[n-i-1];
}
for(int i=0; i<k/2; ++i) {
ans-=arr[i];
}
}
cout<<ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
380 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
3 ms |
504 KB |
Output is correct |
14 |
Correct |
3 ms |
376 KB |
Output is correct |
15 |
Correct |
3 ms |
504 KB |
Output is correct |
16 |
Correct |
3 ms |
504 KB |
Output is correct |
17 |
Correct |
5 ms |
632 KB |
Output is correct |
18 |
Correct |
7 ms |
888 KB |
Output is correct |
19 |
Correct |
9 ms |
888 KB |
Output is correct |
20 |
Correct |
9 ms |
892 KB |
Output is correct |
21 |
Correct |
19 ms |
1912 KB |
Output is correct |
22 |
Correct |
25 ms |
1908 KB |
Output is correct |
23 |
Correct |
25 ms |
1912 KB |
Output is correct |
24 |
Correct |
26 ms |
1912 KB |
Output is correct |
25 |
Correct |
75 ms |
5232 KB |
Output is correct |
26 |
Correct |
75 ms |
5184 KB |
Output is correct |
27 |
Correct |
75 ms |
5228 KB |
Output is correct |
28 |
Correct |
52 ms |
5356 KB |
Output is correct |
29 |
Correct |
75 ms |
5216 KB |
Output is correct |
30 |
Correct |
76 ms |
5232 KB |
Output is correct |
31 |
Correct |
75 ms |
5236 KB |
Output is correct |
32 |
Correct |
78 ms |
5356 KB |
Output is correct |
33 |
Correct |
2 ms |
376 KB |
Output is correct |
34 |
Correct |
17 ms |
1656 KB |
Output is correct |