Problem p02610 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <cmath>
#include <deque>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define ll long long
#define rep2(i, a, b) for (int i = a; i <= b; ++i)
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep3(i, a, b) for (int i = a; i >= b; i--)
#define REP(e, v) for (auto e : v)
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mp make_pair
#define mt make_tuple
#define pq priority_queue<int>
#define pqg priority_queue<int, vector<int>, greater<int>>
#define pb push_back
#define vec vector<int>
#define vecll vector<ll>
#define vecpii vector<pii>
#define vecpll vector<pll>
#define vecbl vector<bool>
#define endl "\n"
#define ALL(c) (c).begin(), (c).end()
using namespace std;
int in() {
  int x;
  scanf("%d", &x);
  return x;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}
string stin() {
  string s;
  cin >> s;
  return s;
}

void func() {
  int n = in();
  ll ans = 0;
  vector<vector<pii>> que(2);
  rep(i, n) {
    int k = in(), l = in(), r = in();
    if (l > r) {
      ans += r;
      l -= r;
      que[0].pb(mp(-k, l));
    } else {
      ans += l;
      r -= l;
      que[1].pb(mp(-(n - k), r));
    }
  }
  rep(x, 2) {
    sort(ALL(que[x]));
    pq p;
    int tmp = n;
    int id = 0;
    while (tmp >= 1) {
      while (id < n) {
        if (que[x][id].first == -tmp) {
          p.push(que[x][id].second);
          id++;
        } else {
          break;
        }
      }
      if (!p.empty()) {
        ans += p.top();
        p.pop();
      }
      tmp--;
    }
  }
  cout << ans << endl;
}

int main() {
  int t = in();
  rep(i, t) func();
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <cmath>
#include <deque>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define ll long long
#define rep2(i, a, b) for (int i = a; i <= b; ++i)
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep3(i, a, b) for (int i = a; i >= b; i--)
#define REP(e, v) for (auto e : v)
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mp make_pair
#define mt make_tuple
#define pq priority_queue<int>
#define pqg priority_queue<int, vector<int>, greater<int>>
#define pb push_back
#define vec vector<int>
#define vecll vector<ll>
#define vecpii vector<pii>
#define vecpll vector<pll>
#define vecbl vector<bool>
#define endl "\n"
#define ALL(c) (c).begin(), (c).end()
using namespace std;
int in() {
  int x;
  scanf("%d", &x);
  return x;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}
string stin() {
  string s;
  cin >> s;
  return s;
}

void func() {
  int n = in();
  ll ans = 0;
  vector<vector<pii>> que(2);
  rep(i, n) {
    int k = in(), l = in(), r = in();
    if (l > r) {
      ans += r;
      l -= r;
      que[0].pb(mp(-k, l));
    } else {
      ans += l;
      r -= l;
      que[1].pb(mp(-(n - k), r));
    }
  }
  rep(x, 2) {
    sort(ALL(que[x]));
    pq p;
    int tmp = n;
    int id = 0;
    while (tmp >= 1) {
      while (id < que[x].size()) {
        if (-que[x][id].first == tmp) {
          p.push(que[x][id].second);
          id++;
        } else {
          break;
        }
      }
      if (!p.empty()) {
        ans += p.top();
        p.pop();
      }
      tmp--;
    }
  }
  cout << ans << endl;
}

int main() {
  int t = in();
  rep(i, t) func();
}