-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1832B.cpp
More file actions
43 lines (33 loc) · 966 Bytes
/
1832B.cpp
File metadata and controls
43 lines (33 loc) · 966 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
37
38
39
40
41
42
43
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--) {
int n, k;
cin >> n >> k;
vector<ll> a(n);
for (int i = 0; i < n; ++i)
cin >> a[i];
sort(a.begin(), a.end());
vector<ll> prefix(n + 1, 0);
for (int i = 0; i < n; ++i)
prefix[i + 1] = prefix[i] + a[i];
ll total_sum = prefix[n];
ll max_remaining = 0;
int max_pairs = min(k, n - k);
for (int x = 0; x <= max_pairs; ++x) {
int two_min = 2 * x;
int one_max = k - x;
if (k + x > n) continue;
ll removed = prefix[2 * x] + (prefix[n] - prefix[n - (k - x)]);
ll remaining = total_sum - removed;
max_remaining = max(max_remaining, remaining);
}
cout << max_remaining << '\n';
}
return 0;
}