#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fi first
#define se second
#define ll long long
#define ld long double
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mpp make_pair
#define ve vector
using namespace std;
using namespace __gnu_pbds;
template<class T> using oset = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
const ll inf = 1e18; const int iinf = 1e9;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename T> inline bool chmin(T& a, T b) { return (a > b ? a = b, 1 : 0); }
template <typename T> inline bool chmax(T& a, T b) { return (a < b ? a = b, 1 : 0); }
struct Node {
Node *l = nullptr;
Node *r = nullptr;
ll p, k, s, sz;
Node() {}
Node(ll x) : k(x), s(x) { p = 1 + rng() % inf; sz = 1; }
}; typedef Node* node;
inline ll s(node t) { return !t ? 0 : t->s; }
inline ll sz(node t) { return !t ? 0 : t->sz; }
inline void upd(node t) { if(!t){return;} t->s = s(t->l) + t->k + s(t->r); t->sz = sz(t->l) + 1 + sz(t->r); }
inline node mg(node a, node b) {
if (!a || !b) return (!a ? b : a);
if (a->p > b->p) { a->r = mg(a->r, b); upd(a); return a; }
else { b->l = mg(a, b->l); upd(b); return b; }
}
inline pair<node,node> sp(node t, int x) {
if (!t) return {nullptr, nullptr};
int q = sz(t->l);
if (x > q) { auto tt = sp(t->r, x - q - 1); t->r = tt.fi; upd(t); return {t, tt.se}; }
else { auto tt = sp(t->l, x); t->l = tt.se; upd(t); return {tt.fi, t}; }
}
inline pair<node,node> spk(node t, int x) {
if (!t) return {nullptr, nullptr};
if (x >= t->k) { auto tt = spk(t->r, x); t->r = tt.fi; upd(t); return {t, tt.se}; }
else { auto tt = spk(t->l, x); t->l = tt.se; upd(t); return {tt.fi, t}; }
}
inline void pr(node t) {
if (!t) return;
if (t->l) pr(t->l);
cout << t->k << " ";
if (t->r) pr(t->r);
}
inline void insert(node &t, int x) {
auto [t1, t2] = spk(t, x);
t = mg(t1, mg(new Node(x), t2));
}
inline void solve() {
int n, A, B;
cin >> n >> A >> B;
ve<pii> a(n);
for (auto &[x, y] : a) cin >> x >> y;
sort(all(a), [](const pii &a, const pii &b) { return a.fi - a.se < b.fi - b.se; });
reverse(all(a));
ve<ll> sum(n, -inf);
node t = nullptr;
for (int i = n - 1; ~i; --i) {
insert(t, a[i].se);
if (sz(t) >= B) {
auto [t1, t2] = sp(t, sz(t) - B);
sum[i] = s(t2);
t = mg(t1, t2);
}
}
ll ans = 0;
node t1 = nullptr;
for (int i = 0; i < n - 1; ++i) {
insert(t1, a[i].fi);
if (sz(t1) >= A) {
auto [t2, t3] = sp(t1, sz(t1) - A);
chmax(ans, sum[i + 1] + s(t3));
t1 = mg(t2, t3);
}
}
cout << ans;
}
signed main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int q = 1; // cin >> q;
while (q--) solve();
cerr << fixed << setprecision(3) << "Time execution: " << (double)clock() / CLOCKS_PER_SEC << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
5 ms |
980 KB |
Output is correct |
8 |
Correct |
5 ms |
980 KB |
Output is correct |
9 |
Correct |
5 ms |
980 KB |
Output is correct |
10 |
Correct |
6 ms |
980 KB |
Output is correct |
11 |
Correct |
7 ms |
980 KB |
Output is correct |
12 |
Correct |
6 ms |
980 KB |
Output is correct |
13 |
Correct |
69 ms |
5744 KB |
Output is correct |
14 |
Correct |
153 ms |
12144 KB |
Output is correct |
15 |
Correct |
321 ms |
24416 KB |
Output is correct |
16 |
Correct |
352 ms |
27568 KB |
Output is correct |
17 |
Correct |
394 ms |
33604 KB |
Output is correct |
18 |
Correct |
469 ms |
36916 KB |
Output is correct |
19 |
Correct |
570 ms |
39888 KB |
Output is correct |
20 |
Correct |
603 ms |
45836 KB |
Output is correct |