Problem p03653 (C++)

Buggy submission (status: Runtime Error):

#include <algorithm>
#include <iostream>
#include <numeric>
#include <queue>
#include <utility>
#include <vector>

template <class T, class U> inline bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

template <class T, class U> inline bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

class top_k {
private:
  const size_t size;
  int64_t sum;
  std::priority_queue<int, std::vector<int>, std::greater<int>> que;

public:
  top_k(size_t size_) : size(size_), sum(0) {}

  int64_t fold() const { return sum; }

  void push(int value) {
    sum += value;
    que.push(value);
    if (que.size() > size) {
      sum -= que.top();
      que.pop();
    }
  }
};

constexpr int64_t inf = (int64_t(1) << 60);

int main() {
  int X, Y, Z;
  std::cin >> X >> Y >> Z;
  int N = X + Y + Z;
  std::vector<int> A(N), B(N), C(N);
  for (int i = 0; i < N; ++i) {
    std::cin >> A[i] >> B[i] >> C[i];
    B[i] -= A[i];
    C[i] -= A[i];
  }
  std::vector<int> order(N);
  std::iota(order.begin(), order.end(), 0);
  std::sort(order.begin(), order.end(),
            [&](int i, int j) { return B[i] - C[i] >= B[j] - C[j]; });
  top_k top_b(Y), top_c(Z);
  std::vector<int64_t> sum_b(N + 1), sum_c(N + 1);
  for (int i = 0; i < N; ++i) {
    top_b.push(B[order[i]]);
    sum_b[i + 1] = top_b.fold();
  }
  for (int i = N - 1; i >= 0; --i) {
    top_c.push(C[order[i]]);
    sum_c[N - i] = top_c.fold();
  }
  int64_t max = -inf;
  for (int i = 0; i < N; ++i) {
    if (i + 1 >= Y && N - i - 1 >= Z) {
      chmax(max, sum_b[i + 1] + sum_c[N - i - 1]);
    }
  }
  std::cout << max + std::accumulate(A.cbegin(), A.cend(), int64_t(0)) << '\n';
  return 0;
}


Fixed submission:

#include <algorithm>
#include <iostream>
#include <numeric>
#include <queue>
#include <utility>
#include <vector>

template <class T, class U> inline bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

template <class T, class U> inline bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

class top_k {
private:
  const size_t size;
  int64_t sum;
  std::priority_queue<int, std::vector<int>, std::greater<int>> que;

public:
  top_k(size_t size_) : size(size_), sum(0) {}

  int64_t fold() const { return sum; }

  void push(int value) {
    sum += value;
    que.push(value);
    if (que.size() > size) {
      sum -= que.top();
      que.pop();
    }
  }
};

constexpr int64_t inf = (int64_t(1) << 60);

int main() {
  int X, Y, Z;
  std::cin >> X >> Y >> Z;
  int N = X + Y + Z;
  std::vector<int> A(N), B(N), C(N);
  for (int i = 0; i < N; ++i) {
    std::cin >> A[i] >> B[i] >> C[i];
    B[i] -= A[i];
    C[i] -= A[i];
  }
  std::vector<int> order(N);
  std::iota(order.begin(), order.end(), 0);
  std::sort(order.begin(), order.end(),
            [&](int i, int j) { return B[i] - C[i] > B[j] - C[j]; });
  top_k top_b(Y), top_c(Z);
  std::vector<int64_t> sum_b(N + 1), sum_c(N + 1);
  for (int i = 0; i < N; ++i) {
    top_b.push(B[order[i]]);
    sum_b[i + 1] = top_b.fold();
  }
  for (int i = N - 1; i >= 0; --i) {
    top_c.push(C[order[i]]);
    sum_c[N - i] = top_c.fold();
  }
  int64_t max = -inf;
  for (int i = 0; i < N; ++i) {
    if (i + 1 >= Y && N - i - 1 >= Z) {
      chmax(max, sum_b[i + 1] + sum_c[N - i - 1]);
    }
  }
  std::cout << max + std::accumulate(A.cbegin(), A.cend(), int64_t(0)) << '\n';
  return 0;
}