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

#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define ll long long
#define ld long double
#define pii pair<int, int>
#define fi first
#define se second
#define mex 1000005
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
// vector<int> adj[mex];
// a+b=a^b+2*(a&b)

// FIRST THINK, THEN CODE
// DO NOT JUMP BETWEEN PROBLEMS

int main() {
  fast int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int k[n], l[n], r[n];
    for (int i = 0; i < n; i++)
      cin >> k[i] >> l[i] >> r[i];

    priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>
        p1, p2;
    ll s = 0;
    vector<pair<ll, ll>> v1, v2;
    for (int i = 0; i < n; i++) {
      if (l[i] >= r[i]) {
        s = s + r[i];
        v1.push_back({k[i], l[i] - r[i]});
      }
    }
    sort(v1.begin(), v1.end());
    for (int i = 0; i < v1.size(); i++) {
      p1.push({v1[i].se, v1[i].fi});
      if (p1.size() > v1[i].fi)
        p1.pop();
    }
    while (!p1.empty()) {
      s = s + p1.top().fi;
      // cout<<(p1.top().fi)<<endl;
      p1.pop();
    }
    // cout<<s<<endl;
    for (int i = 0; i < n; i++) {
      if (r[i] > l[i]) {
        s = s + l[i];
        v2.push_back({k[i], r[i] - l[i]});
      }
    }
    sort(v2.rbegin(), v2.rend());
    for (int i = 0; i < v2.size(); i++) {
      p2.push({v2[i].se, v2[i].fi});
      if (p2.size() > n - v2[i].fi)
        p2.pop();
    }
    while (!p2.empty()) {
      s = s + p2.top().fi;
      p2.pop();
    }
    cout << s << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define ll long long
#define ld long double
#define pii pair<int, int>
#define fi first
#define se second
#define mex 1000005
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
// vector<int> adj[mex];
// a+b=a^b+2*(a&b)

// FIRST THINK, THEN CODE
// DO NOT JUMP BETWEEN PROBLEMS

int main() {
  fast int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<int> k(n), l(n), r(n);
    for (int i = 0; i < n; i++)
      cin >> k[i] >> l[i] >> r[i];

    ll s = 0;
    for (int i = 0; i < n; i++)
      s = s + l[i];

    vector<pair<int, int>> v(n);
    for (int i = 0; i < n; i++)
      v[i] = make_pair(k[i], l[i] - r[i]);

    sort(v.begin(), v.end());

    int tt = 0;
    priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>
        pq;
    vector<pair<ll, ll>> v1;
    for (int i = 0; i < n; i++) {
      if (tt < v[i].fi) {
        if (v[i].se < 0) {
          s = s - v[i].se;
          v1.push_back(v[i]);
        } else {
          tt += 1;
          pq.push(make_pair(v[i].se, v[i].fi));
        }
      } else if (tt == v[i].fi) {
        if (v[i].se < 0) {
          s = s - v[i].se;
          v1.push_back(v[i]);
          continue;
        }
        pair<ll, ll> y = pq.top();
        if (y.fi < v[i].se) {
          s = s - y.fi;
          pq.pop();
          pq.push(make_pair(v[i].se, v[i].fi));
          v1.push_back({y.se, y.fi});
        } else {
          s = s - v[i].se;
          v1.push_back(v[i]);
        }
      }
    }
    // cout<<v1.size()<<endl;
    sort(v1.begin(), v1.end());
    int i = 0;

    for (; i < v1.size(); i++) {
      if (tt >= v1[i].fi)
        tt += 1;
      else
        break;
    }
    // i-=1;
    // cout<<i<<endl;
    priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>
        pq1;
    for (; i < v1.size(); i++) {
      if (tt < v1[i].fi) {
        tt += 1;
        // cout<<v1[i].se;
        s = s + v1[i].se;
        pq1.push({v1[i].se, v1[i].fi});
      } else {
        pair<ll, ll> p = pq1.top();
        pq1.pop();
        if (p.fi < v1[i].se) {
          s = s - p.fi;
          s = s + v1[i].se;
          pq1.push({v1[i].se, v1[i].fi});
        }
        tt += 1;
      }
    }

    cout << s << "\\n";
  }
}