#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int n, k, p[300005];
ll ans;
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
cin >> n >> k;
if(~k&1) k--;
for(int i = 0; i < n; i++) cin >> p[i];
sort(p, p+n);
for(int i = 0; i < k/2; i++) ans -= p[i];
for(int i = 0; i < (k+1)/2; i++) ans += p[n-1-i];
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
364 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 |
376 KB |
Output is correct |
14 |
Correct |
3 ms |
376 KB |
Output is correct |
15 |
Correct |
3 ms |
376 KB |
Output is correct |
16 |
Correct |
3 ms |
376 KB |
Output is correct |
17 |
Correct |
4 ms |
632 KB |
Output is correct |
18 |
Correct |
7 ms |
760 KB |
Output is correct |
19 |
Correct |
9 ms |
680 KB |
Output is correct |
20 |
Correct |
9 ms |
760 KB |
Output is correct |
21 |
Correct |
19 ms |
1656 KB |
Output is correct |
22 |
Correct |
24 ms |
1656 KB |
Output is correct |
23 |
Correct |
24 ms |
1656 KB |
Output is correct |
24 |
Correct |
24 ms |
1656 KB |
Output is correct |
25 |
Correct |
71 ms |
4484 KB |
Output is correct |
26 |
Correct |
70 ms |
4472 KB |
Output is correct |
27 |
Correct |
71 ms |
4344 KB |
Output is correct |
28 |
Correct |
49 ms |
4472 KB |
Output is correct |
29 |
Correct |
71 ms |
4448 KB |
Output is correct |
30 |
Correct |
70 ms |
4644 KB |
Output is correct |
31 |
Correct |
72 ms |
4444 KB |
Output is correct |
32 |
Correct |
70 ms |
4344 KB |
Output is correct |
33 |
Correct |
2 ms |
376 KB |
Output is correct |
34 |
Correct |
16 ms |
1400 KB |
Output is correct |