#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <iomanip>
#include <cassert>
#include <stack>
#include <queue>
#include <deque>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>-
using namespace std;
//using namespace __gnu_pbds;
typedef long long ll;
#define int ll
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
// template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// order_of_key (k) : Number of items strictly smaller than k .
// find_by_order(k) : K-th element in a set (counting from zero).
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define pb push_back
#define ppb pop_back
#define mkp make_pair
#define F first
#define S second
#define lb lower_bound
#define ub upper_bound
#define show(a) cerr << #a <<" -> "<< a <<" "
#define nl cerr <<"\n"
const int N = 3e5 + 5;
const int oo = 1e9 + 5;
int n, m, s, a[2][N], cnt[2];
vector < pair <int, pii> > vec;
multiset <pii> other[2], have[2];
bool used[N];
void add(int typ, int ind) {
int val = a[typ][ind];
cerr <<"add: "; show(val); show(typ); show(ind); nl;
--cnt[typ];
have[typ].insert({val, ind});
used[ind] = 1;
if (sz(other[typ])) { other[typ].erase(other[typ].find({val, ind})); }
}
void del(int typ, int ind) {
int val = a[typ][ind];
cerr <<"del: "; show(val); show(typ); show(ind); nl;
++cnt[typ];
if (sz(have[typ])) { have[typ].erase(have[typ].find({val, ind})); }
used[ind] = 0;
other[typ].insert({val, ind});
}
void Print() {
cerr << "other[0] -> ";
for (pii x : other[0]) { cout << x.F <<" "; } nl;
cerr << "other[1] -> ";
for (pii x : other[1]) { cout << x.F <<" "; } nl;
cerr << "have[0] -> ";
for (pii x : have[0]) { cout << x.F <<" "; } nl;
cerr << "have[1] -> ";
for (pii x : have[1]) { cout << x.F <<" "; } nl;
}
void solve() {
cin >> n >> m >> s;
for (int i = 1; i <= n; ++i) {
cin >> a[0][i] >> a[1][i];
vec.pb({a[0][i], {0, i}});
vec.pb({a[1][i], {1, i}});
}
sort(all(vec));
cnt[0] = m, cnt[1] = s;
int sum = 0;
for (int i = sz(vec)-1; i >= 0; --i) {
int val = vec[i].F,
typ = vec[i].S.F,
ind = vec[i].S.S;
int before_sum = sum, oth = -1, hav = -1; // OK
bool del_have = 0;
if (!cnt[typ]) {
if (sz(have[typ])) {
sum -= have[typ].begin() -> F;
hav = have[typ].begin() -> S;
del_have = 1;
}
else { other[typ].insert({val, ind}); continue; }
}
bool del_ind = 0, add_other = 0;
if (used[ind]) {
sum -= a[(typ^1)][ind]; del_ind = 1;
if (sz(other[(typ^1)])) {
sum += other[(typ^1)].rbegin() -> F;
oth = other[(typ^1)].rbegin() -> S;
add_other = 1;
}
}
sum += val;
//show(sum); show(before_sum); show(del_have); show(del_ind); show(add_other); nl;
//Print(); nl;
if (sum > before_sum) {
add(typ, ind);
if (add_other) { assert(oth != -1); add((typ^1), oth); }
if (del_have) { assert(hav != -1); del(typ, hav); }
if (del_ind) { del((typ^1), ind); }
}
else {
sum = before_sum;
other[typ].insert({val, ind});
}
}
cout << sum;
}
main () {
//ios_base::sync_with_stdio(false);
//cin.tie(NULL);
int tests = 1;
//cin >> tests;
while (tests --) {
solve();
}
return 0;
}
/*
Just Chalish!
show((typ^1)); show((other[(typ^1)].rbegin() -> S)); nl;
*/
Compilation message
school.cpp:128:8: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
128 | main () {
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
492 KB |
Execution killed with signal 6 (could be triggered by violating memory limits) |
2 |
Correct |
1 ms |
368 KB |
Output is correct |
3 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
4 |
Runtime error |
2 ms |
492 KB |
Execution killed with signal 6 (could be triggered by violating memory limits) |
5 |
Incorrect |
2 ms |
364 KB |
Output isn't correct |
6 |
Runtime error |
4 ms |
640 KB |
Execution killed with signal 6 (could be triggered by violating memory limits) |
7 |
Runtime error |
7 ms |
1256 KB |
Execution killed with signal 6 (could be triggered by violating memory limits) |
8 |
Runtime error |
9 ms |
1404 KB |
Execution killed with signal 6 (could be triggered by violating memory limits) |
9 |
Runtime error |
12 ms |
1384 KB |
Execution killed with signal 6 (could be triggered by violating memory limits) |
10 |
Runtime error |
11 ms |
1384 KB |
Execution killed with signal 6 (could be triggered by violating memory limits) |
11 |
Runtime error |
12 ms |
1384 KB |
Execution killed with signal 6 (could be triggered by violating memory limits) |
12 |
Runtime error |
10 ms |
1384 KB |
Execution killed with signal 6 (could be triggered by violating memory limits) |
13 |
Runtime error |
44 ms |
5340 KB |
Execution killed with signal 6 (could be triggered by violating memory limits) |
14 |
Runtime error |
90 ms |
10864 KB |
Execution killed with signal 6 (could be triggered by violating memory limits) |
15 |
Incorrect |
429 ms |
30784 KB |
Output isn't correct |
16 |
Runtime error |
193 ms |
23872 KB |
Execution killed with signal 6 (could be triggered by violating memory limits) |
17 |
Runtime error |
274 ms |
29028 KB |
Execution killed with signal 6 (could be triggered by violating memory limits) |
18 |
Runtime error |
271 ms |
31808 KB |
Execution killed with signal 6 (could be triggered by violating memory limits) |
19 |
Runtime error |
304 ms |
34368 KB |
Execution killed with signal 6 (could be triggered by violating memory limits) |
20 |
Runtime error |
342 ms |
39492 KB |
Execution killed with signal 6 (could be triggered by violating memory limits) |