Problem p02610 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;
typedef pair<int, int> Pi;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define fi first
#define se second
#define endl "\\n"

template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> ostream &operator<<(ostream &s, const complex<T> &d) {
  return s << "(" << d.real() << ", " << d.imag() << ")";
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &d) {
  return s << "(" << d.first << ", " << d.second << ")";
}
template <typename T> ostream &operator<<(ostream &s, const vector<T> &d) {
  int len = d.size();
  rep(i, len) {
    s << d[i];
    if (i < len - 1)
      s << " ";
  }
  return s;
}
template <typename T>
ostream &operator<<(ostream &s, const vector<vector<T>> &d) {
  int len = d.size();
  rep(i, len) { s << d[i] << endl; }
  return s;
}
template <typename T> ostream &operator<<(ostream &s, const set<T> &v) {
  s << "{ ";
  for (auto itr = v.begin(); itr != v.end(); ++itr) {
    if (itr != v.begin()) {
      s << ", ";
    }
    s << (*itr);
  }
  s << " }";
  return s;
}
template <typename T> ostream &operator<<(ostream &s, const multiset<T> &v) {
  s << "{ ";
  for (auto itr = v.begin(); itr != v.end(); ++itr) {
    if (itr != v.begin()) {
      s << ", ";
    }
    s << (*itr);
  }
  s << " }";
  return s;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const map<T1, T2> &m) {
  s << "{" << endl;
  for (auto itr = m.begin(); itr != m.end(); ++itr) {
    s << " " << (*itr).first << " : " << (*itr).second << endl;
  }
  s << "}" << endl;
  return s;
}

const ll mod = 1'000'000'007;
const ll inf = 1'000'000'000'000'000'00;
const int INF = 1'000'000'000;
const double EPS = 1e-10;
const double PI = acos(-1);

struct state {
  ll k, l, r;
};

void solve() {
  ll n;
  cin >> n;
  ll ans = 0;
  vector<vector<ll>> left(n + 1), right(n + 1);
  rep(i, n) {
    ll k, l, r;
    cin >> k >> l >> r;
    if (l >= r) {
      left[k].push_back(l - r);
      ans += r;
    } else {
      right[k].push_back(r - l);
      ans += l;
    }
  }
  priority_queue<ll, vector<ll>, greater<ll>> que;
  FOR(k, 1, n + 1) {
    for (auto x : left[k])
      que.push(x);
    while (que.size() > k)
      que.pop();
  }
  while (!que.empty()) {
    ans += que.top();
    que.pop();
  }
  for (int k = n; k >= 1; k--) {
    for (auto x : right[k])
      que.push(x);
    while (que.size() > n - k)
      que.pop();
  }
  while (!que.empty()) {
    ans += que.top();
    que.pop();
  }
  cout << ans << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--)
    solve();
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;
typedef pair<int, int> Pi;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define fi first
#define se second
#define endl "\\n"

template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> ostream &operator<<(ostream &s, const complex<T> &d) {
  return s << "(" << d.real() << ", " << d.imag() << ")";
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &d) {
  return s << "(" << d.first << ", " << d.second << ")";
}
template <typename T> ostream &operator<<(ostream &s, const vector<T> &d) {
  int len = d.size();
  rep(i, len) {
    s << d[i];
    if (i < len - 1)
      s << " ";
  }
  return s;
}
template <typename T>
ostream &operator<<(ostream &s, const vector<vector<T>> &d) {
  int len = d.size();
  rep(i, len) { s << d[i] << endl; }
  return s;
}
template <typename T> ostream &operator<<(ostream &s, const set<T> &v) {
  s << "{ ";
  for (auto itr = v.begin(); itr != v.end(); ++itr) {
    if (itr != v.begin()) {
      s << ", ";
    }
    s << (*itr);
  }
  s << " }";
  return s;
}
template <typename T> ostream &operator<<(ostream &s, const multiset<T> &v) {
  s << "{ ";
  for (auto itr = v.begin(); itr != v.end(); ++itr) {
    if (itr != v.begin()) {
      s << ", ";
    }
    s << (*itr);
  }
  s << " }";
  return s;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const map<T1, T2> &m) {
  s << "{" << endl;
  for (auto itr = m.begin(); itr != m.end(); ++itr) {
    s << " " << (*itr).first << " : " << (*itr).second << endl;
  }
  s << "}" << endl;
  return s;
}

const ll mod = 1'000'000'007;
const ll inf = 1'000'000'000'000'000'00;
const int INF = 1'000'000'000;
const double EPS = 1e-10;
const double PI = acos(-1);

struct state {
  ll k, l, r;
};

void solve() {
  ll n;
  cin >> n;
  vector<state> g;
  rep(i, n) {
    ll k, l, r;
    cin >> k >> l >> r;
    g.push_back({k, l, r});
  }
  sort(begin(g), end(g),
       [](const state &a, const state &b) { return a.k < b.k; });
  vector<vector<ll>> dp(n + 1, vector<ll>(n + 1, -1));
  dp[0][0] = 0;
  rep(i, n) rep(j, n) {
    if (dp[i][j] == -1)
      continue;
    auto [k, l, r] = g[i];
    if (k > j)
      chmax(dp[i + 1][j + 1], dp[i][j] + l);
    if (k < n - (i - j))
      chmax(dp[i + 1][j], dp[i][j] + r);
  }
  ll ans = -1;
  rep(j, n + 1) chmax(ans, dp[n][j]);
  cout << ans << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--)
    solve();
}