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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
#define fi first
#define se second
#define all(in) in.begin(), in.end()

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

using ll = long long;
using namespace std;

int main() {
  ll t;
  cin >> t;
  rep(count, t) {
    ll n;
    cin >> n;
    vector<pair<ll, ll>> right, left;
    ll ans = 0;
    rep(i, n) {
      ll k, l, r;
      cin >> k >> l >> r;
      if (l < r) {
        ans += l;
        right.pb(make_pair(n - k, r - l));
      } // right
      else if (l > r) {
        ans += r;
        left.pb(make_pair(k, l - r));
      } // left
      else
        ans += l;
    }
    sort(all(right));
    sort(all(left));
    priority_queue<ll, vector<ll>, greater<ll>> que1, que2;
    rep(i, (int)right.size()) {
      if (que1.size() < right[i].fi) {
        ans += right[i].se;
        que1.push(right[i].se);
      } else if (que1.size() && que1.top() < right[i].se) {
        ans -= que1.top();
        que1.pop();
        ans += right[i].se;
        que1.push(right[i].se);
      }
    }
    rep(i, (int)left.size()) {
      if (que2.size() < left[i].fi) {
        ans += left[i].se;
        que2.push(left[i].se);
      } else if (que2.size() && que2.top() < left[i].se) {
        ans -= que2.top();
        que2.pop();
        ans += left[i].se;
        que2.push(left[i].se);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
#define fi first
#define se second
#define all(in) in.begin(), in.end()

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

using ll = long long;
using namespace std;

int main() {
  ll t;
  cin >> t;
  rep(count, t) {
    ll n;
    cin >> n;
    vector<pair<ll, ll>> right, left;
    ll ans = 0;
    rep(i, n) {
      ll k, l, r;
      cin >> k >> l >> r;
      if (l < r) {
        ans += l;
        right.pb(make_pair(n - k, r - l));
      } // right
      else if (l > r) {
        ans += r;
        left.pb(make_pair(k, l - r));
      } // left
      else
        ans += l;
    }
    if ((int)right.size())
      sort(all(right));
    if ((int)left.size())
      sort(all(left));
    priority_queue<ll, vector<ll>, greater<ll>> que1, que2;
    rep(i, (int)right.size()) {
      if ((ll)que1.size() < right[i].fi) {
        ans += right[i].se;
        que1.push(right[i].se);
      } else if (que1.top() < right[i].se) {
        ans -= que1.top();
        que1.pop();
        ans += right[i].se;
        que1.push(right[i].se);
      }
    }
    rep(i, (int)left.size()) {
      if ((ll)que2.size() < left[i].fi) {
        ans += left[i].se;
        que2.push(left[i].se);
      } else if (que2.top() < left[i].se) {
        ans -= que2.top();
        que2.pop();
        ans += left[i].se;
        que2.push(left[i].se);
      }
    }
    cout << ans << endl;
  }
  return 0;
}