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

#include <bits/stdc++.h>
#define P pair<int, int>
using namespace std;
const int N = 2e5 + 5;
int t, n, k[N], l[N], r[N];
priority_queue<P, vector<P>, greater<P>> q;
int a[N], cnta;
long long ans = 0;
bool cmp1(int x, int y) { return k[x] < k[y]; }
bool cmp2(int x, int y) { return k[x] > k[y]; }
int main() {
  cin >> t;
  while (t--) {
    cin >> n;
    for (int i = 1; i <= n; i++)
      cin >> k[i] >> l[i] >> r[i];
    cnta = 0;
    for (int i = 1; i <= n; i++)
      if (l[i] > r[i])
        a[++cnta] = i;
    sort(a + 1, a + cnta + 1, cmp1);
    for (int i = 1; i <= cnta; i++) {
      int now = a[i];
      if (q.size() < k[now])
        q.push(P(l[now] - r[now], now)), ans += l[now];
      else {
        if (q.empty()) {
          ans += r[now];
          continue;
        }
        int x = q.top().second;
        q.pop();
        ans -= l[x];
        if (l[x] - r[x] > l[now] - r[now])
          q.push(P(l[x] - r[x], x)), ans += l[x] + r[now];
        else
          q.push(P(l[now] - r[now], now)), ans += r[x] + l[now];
      }
    }
    //  cout<<ans<<endl;
    while (!q.empty())
      q.pop();
    for (int i = 1; i <= n; i++)
      if (l[i] == r[i])
        ans += l[i];
    cnta = 0;
    for (int i = 1; i <= n; i++)
      if (l[i] < r[i])
        a[++cnta] = i;
    sort(a + 1, a + cnta + 1, cmp2);
    //  for(int i=1;i<=cnta;i++)cout<<a[i]<<" ";cout<<endl;
    for (int i = 1; i <= cnta; i++) {
      int now = a[i];
      if (q.size() < n - k[now])
        q.push(P(r[now] - l[now], now)), ans += r[now];
      else {
        if (q.empty()) {
          ans += l[now];
          continue;
        }
        int x = q.top().second;
        q.pop();
        ans -= r[x];
        if (r[x] - l[x] > r[now] - l[now])
          q.push(P(r[x] - l[x], x)), ans += r[x] + l[now];
        else
          q.push(P(r[now] - l[now], now)), ans += l[x] + r[now];
      }
    }
    while (!q.empty())
      q.pop();
    cout << ans << endl;
    ans = 0;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define P pair<int, int>
using namespace std;
const int N = 1e5 + 5;
int t, n, k[N], l[N], r[N];
priority_queue<P, vector<P>, greater<P>> q;
int a[N], cnta;
long long ans = 0;
bool cmp1(int x, int y) { return k[x] < k[y]; }
bool cmp2(int x, int y) { return k[x] > k[y]; }
int main() {
  cin >> t;
  while (t--) {
    cin >> n;
    for (int i = 1; i <= n; i++)
      cin >> k[i] >> l[i] >> r[i];
    cnta = 0;
    for (int i = 1; i <= n; i++)
      if (l[i] > r[i])
        a[++cnta] = i;
    sort(a + 1, a + cnta + 1, cmp1);
    for (int i = 1; i <= cnta; i++) {
      int now = a[i];
      if (q.size() < k[now])
        q.push(P(l[now], now)), ans += l[now];
      else {
        if (q.empty()) {
          ans += r[now];
          continue;
        }
        int x = q.top().second;
        q.pop();
        ans -= l[x];
        if (l[x] > l[now])
          q.push(P(l[x], x)), ans += l[x] + r[now];
        else
          q.push(P(l[now], now)), ans += r[x] + l[now];
      }
    }
    //  cout<<ans<<endl;
    while (!q.empty())
      q.pop();
    for (int i = 1; i <= n; i++)
      if (l[i] == r[i])
        ans += l[i];
    cnta = 0;
    for (int i = 1; i <= n; i++)
      if (l[i] < r[i])
        a[++cnta] = i;
    sort(a + 1, a + cnta + 1, cmp2);
    //  for(int i=1;i<=cnta;i++)cout<<a[i]<<" ";cout<<endl;
    for (int i = 1; i <= cnta; i++) {
      int now = a[i];
      if (q.size() < n - k[now])
        q.push(P(r[now], now)), ans += r[now];
      else {
        if (q.empty()) {
          ans += l[now];
          continue;
        }
        int x = q.top().second;
        q.pop();
        ans -= r[x];
        if (r[x] > r[now])
          q.push(P(r[x], x)), ans += r[x] + l[now];
        else
          q.push(P(r[now], now)), ans += l[x] + r[now];
      }
    }
    while (!q.empty())
      q.pop();
    cout << ans << endl;
    ans = 0;
  }
  return 0;
}