# include <bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define int long long
#define pii pair <int, int>
using namespace std;
const int N = 3e5 + 5;
int n,m,k,a[N],b[N],sum,num,ans,pr[N],sf[N];
multiset <int> ms;
multiset <int> :: iterator it;
vector <pii> v;
main() {
std::ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>n>>m>>k;
for (int i = 1; i <= n; i++) {
cin>>a[i]>>b[i];
v.pb({a[i] - b[i], i});
}
sort(v.begin(),v.end()); reverse(v.begin(),v.end());
if (m) {
for (int i = 0; i < v.size(); i++) {
num = a[v[i].s];
if (ms.size() < m) {
ms.insert(num);
sum += num;
}
else {
it = ms.begin();
if (*it < num) {
sum -= *it;
ms.erase(it);
sum += num; ms.insert(num);
}
} pr[i] = sum;
}
}
sum = 0;
ms.clear();
if (k) {
for (int i = (int)v.size() - 1; i >= 0; i--) {
num = b[v[i].s];
if (ms.size() < k) {
ms.insert(num); sum += num;
} else {
it = ms.begin();
if (*it < num) {
sum -= *it;
ms.erase(it); sum += num; ms.insert(num);
}
}
sf[i] = sum;
}
}
if (m == 0) {
cout<<sf[0]<<endl;
return 0;
}
if (k == 0) {
cout<<pr[n-1]<<endl; return 0;
}
for (int i = m - 1; i <= n - k - 1; i++) {
ans = max(ans, pr[i] + sf[i + 1]);
}
cout<<ans<<endl;
}
Compilation message
school.cpp:13:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
13 | main() {
| ^~~~
school.cpp: In function 'int main()':
school.cpp:22:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | for (int i = 0; i < v.size(); i++) {
| ~~^~~~~~~~~~
school.cpp:24:27: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
24 | if (ms.size() < m) {
| ~~~~~~~~~~^~~
school.cpp:44:27: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
44 | if (ms.size() < k) {
| ~~~~~~~~~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
3 ms |
588 KB |
Output is correct |
8 |
Correct |
2 ms |
716 KB |
Output is correct |
9 |
Correct |
2 ms |
716 KB |
Output is correct |
10 |
Correct |
2 ms |
716 KB |
Output is correct |
11 |
Correct |
2 ms |
716 KB |
Output is correct |
12 |
Correct |
2 ms |
716 KB |
Output is correct |
13 |
Correct |
16 ms |
3508 KB |
Output is correct |
14 |
Correct |
28 ms |
4932 KB |
Output is correct |
15 |
Correct |
49 ms |
7988 KB |
Output is correct |
16 |
Correct |
129 ms |
16188 KB |
Output is correct |
17 |
Correct |
183 ms |
15580 KB |
Output is correct |
18 |
Correct |
151 ms |
15924 KB |
Output is correct |
19 |
Correct |
181 ms |
17236 KB |
Output is correct |
20 |
Correct |
233 ms |
19788 KB |
Output is correct |