alexjercan/bugnet huggingface dataset:
Problem p02610 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

int main() {
  int t;
  cin >> t;

  for (int tt = 0; tt < t; tt++) {
    int n;
    cin >> n;

    long long int score = 0;
    int a = 0, b = 0;
    vector<vector<long long int>> camel1(n + 1), camel2(n + 1);
    for (int i = 0; i < n; i++) {
      int k;
      long long int l, r;
      cin >> k >> l >> r;

      score += min(l, r);
      if (l >= r) {
        camel1[k].push_back(l - r);
        a += 1;
      }
      if (l < r) {
        camel2[n - k].push_back(r - l);
        b += 1;
      }
    }

    priority_queue<long long int> pq1;
    for (int j = n; j >= a; j--) {
      for (long long int c : camel1[j])
        pq1.push(c);
    }
    if (pq1.empty() == false) {
      score += pq1.top();
      pq1.pop();
    }

    for (int j = a - 1; j >= 1; j--) {
      for (long long int c : camel1[j])
        pq1.push(c);
      if (pq1.empty() == false) {
        score += pq1.top();
        pq1.pop();
      }
    }

    priority_queue<long long int> pq2;
    for (int j = n; j >= b; j--) {
      for (long long int c : camel2[j])
        pq2.push(c);
    }
    if (pq2.empty() == false) {
      score += pq2.top();
      pq2.pop();
    }

    for (int j = b - 1; j >= 1; j--) {
      for (long long int c : camel2[j])
        pq2.push(c);
      if (pq2.empty() == false) {
        score += pq2.top();
        pq2.pop();
      }
    }
    cout << score << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {
  int t;
  cin >> t;

  for (int tt = 0; tt < t; tt++) {
    int n;
    cin >> n;

    long long int score = 0;
    int a = 0, b = 0;
    vector<vector<long long int>> camel1(n + 1), camel2(n + 1);
    for (int i = 0; i < n; i++) {
      int k;
      long long int l, r;
      cin >> k >> l >> r;

      score += min(l, r);
      if (l >= r) {
        camel1[k].push_back(l - r);
        a += 1;
      }
      if (l < r) {
        camel2[n - k].push_back(r - l);
        b += 1;
      }
    }

    priority_queue<long long int> pq1;
    for (int j = n; j >= a; j--) {
      for (long long int c : camel1[j])
        pq1.push(c);
    }
    if (pq1.empty() == false) {
      score += pq1.top();
      pq1.pop();
    }

    for (int j = a - 1; j >= 1; j--) {
      for (long long int c : camel1[j])
        pq1.push(c);
      if (pq1.empty() == false) {
        score += pq1.top();
        pq1.pop();
      }
    }

    priority_queue<long long int> pq2;
    for (int j = 1; j <= a + 1; j++) {
      for (long long int c : camel2[j])
        pq2.push(c);
    }
    if (pq2.empty() == false) {
      score += pq2.top();
      pq2.pop();
    }

    for (int j = a + 2; j <= n; j++) {
      for (long long int c : camel2[j])
        pq2.push(c);
      if (pq2.empty() == false) {
        score += pq2.top();
        pq2.pop();
      }
    }
    cout << score << endl;
  }
  return 0;
}
Problem p02610 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

struct C {
  int k, l, r, diff;
};

int cmp(C &a, C &b) { return a.k < b.k; }

void go() {
  int n;
  cin >> n;
  vector<C> c1, c2;
  ll ans = 0;

  for (int i = 0; i < n; i++) {
    C t;
    cin >> t.k >> t.l >> t.r;
    t.k--;
    t.diff = t.l - t.r;
    if (t.diff > 0)
      c1.pb(t);
    else
      c2.pb(t);
    ans += min(t.l, t.r);
  }

  sort(c1.begin(), c1.end(), cmp);
  sort(c2.begin(), c2.end(), cmp);
  reverse(c2.begin(), c2.end());

  priority_queue<int, vector<int>, greater<int>> pq1;
  int p = 0;
  for (int i = 0; i < n; i++) {
    while (p < c1.size() && c1[p].k <= i) {
      pq1.push(c1[p].diff);
      ans += c1[p].diff;
      p++;
    }
    while (pq1.size() > i + 1) {
      ans -= pq1.top();
      pq1.pop();
    }
  }

  priority_queue<int, vector<int>, greater<int>> pq2;
  int q = 0;
  if (c2.size())
    while (c2[q].k == n - 1)
      q++;
  for (int i = 0; i < n - 1; i++) {
    while (q < c2.size() && c2[q].k >= n - 2 - i) {
      pq2.push(-c2[q].diff);
      ans -= c2[q].diff;
      q++;
    }
    while (pq2.size() > i + 1) {
      ans -= pq2.top();
      pq2.pop();
    }
  }

  cout << ans << endl;
}

int main() {
  ios_base::sync_with_stdio(0);
  int t;
  cin >> t;
  while (t--)
    go();
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

struct C {
  int k, l, r, diff;
};

int cmp(C &a, C &b) { return a.k < b.k; }

void go() {
  int n;
  cin >> n;
  int cntA = 0, cntB = 0;
  vector<C> c1, c2;
  ll ans = 0;

  for (int i = 0; i < n; i++) {
    C t;
    cin >> t.k >> t.l >> t.r;
    t.k--;
    t.diff = t.l - t.r;
    if (t.diff > 0)
      c1.pb(t);
    else
      c2.pb(t);
    ans += min(t.l, t.r);
  }

  sort(c1.begin(), c1.end(), cmp);
  sort(c2.begin(), c2.end(), cmp);
  reverse(c2.begin(), c2.end());

  priority_queue<int, vector<int>, greater<int>> pq1;
  int p = 0;
  for (int i = 0; i < n; i++) {
    while (p < c1.size() && c1[p].k <= i) {
      pq1.push(c1[p].diff);
      ans += c1[p].diff;
      p++;
    }
    while (pq1.size() > i + 1) {
      ans -= pq1.top();
      pq1.pop();
    }
  }

  priority_queue<int, vector<int>, greater<int>> pq2;
  int q = 0;
  while (c2[q].k == n - 1)
    q++;
  for (int i = 0; i < n; i++) {
    while (q < c2.size() && c2[q].k >= n - 1 - i) {
      pq2.push(-c2[q].diff);
      ans -= c2[q].diff;
      q++;
    }
    while (pq2.size() > i + 1) {
      ans -= pq2.top();
      pq2.pop();
    }
  }

  cout << ans << endl;
}

int main() {
  ios_base::sync_with_stdio(0);
  int t;
  cin >> t;
  while (t--)
    go();
}
Problem p02610 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using Z = long;
Z S(vector<pair<int, int>> a) {
  sort(a.begin(), a.end());
  Z A = 0;
  priority_queue<int, vector<int>, greater<>> Q;
  for (auto [k, d] : a) {
    Q.emplace(d);
    A += d;
    while (Q.size() > k)
      A -= Q.top(), Q.pop();
  }
  return A;
}
int main() {
  int q;
  scanf("%d", &q);
  rep(_, q) {
    int n;
    scanf("%d", &n);
    Z sum = 0;
    vector<pair<int, int>> L, R;
    rep(i, n) {
      int k, l, r;
      scanf("%d%d%d", &k, &l, &r);
      if (l > r)
        L.emplace_back(k, l - r), sum += r;
      else if (r > l)
        R.emplace_back(n - k, r - l), sum += l;
      else
        sum += l;
    }
    cout << sum + S(L) + S(R) << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
#pragma warning(disable : 4996)
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

const ld PI = 3.14159265358979323846;
const ll MOD = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

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

bool cmp(kek a, kek b) {
  if (abs(a.l - a.r) == abs(b.r - b.l)) {
    if (a.r > a.l) {
      return true;
    } else {
      return false;
    }
  } else {
    return abs(a.l - a.r) > abs(b.r - b.l);
  }
}

void solve() {
  int n;
  cin >> n;
  vector<kek> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i].k >> a[i].l >> a[i].r;
  }
  sort(a.begin(), a.end(), cmp);
  set<int> s;
  for (int i = 1; i <= n; i++)
    s.insert(i);
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    if (a[i].r >= a[i].l) {
      auto it = s.end();
      it--;
      if (*it > a[i].k) {
        s.erase(it);
        ans += a[i].r;
      } else {
        ans += a[i].l;
      }
    }
  }
  for (int i = 0; i < n; i++) {
    if (a[i].r < a[i].l) {
      auto it = s.upper_bound(a[i].k);
      if (it != s.begin()) {
        it--;
        s.erase(it);
        ans += a[i].l;
      } else {
        ans += a[i].r;
      }
    }
  }
  cout << ans << "\\n";
}

int main() {
#ifdef _DEBUG
  freopen("input1.txt", "r", stdin);
  freopen("output2.txt", "w", stdout);
#endif
  srand(time(0));
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int q;
  cin >> q;
  while (q--)
    solve();
}
Problem p02610 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
typedef long long li;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define df 0
template <class T> void print(const T &t) { cout << t << "\\n"; }
template <class T, class... Ts> void print(const T &t, const Ts &...ts) {
  cout << t;
  if (sizeof...(ts))
    cout << " ";
  print(ts...);
}

li calc(vector<pair<int, li>> &vec) {
  int n = vec.size();
  if (n == 0)
    return 0;
  rep(i, n) vec[i].first *= -1;
  sort(vec.begin(), vec.end());
  rep(i, n) vec[i].first *= -1;
  li s = 0;
  int i = 0, i0 = vec[0].first;
  multiset<li> st;
  while (i0) {
    while (i < n and vec[i].first == i0)
      st.insert(-vec[i++].second);
    if (st.size()) {
      s -= *st.begin();
      st.erase(st.begin());
    }
    i0--;
  }
  return s;
}

void solve(vector<int> &k, vector<li> &l, vector<li> r) {
  int n = k.size();
  vector<pair<int, li>> lft, rgt;
  li s = 0;
  rep(i, n) {
    if (r[i] < l[i]) {
      lft.push_back({k[i], l[i] - r[i]});
      s += r[i];
    } else if (l[i] < r[i]) {
      rgt.push_back({n - k[i], r[i] - l[i]});
      s += l[i];
    } else {
      s += l[i];
    }
  }
  print(s + calc(rgt) + calc(lft));
}

int main() {
  int t;
  cin >> t;
  rep(_, t) {
    int n;
    cin >> n;
    vector<int> k(n);
    vector<li> l(n), r(n);
    rep(i, n) { cin >> k[i] >> l[i] >> r[i]; }
    solve(k, l, r);
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
typedef long long li;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define df 0
template <class T> void print(const T &t) { cout << t << "\\n"; }
template <class T, class... Ts> void print(const T &t, const Ts &...ts) {
  cout << t;
  if (sizeof...(ts))
    cout << " ";
  print(ts...);
}

li calc(vector<pair<int, li>> &vec) {
  int n = vec.size();
  rep(i, n) vec[i].first *= -1;
  sort(vec.begin(), vec.end());
  rep(i, n) vec[i].first *= -1;
  li s = 0;
  int i = 0, i0 = vec[0].first;
  multiset<li> st;
  while (i0) {
    while (i < n and vec[i].first == i0)
      st.insert(-vec[i++].second);
    if (st.size()) {
      s -= *st.begin();
      st.erase(st.begin());
    }
    i0--;
  }
  return s;
}

void solve(vector<int> &k, vector<li> &l, vector<li> r) {
  int n = k.size();
  vector<pair<int, li>> lft, rgt;
  li s = 0;
  rep(i, n) {
    if (r[i] < l[i]) {
      lft.push_back({k[i], l[i] - r[i]});
      s += r[i];
    } else if (l[i] < r[i]) {
      rgt.push_back({n - k[i], r[i] - l[i]});
      s += l[i];
    } else {
      s += l[i];
    }
  }
  print(s + calc(rgt) + calc(lft));
}

int main() {
  int t;
  cin >> t;
  rep(_, t) {
    int n;
    cin >> n;
    vector<int> k(n);
    vector<li> l(n), r(n);
    rep(i, n) { cin >> k[i] >> l[i] >> r[i]; }
    solve(k, l, r);
  }
}
Problem p02610 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    set<int> vis;
    for (int i = 1; i <= n; i++)
      vis.insert(i);
    long long ans = 0;
    vector<pair<int, int>> dp1, dp2;
    for (int i = 1; i <= n; i++) {
      int k, l, r;
      cin >> k >> l >> r;
      dp1.emplace_back(make_pair(l - r, k));
      ans += r;
    }
    sort(dp1.begin(), dp1.end());
    reverse(dp1.begin(), dp1.end());
    for (int i = 0; i < dp1.size(); i++) {
      int val = dp1[i].first, k = dp1[i].second;
      if (val <= 0) {
        dp2.emplace_back(make_pair(val, k));
      } else {
        if (vis.upper_bound(k) == vis.begin())
          dp2.emplace_back(make_pair(val, k));
        else {
          auto it = (--(vis.upper_bound(k)));
          vis.erase(it);
          ans += val;
        }
      }
    }
    int cnt = 0;
    for (int i = 1; i <= n; i++) {
      if (vis.find(i) != vis.end())
        cnt++;
    }
    vis.clear();
    for (int i = n; i >= n - cnt + 1; i--)
      vis.insert(i);
    sort(dp2.begin(), dp2.end());
    for (int i = 0; i < dp2.size(); i++) {
      int val = dp2[i].first, k = dp2[i].second;
      if (vis.upper_bound(k) == vis.end()) {
        ans += val;
        auto it = vis.lower_bound(1);
        vis.erase(it);
      } else {
        auto it = vis.upper_bound(k);
        vis.erase(it);
      }
    }
    cout << ans << "\\n";
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;

    set<int> vis;
    for (int i = 1; i <= n; i++)
      vis.insert(i);
    long long ans = 0;
    vector<pair<int, int>> dp1, dp2;
    for (int i = 1; i <= n; i++) {
      int k, l, r;
      cin >> k >> l >> r;
      dp1.emplace_back(make_pair(l - r, k));
      ans += r;
    }
    sort(dp1.begin(), dp1.end());
    reverse(dp1.begin(), dp1.end());
    for (int i = 0; i < dp1.size(); i++) {
      int val = dp1[i].first, k = dp1[i].second;
      if (val < 0) {
        dp2.emplace_back(make_pair(val, k));
      } else {
        if (vis.upper_bound(k) == vis.end())
          dp2.emplace_back(make_pair(val, k));
        else {
          auto it = (--(vis.upper_bound(k)));
          vis.erase(it);
          ans += val;
        }
      }
    }
    sort(dp2.begin(), dp2.end());
    for (int i = 0; i < dp2.size(); i++) {
      int val = dp2[i].first, k = dp2[i].second;
      if (vis.upper_bound(k) == vis.end())
        ans += val;
      else {
        auto it = vis.upper_bound(k);
        vis.erase(it);
      }
    }
    printf("%lld\\n", ans);
  }
}
Problem p02610 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

const int maxn = 200010;
int T, n;
vector<int> G[maxn], H[maxn];

int main() {
  scanf("%d", &T);
  while (T--) {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
      G[i].clear(), H[i].clear();
    }
    long long ans = 0;
    for (int i = 1, K, L, R; i <= n; i++) {
      scanf("%d %d %d", &K, &L, &R);
      if (L > R)
        ans += R, G[K].push_back(L - R);
      else
        ans += L, H[K].push_back(R - L);
    }
    multiset<int> S;
    for (int i = 1; i <= n; i++) {
      for (int j : G[i]) {
        S.insert(j), ans += j;
      }
      while (S.size() > i) {
        auto it = S.begin();
        ans -= *it, S.erase(it);
      }
    }
    S.clear();
    for (int i = n; i; i--) {
      for (int j : H[i]) {
        S.insert(j), ans += j;
      }
      while (S.size() > n - i) {
        auto it = S.begin();
        ans -= *it, S.erase(it);
      }
    }
    printf("%lld\\n", ans);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

const int maxn = 100010;
int T, n;
vector<int> G[maxn], H[maxn];

int main() {
  scanf("%d", &T);
  while (T--) {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
      G[i].clear(), H[i].clear();
    }
    long long ans = 0;
    for (int i = 1, K, L, R; i <= n; i++) {
      scanf("%d %d %d", &K, &L, &R);
      if (L > R)
        ans += R, G[K].push_back(L - R);
      else
        ans += L, H[K].push_back(R - L);
    }
    multiset<int> S;
    for (int i = 1; i <= n; i++) {
      for (int j : G[i]) {
        S.insert(j), ans += j;
      }
      while (S.size() > i) {
        auto it = S.begin();
        ans -= *it, S.erase(it);
      }
    }
    S.clear();
    for (int i = n; i; i--) {
      for (int j : H[i]) {
        S.insert(j), ans += j;
      }
      while (S.size() > n - i) {
        auto it = S.begin();
        ans -= *it, S.erase(it);
      }
    }
    printf("%lld\\n", ans);
  }
  return 0;
}
Problem p02610 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, int> P;
int main() {
  int t, n, k, l, r, tt, i;
  ll res;
  vector<int> e;
  vector<P> p;
  decltype(e)::iterator it;
  cin >> t;
  for (tt = 0; tt < t; tt++) {
    cin >> n;
    res = 0;
    e.clear();
    p.clear();
    for (i = 0; i < n; i++) {
      cin >> k >> l >> r;
      res += r;
      p.emplace_back(l - r, k - 1);
    }
    sort(p.begin(), p.end(), greater<P>());
    for (i = 0; i < n; i++) {
      if (p[i].first <= 0)
        break;
      e.push_back(i);
    }
    for (i = 0; i < n; i++) {
      if (p[i].first <= 0)
        break;
      it = upper_bound(e.begin(), e.end(), p[i].second);
      if (it == e.begin()) {
        continue;
      }
      e.erase(prev(it));
      res += p[i].first;
    }
    e.clear();
    for (i = n - 1; i >= 0; i--) {
      if (p[i].first >= 0)
        break;
      e.push_back(i);
    }
    for (i = n - 1; i >= 0; i--) {
      if (p[i].first >= 0)
        break;
      it = upper_bound(e.begin(), e.end(), p[i].second + 1, greater<int>());
      if (it == e.begin()) {
        res += p[i].first;
        continue;
      }
      e.erase(prev(it));
    }
    cout << res << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <list>
#include <vector>
using namespace std;
int N_MAX = 200000;
typedef long long ll;
typedef pair<ll, int> P;
int main() {
  int t, n, k, l, r, tt, i, j;
  ll res;
  int c[N_MAX];
  list<int> e;
  vector<P> p;
  decltype(e)::iterator it;
  cin >> t;
  for (tt = 0; tt < t; tt++) {
    cin >> n;
    res = 0;
    fill(c, c + n, -1);
    e.clear();
    p.clear();
    for (i = 0; i < n; i++) {
      cin >> k >> l >> r;
      res += r;
      p.emplace_back(l - r, k - 1);
      e.push_back(i);
    }
    sort(p.begin(), p.end(), greater<P>());
    for (i = 0; i < n; i++) {
      if (p[i].first <= 0)
        break;
      it = upper_bound(e.begin(), e.end(), p[i].second);
      if (it == e.begin()) {
        continue;
      }
      e.erase(prev(it));
      res += p[i].first;
    }
    j = e.size();
    e.clear();
    for (i = n - 1; j > 0; i--, j--)
      e.push_back(i);
    for (i = n - 1; i >= 0; i--) {
      if (p[i].first >= 0)
        break;
      it = upper_bound(e.begin(), e.end(), p[i].second + 1, greater<int>());
      if (it == e.begin()) {
        res += p[i].first;
        continue;
      }
      e.erase(prev(it));
    }
    cout << res << endl;
  }
  return 0;
}
Problem p02610 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define ROF(i, a, b) for (int i = a; i >= b; i--)
using namespace std;

using pii = pair<int, int>;
using vpii = vector<pii>;
using vi = vector<int>;
using vvi = vector<vi>;
using ll = long long;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using vll = vector<ll>;
using vvll = vector<vll>;

void solve() {
  int n;
  cin >> n;
  vi k(n + 1), l(n + 1), r(n + 1);
  vvi a(n + 1), b(n + 1);
  ll ans = 0, cl = 0;
  FOR(i, 1, n) {
    cin >> k[i] >> l[i] >> r[i];
    if (l[i] > r[i] || k[i] == n) {
      a[k[i]].pb(i);
      cl++;
    } else
      b[k[i] + 1].pb(i);
    ans += min(l[i], r[i]);
  }
  priority_queue<int> pq;
  int ap = cl, bp = cl + 1;
  FOR(i, ap + 1, n) {
    for (int j : a[i])
      pq.push(max(0, l[j] - r[j]));
  }
  ROF(i, ap, 1) {
    for (int j : a[i])
      pq.push(max(0, l[j] - r[j]));
    if (!pq.empty()) {
      ans += pq.top();
      pq.pop();
    }
  }
  while (!pq.empty())
    pq.pop();
  FOR(i, 1, ap)
  for (int j : b[i])
    pq.push(r[j] - l[j]);
  FOR(i, bp, n) {
    for (int j : b[i])
      pq.push(r[j] - l[j]);
    if (!pq.empty()) {
      ans += pq.top();
      pq.pop();
    }
  }
  cout << ans << endl;
}

int main() {
  int t;
  cin >> t;
  while (t--)
    solve();
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define ROF(i, a, b) for (int i = a; i >= b; i--)
using namespace std;

using pii = pair<int, int>;
using vpii = vector<pii>;
using vi = vector<int>;
using vvi = vector<vi>;
using ll = long long;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using vll = vector<ll>;
using vvll = vector<vll>;

void solve() {
  int n;
  cin >> n;
  vi k(n + 1), l(n + 1), r(n + 1);
  vvi a(n + 1), b(n + 1);
  ll ans = 0, cl = 0;
  FOR(i, 1, n) {
    cin >> k[i] >> l[i] >> r[i];
    if (l[i] > r[i] || k[i] == n) {
      a[k[i]].pb(i);
      cl++;
    } else
      b[k[i] + 1].pb(i);
    ans += min(l[i], r[i]);
  }
  priority_queue<int, vector<int>, greater<int>> pq;
  int ap = cl, bp = cl + 1;
  FOR(i, ap + 1, n) {
    for (int j : a[i])
      pq.push(max(0, l[j] - r[j]));
  }
  ROF(i, ap, 1) {
    for (int j : a[i])
      pq.push(max(0, l[j] - r[j]));
    ans += pq.top();
    pq.pop();
  }
  FOR(i, 1, ap)
  for (int j : b[i])
    pq.push(r[j] - l[j]);
  FOR(i, bp, n) {
    for (int j : b[i])
      pq.push(r[j] - l[j]);
    ans += pq.top();
    pq.pop();
  }
  cout << ans << endl;
}

int main() {
  int t;
  cin >> t;
  while (t--)
    solve();
}
Problem p02610 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

#define reps(i, s, n) for (int(i) = (s); (i) < (n); (i)++)
#define rep(i, n) reps(i, 0, n)
using namespace std;
using ll = long long;
using pii = pair<int, int>;

ll f(vector<pii> p) {
  int n = p.size();
  rep(i, n) p[i].first = min(p[i].first, n);
  vector<vector<int>> s(n + 1);
  rep(i, n) s[p[i].first].push_back(p[i].second);

  ll res = 0;
  priority_queue<int> q;
  for (int i = n; i >= 1; --i) {
    for (int x : s[i])
      q.push(x);
    if (q.size() > 0) {
      res += q.top();
      q.pop();
    }
  }
  return res;
}

void solve() {
  int n;
  cin >> n;
  vector<pii> pl, pr;
  ll ans = 0;
  rep(i, n) {
    int k, l, r;
    cin >> k >> l >> r;
    int m = min(l, r);
    ans += m;
    l -= m;
    r -= m;
    if (l > 0)
      pl.emplace_back(k, l);
    else
      pr.emplace_back(n - k, r);
  }
  ans += f(pl);
  ans += f(pr);
  cout << ans << endl;
}

int main() {
  int ts;
  cin >> ts;
  rep(i, ts) solve();
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>

#define reps(i, s, n) for (int(i) = (s); (i) < (n); (i)++)
#define rep(i, n) reps(i, 0, n)
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;

void solve() {
  int n;
  cin >> n;
  priority_queue<pll, vector<pll>, function<bool(pll, pll)>> pq(
      [](pll a, pll b) { return a.second > b.second; });
  priority_queue<pll, vector<pll>, function<bool(pll, pll)>> pq2(
      [](pll a, pll b) { return a.second < b.second; });

  vector<pll> vec;
  ll k, l, r, tmp;
  ll ans = 0;

  rep(i, n) {
    cin >> k >> l >> r;
    ans += min(l, r);
    tmp = l - r;
    vec.push_back(make_pair(k, tmp));
  }
  sort(vec.begin(), vec.end(),
       [](const pll &a, const pll &b) { return a.first <= b.first; });

  int pos = 0;
  reps(j, 1, n + 1) {
    while (vec[pos].first == j) {
      if (vec[pos].second >= 0) {
        pq.push(vec[pos]);
      }
      pos++;
    }
    while (pq.size() > j) {
      pq.pop();
    }
  }

  while (!pq.empty()) {
    ans += pq.top().second;
    pq.pop();
  }

  pos = n - 1;
  for (int j = n; j > 0; j--) {
    while (vec[pos].first == j) {
      if (vec[pos].second < 0) {
        pq2.push(vec[pos]);
      }
      pos--;
    }
    while (pq2.size() > n - j) {
      pq2.pop();
    }
  }

  while (!pq2.empty()) {
    ans -= pq2.top().second;
    pq2.pop();
  }

  cout << ans << endl;
}

int main() {
  int t;
  cin >> t;
  rep(i, t) { solve(); }

  return 0;
}