Problem p03653 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#define mkp make_pair
#define mkt make_tuple
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int X, Y, Z;
  cin >> X >> Y >> Z;
  int N = X + Y + Z;
  vector<ll> A(N), B(N), C(N);
  rep(i, N) cin >> A[i] >> B[i] >> C[i];

  vector<int> ord(N, 0);
  rep(i, N) ord[i] = i;
  sort(ord.begin(), ord.end(), [&](int a, int b) {
    if (A[a] - B[a] > A[b] - B[b])
      return a > b;
    return a < b;
  });

  vector<ll> left(N, 0);
  ll sum = 0;
  priority_queue<ll> PQ;
  rep(i, X) {
    int now = ord[i];
    sum += A[now];
    PQ.push(C[now] - A[now]);
  }
  left[X - 1] = sum;
  for (int i = X; i < X + Z; i++) {
    int now = ord[i];
    sum += A[now];
    PQ.push(C[now] - A[now]);
    auto f = PQ.top();
    PQ.pop();
    sum += f;
    left[i] = sum;
  }

  vector<ll> right(N, 0);
  sum = 0;
  while (!PQ.empty())
    PQ.pop();
  for (int i = 1; i <= Y; i++) {
    int now = ord[N - i];
    sum += B[now];
    PQ.push(C[now] - B[now]);
  }
  right[N - Y] = sum;
  for (int i = N - Y - 1; i >= X; i--) {
    int now = ord[i];
    sum += B[now];
    PQ.push(C[now] - B[now]);
    auto f = PQ.top();
    PQ.pop();
    sum += f;
    right[i] = sum;
  }

  ll ans = 0;
  for (int i = X; i <= N - Y; i++)
    chmax(ans, left[i - 1] + right[i]);
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#define mkp make_pair
#define mkt make_tuple
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int X, Y, Z;
  cin >> X >> Y >> Z;
  int N = X + Y + Z;
  vector<ll> A(N), B(N), C(N);
  rep(i, N) cin >> A[i] >> B[i] >> C[i];

  vector<int> ord(N, 0);
  rep(i, N) ord[i] = i;
  sort(ord.begin(), ord.end(),
       [&](int a, int b) { return A[a] - B[a] > A[b] - B[b]; });

  vector<ll> left(N, 0);
  ll sum = 0;
  priority_queue<ll> PQ;
  rep(i, X) {
    int now = ord[i];
    sum += A[now];
    PQ.push(C[now] - A[now]);
  }
  left[X - 1] = sum;
  for (int i = X; i < X + Z; i++) {
    int now = ord[i];
    sum += A[now];
    PQ.push(C[now] - A[now]);
    auto f = PQ.top();
    PQ.pop();
    sum += f;
    left[i] = sum;
  }

  vector<ll> right(N, 0);
  sum = 0;
  while (!PQ.empty())
    PQ.pop();
  for (int i = 1; i <= Y; i++) {
    int now = ord[N - i];
    sum += B[now];
    PQ.push(C[now] - B[now]);
  }
  right[N - Y] = sum;
  for (int i = N - Y - 1; i >= X; i--) {
    int now = ord[i];
    sum += B[now];
    PQ.push(C[now] - B[now]);
    auto f = PQ.top();
    PQ.pop();
    sum += f;
    right[i] = sum;
  }

  ll ans = 0;
  for (int i = X; i <= N - Y; i++)
    chmax(ans, left[i - 1] + right[i]);
  cout << ans << endl;

  return 0;
}