Problem p03653 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
#define ll long long
using namespace std;

ll A[111111];
ll B[111111];
ll C[111111];

ll dp1[111111];
ll dp2[111111];

ll nA[111111];
ll nB[111111];
ll nC[111111];
int main() {
  ll X, Y, Z;
  cin >> X >> Y >> Z;
  for (int i = 0; i < X + Y + Z; i++) {
    cin >> A[i] >> B[i] >> C[i];
  }

  vector<pair<ll, int>> diff_ids;
  for (int i = 0; i < X + Y + Z; i++) {
    diff_ids.push_back(make_pair(C[i] - B[i], i));
  }

  sort(diff_ids.begin(), diff_ids.end());
  for (int i = 0; i < X + Y + Z; i++) {
    nA[i] = A[diff_ids[i].second];
    nB[i] = B[diff_ids[i].second];
    nC[i] = C[diff_ids[i].second];
  }
  for (int i = 0; i < X + Y + Z; i++) {
    A[i] = nA[i];
    B[i] = nB[i];
    C[i] = nC[i];
    diff_ids[i].second = i;
  }

  set<pair<ll, int>> fwd;
  ll summ = 0;
  for (int i = 0; i < Y; i++) {
    int idd = diff_ids[i].second;
    fwd.insert(make_pair(B[idd] - A[idd], idd));
    summ += B[idd];
  }

  dp1[Y - 1] = summ;

  for (int i = Y; i < X + Y; i++) {
    int idd = diff_ids[i].second;
    fwd.insert(make_pair(B[idd] - A[idd], idd));
    set<pair<ll, int>>::iterator topp = fwd.begin();
    summ += A[topp->second] - B[topp->second];
    summ += B[idd];
    fwd.erase(topp);
    dp1[i] = summ;

    // brute force
    vector<pair<ll, int>> tmp;
    for (int j = 0; j <= i; j++) {
      tmp.push_back(make_pair(A[j] - B[j], j));
    }
    sort(tmp.begin(), tmp.end());
    ll bsumm = 0;
    for (int j = 0; j <= i; j++) {
      int id = tmp[j].second;
      bsumm += (j < Y ? B[id] : A[id]);
    }
  }

  set<pair<ll, int>> bck;
  summ = 0;
  for (int i = X + Y + Z - 1; i >= X + Y; i--) {
    int idd = diff_ids[i].second;
    bck.insert(make_pair(C[idd] - A[idd], idd));
    summ += C[idd];
  }

  dp2[X + Y] = summ;

  for (int i = X + Y - 1; i >= Y; i--) {
    int idd = diff_ids[i].second;
    bck.insert(make_pair(C[idd] - A[idd], idd));
    set<pair<ll, int>>::iterator topp = bck.begin();
    summ += A[topp->second] - C[topp->second];
    summ += C[idd];
    bck.erase(topp);
    dp2[i] = summ;
  }

  // Z-1-(X-x)+1=Z-X+x=x+Y
  ll ans = 0;
  for (int x = 0; x <= X; x++) {
    ans = max(ans, dp1[x + Y - 1] + dp2[x + Y]);
  }

  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
#define ll long long
using namespace std;

ll A[111111];
ll B[111111];
ll C[111111];

ll dp1[111111];
ll dp2[111111];

ll nA[111111];
ll nB[111111];
ll nC[111111];
int main() {
  ll X, Y, Z;
  cin >> X >> Y >> Z;
  for (int i = 0; i < X + Y + Z; i++) {
    cin >> A[i] >> B[i] >> C[i];
  }

  vector<pair<ll, int>> diff_ids;
  for (int i = 0; i < X + Y + Z; i++) {
    diff_ids.push_back(make_pair(C[i] - B[i], i));
  }

  sort(diff_ids.begin(), diff_ids.end());
  for (int i = 0; i < X + Y + Z; i++) {
    nA[i] = A[diff_ids[i].second];
    nB[i] = B[diff_ids[i].second];
    nC[i] = C[diff_ids[i].second];
  }
  for (int i = 0; i < X + Y + Z; i++) {
    A[i] = nA[i];
    B[i] = nB[i];
    C[i] = nC[i];
    diff_ids[i].second = i;
  }

  set<pair<ll, int>> fwd;
  ll summ = 0;
  for (int i = 0; i < Y; i++) {
    int idd = diff_ids[i].second;
    fwd.insert(make_pair(B[idd] - A[idd], idd));
    summ += B[idd];
  }

  dp1[Y - 1] = summ;

  for (int i = Y; i < X + Y; i++) {
    int idd = diff_ids[i].second;
    fwd.insert(make_pair(B[idd] - A[idd], idd));
    set<pair<ll, int>>::iterator topp = fwd.begin();
    summ += A[topp->second] - B[topp->second];
    summ += B[idd];
    fwd.erase(topp);
    dp1[i] = summ;
  }

  set<pair<ll, int>> bck;
  summ = 0;
  for (int i = X + Y + Z - 1; i >= X + Y; i--) {
    int idd = diff_ids[i].second;
    bck.insert(make_pair(C[idd] - A[idd], idd));
    summ += C[idd];
  }

  dp2[X + Y] = summ;

  for (int i = X + Y - 1; i >= Y; i--) {
    int idd = diff_ids[i].second;
    bck.insert(make_pair(C[idd] - A[idd], idd));
    set<pair<ll, int>>::iterator topp = bck.begin();
    summ += A[topp->second] - C[topp->second];
    summ += C[idd];
    bck.erase(topp);
    dp2[i] = summ;
  }

  // Z-1-(X-x)+1=Z-X+x=x+Y
  ll ans = 0;
  for (int x = 0; x <= X; x++) {
    ans = max(ans, dp1[x + Y - 1] + dp2[x + Y]);
  }

  cout << ans << endl;
}