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

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define int long long
#define endl "\\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007;

struct fast_io {
  fast_io() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
  };
} fio;

signed main() {
  cout << fixed << setprecision(10);

  int T;

  cin >> T;

  for (int _ = 0; _ < T; _++) {
    int N;
    int sum = 0;
    int ans = 0;
    priority_queue<pair<int, int>, vector<pair<int, int>>,
                   greater<pair<int, int>>>
        Q1, Q2;
    vector<pair<int, int>> A, B;

    cin >> N;

    for (int i = 0; i < N; i++) {
      int K, L, R;

      cin >> K >> L >> R;

      K--;

      ans += min(L, R);

      if (L > R)
        A.push_back({K, L - R});
      if (R > L)
        B.push_back({N - K - 2, R - L});
    }

    sort(A.begin(), A.end());
    sort(B.begin(), B.end());

    for (int i = 0; i < A.size(); i++) {
      // cout<<"A "<<A[i].first<<" "<<A[i].second<<endl;
      if (Q1.size() <= A[i].first) {
        Q1.push({A[i].second, A[i].first});
        ans += A[i].second;
      } else {
        pair<int, int> p = Q1.top();
        Q1.pop();

        ans -= p.first;

        if (p.first >= A[i].second) {
          Q1.push(p);
          ans += p.first;
        } else {
          Q1.push({A[i].second, A[i].first});
          ans += A[i].second;
        }
      }
    }

    for (int i = 0; i < B.size(); i++) {
      // cout<<"B "<<B[i].first<<" "<<B[i].second<<endl;
      if (B[i].first <= -1)
        continue;
      if (Q2.size() <= B[i].first) {

        Q2.push({B[i].second, B[i].first});
        ans += B[i].second;
      } else {
        pair<int, int> p = Q2.top();
        Q2.pop();

        ans -= p.first;

        if (p.first >= B[i].second) {
          Q2.push(p);
          ans += p.first;
        } else {
          Q2.push({B[i].second, B[i].first});
          ans += B[i].second;
        }
      }
    }

    cout << ans << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define int long long
#define endl "\\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007;

struct fast_io {
  fast_io() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
  };
} fio;

signed main() {
  cout << fixed << setprecision(10);

  int T;

  cin >> T;

  for (int _ = 0; _ < T; _++) {
    int N;
    int sum = 0;
    int ans = 0;
    // vector<int> in;
    priority_queue<pair<int, int>, vector<pair<int, int>>,
                   greater<pair<int, int>>>
        Q1, Q2;
    vector<pair<int, int>> A, B;

    cin >> N;

    // in.resize(N);

    for (int i = 0; i < N; i++) {
      int K, L, R;

      cin >> K >> L >> R;

      K--;

      ans += min(L, R);

      if (L >= R)
        A.push_back({K, L - R});
      if (R >= L)
        B.push_back({N - K - 2, R - L});
    }

    sort(A.begin(), A.end());
    sort(B.begin(), B.end());

    for (int i = 0; i < A.size(); i++) {
      // cout<<"A "<<A[i].first<<" "<<A[i].second<<endl;
      if (Q1.size() <= A[i].first) {
        Q1.push(B[i]);
        ans += A[i].second;
      } else {
        pair<int, int> p = Q1.top();
        Q1.pop();

        ans -= p.second;

        if (p.second >= A[i].second) {
          Q1.push(p);
          ans += p.second;
        } else {
          Q1.push(A[i]);
          ans += A[i].second;
        }
      }
    }

    for (int i = 0; i < B.size(); i++) {
      // cout<<"B "<<B[i].first<<" "<<B[i].second<<endl;
      if (B[i].first == -1)
        continue;
      if (Q2.size() <= B[i].first) {
        Q2.push(B[i]);
        ans += B[i].second;
      } else {
        pair<int, int> p = Q2.top();
        Q2.pop();

        ans -= p.second;

        if (p.second >= B[i].second) {
          Q2.push(p);
          ans += p.second;
        } else {
          Q2.push(B[i]);
          ans += B[i].second;
        }
      }
    }

    cout << ans << endl;
  }

  return 0;
}