#include <bits/stdc++.h>
using namespace std;
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int N, M, S;
cin >> N >> M >> S;
vector<int> A(N), B(N);
for (int i = 0; i < N; i++) {
cin >> A[i] >> B[i];
}
vector<int> ord(N);
iota(ord.begin(), ord.end(), 0);
sort(ord.begin(), ord.end(), [&](int x, int y) {
return A[x] - B[x] > A[y] - B[y];
});
vector<long long> pref(N + 1), suff(N + 1);
priority_queue<pair<int, int>> Q;
long long sum_a = 0, sum_b = 0;
for (int i = 0; i < N; i++) {
Q.push({-A[ord[i]], i});
sum_a += A[ord[i]];
while (int(Q.size()) > M) {
sum_a -= A[ord[Q.top().second]];
Q.pop();
}
pref[i + 1] = sum_a;
}
Q = priority_queue<pair<int, int>>();
for (int i = N - 1; i >= 0; i--) {
Q.push({-B[ord[i]], i});
sum_b += B[ord[i]];
while (int(Q.size()) > S) {
sum_b -= B[ord[Q.top().second]];
Q.pop();
}
suff[i] = sum_b;
}
long long answer = numeric_limits<int>::min();
for (int i = 0; i <= N; i++) {
answer = max(answer, pref[i] + suff[i]);
}
cout << answer << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
2 ms |
720 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
13 ms |
2316 KB |
Output is correct |
14 |
Correct |
25 ms |
3720 KB |
Output is correct |
15 |
Correct |
45 ms |
6484 KB |
Output is correct |
16 |
Correct |
56 ms |
9388 KB |
Output is correct |
17 |
Correct |
68 ms |
10808 KB |
Output is correct |
18 |
Correct |
77 ms |
11512 KB |
Output is correct |
19 |
Correct |
82 ms |
12288 KB |
Output is correct |
20 |
Correct |
93 ms |
14048 KB |
Output is correct |