Language: C++

#include <bits/stdc++.h>

#define pii pair<int, int>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define ll long long
#define ld long double
#define sz(v) int(v.size())
#define all(v) v.begin(), v.end()

#define y1 what

using namespace std;

const int N = (int) 2e5 + 10;
const int M = (int) 1e6 + 7;
const ll LINF = (ll) 1e16;
const int INF = (int) 1e9 + 7;
const double EPS = (double) 1e-9;

int n, a, b;
pii pts[N];
ll pref[N];
ll suff[N];

vector<int> all_num;

int get_id(int x) {
    return lower_bound(all(all_num), x) - all_num.begin();
}

struct node {
    int cnt;
    ll sum;

    node() {
        cnt = sum = 0;
    }
};

node t[4 * N];

void upd(int x, int y, int v = 1, int l = 0, int r = sz(all_num) - 1) {
    if (l == r) {
        ++t[v].cnt;
        t[v].sum += y;
        return;
    }
    int m = (l + r) / 2;
    if (x <= m) {
        upd(x, y, v + v, l, m);
    } else {
        upd(x, y, v + v + 1, m + 1, r);
    }
    t[v].sum = t[v + v].sum + t[v + v + 1].sum;
    t[v].cnt = t[v + v].cnt + t[v + v + 1].cnt;
}

ll get(int k, int v = 1, int l = 0, int r = sz(all_num) - 1) {
    if (k == 0) 
        return 0;
    if (l == r) {
        if (!(t[v].cnt >= k)) {
            cout << "WTF?\n";
            exit(0);
        }
        if (t[v].cnt == 0) return 0;
        return k * 1ll * (t[v].sum / t[v].cnt);
    }
    int m = (l + r) / 2;
    if (t[v + v + 1].cnt >= k)
        return get(k, v + v + 1, m + 1, r);
    return get(k - t[v + v + 1].cnt, v + v, l, m) + t[v + v + 1].sum;
}

void add(int x) {
    upd(get_id(x), x);
}

int main() {
    #define fn "balls"
    #ifdef witch
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #else
//        freopen(fn".in", "r", stdin);
//        freopen(fn".out", "w", stdout);
    #endif
    srand(time(0));
    cin >> n >> a >> b;
    n += a + b;
    ll sum = 0, ans = -LINF;
    for (int i = 1, x, y, z; i <= n; i++) {
        cin >> x >> y >> z;
        y -= x;
        z -= x;
        sum += x;
        pts[i] = mp(y, z);
    }
    sort(pts + 1, pts + n + 1, greater<pii>());
    //ai, bi -> X, Y
    for (int i = 1; i <= n; i++) {
        //cout << pts[i].f << ' ' << pts[i].s << endl;
        pref[i] = pref[i - 1] + pts[i].f;
        all_num.pb(pts[i].s);
        all_num.pb(pts[i].s - pts[i].f);
    }
    sort(all(all_num));
   // cout << endl;
    multiset<int> A, B;
    ll sum_A = 0, sum_B = 0;
    for (int i = n; i >= 1; i--) {
        if (i >= a && i <= a + b) {
            if (i == a) { continue;
                cout<<b - (i - a)<<endl;
                for(int j=i+1;j<=n;j++){
                    cout<<pts[j].s<<" ";x
                }
                cout << endl;
            }
            suff[i] = get(b - (i - a));
        }
        add(pts[i].s);
    }
    for (int i = 0; i < 4 * N; i++) {
        t[i].cnt = 0;
        t[i].sum = 0;
    }
    for (int i = 1; i < a; i++) {
        add(pts[i].s - pts[i].f);
    }
    for (int i = 1; i <= a + b; i++) {
        add(pts[i].s - pts[i].f);
        // we want to delete from back so that we have chosen already a silver
        if (i >= a) {
            //cout << suff[i] << endl;
            ans = max(ans, pref[i] + suff[i] + get(i - a));
        }
    }
    cout << sum + ans;
    return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:119:41: error: 'x' was not declared in this scope
  119 |                     cout<<pts[j].s<<" ";x
      |                                         ^