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

// love yjl forever
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<ll, ll>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
const int INF = 0x3f3f3f3f, N = 5e5 + 5;
int n, n1;
struct node {
  ll k, l, r;
} a[N];
bool cmp1(node x, node y) { return x.l - x.r > y.l - y.r; }
bool cmp(node x, node y) { return x.k > y.k; }
bool cmp2(node x, node y) { return x.k < y.k; }
void slove2() {
  priority_queue<ll> q;
  ll ans = 0;

  int j = 1, i;
  for (i = 1; i <= n1; i++) {
    while (a[j].k == i && j <= n1) {
      ans += a[j].l;
      q.push(-a[j].l + a[j].r);
      j++;
    }

    while (q.size() > i) {
      int x = q.top();
      q.pop();
      ans += x;
    }
  }

  while (j <= n1)
    ans += a[j].l, j++;

  //	cout<<ans<<endl;

  while (!q.empty())
    q.pop();

  j = n;
  while (a[j].k > n) {
    ans += a[j].l;
    j--;
  }

  for (int i = n; i > n1; i--) {
    while (a[j].k == i && j > n1) {
      q.push(a[j].l - a[j].r);
      ans += a[j].r;
      j--;
    }

    while (q.size() > n - i + 1) {
      ll x = q.top();
      q.pop();
      ans += x;
    }
  }
  while (j > n1) {
    ans += a[j].r;
    j--;
  }
  cout << ans << endl;
}
int main() {
  //	freopen("b.in","r",stdin);
  //	freopen("b.out","w",stdout);
  int i, T;
  cin >> T;
  while (T--) {
    cin >> n;
    for (i = 1; i <= n; i++)
      scanf("%d%d%d", &a[i].k, &a[i].l, &a[i].r);

    sort(a + 1, a + n + 1, cmp1);

    for (i = 1; i <= n; i++) {
      if (a[i].l < a[i].r)
        break;
    }
    n1 = i;
    n1--;
    //		cout<<n1<<endl;
    //		cout<<endl;
    //		for(i=1;i<=n;i++)
    //			printf("%d %d %d\\n",a[i].k,a[i].l,a[i].r);
    sort(a + 1, a + n1 + 1, cmp2);
    //		cout<<endl;
    //		for(i=1;i<=n;i++)
    //			printf("%d %d %d\\n",a[i].k,a[i].l,a[i].r);
    sort(a + n1 + 1, a + n + 1, cmp2);

    for (i = n1 + 1; i <= n; i++)
      a[i].k++;

    //		cout<<endl;
    //		for(i=1;i<=n;i++)
    //			printf("%d %d %d\\n",a[i].k,a[i].l,a[i].r);
    //		cout<<endl;
    slove2();
  }
}
/*
3

3
2 1 9
2 6 6
3 7 4

4
4 7 8
4 7 9
4 1 5
1 10 10

8
5 1 5
1 4 5
1 8 7
5 8 2
1 1 2
8 8 2
1 3 7
5 9 8
*/

Error: WA

Fixed submission:

// love yjl forever
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<ll, ll>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
const int INF = 0x3f3f3f3f, N = 5e5 + 5;
int n;
struct node {
  ll k, l, r;
} a[N];
ll b[N];
void slove1() {
  ll ans = 0, i;
  do {
    ll tmp = 0;
    for (i = 1; i <= n; i++)
      tmp += (b[i] <= a[i].k ? a[i].l : a[i].r);
    ans = max(ans, tmp);
  } while (next_permutation(b + 1, b + n + 1));
  cout << ans << endl;
}
bool cmp(node x, node y) { return x.k > y.k; }
void slove2() {
  sort(a + 1, a + n + 1, cmp);
  priority_queue<ll> q;
  ll ans = 0;
  int j = 1;
  for (int i = n - 1; i >= 0; i--) {
    while (a[j].k == i + 1 && j <= n) {
      q.push(a[j].l - a[j].r);
      ans += a[j].r;
      j++;
    }
    //	cout<<ans<<endl;

    while ((q.size() > n - i + 1 || (q.top() > 0)) && !q.empty()) {
      ll x = q.top();
      q.pop();
      ans += x;
    }
    //	cout<<ans<<endl;
  }
  cout << ans << endl;
}
int main() {
  //	freopen("b.in","r",stdin);
  //	freopen("b.out","w",stdout);
  int i, T;
  cin >> T;
  while (T--) {
    cin >> n;
    for (i = 1; i <= n; i++) {
      scanf("%d%d%d", &a[i].k, &a[i].l, &a[i].r);
      b[i] = i;
    }
    if (n <= 10) {
      slove1();
    } else
      slove2();
  }
}
/*
3

3
2 1 9
2 6 6
3 7 4

4
4 7 8
4 7 9
4 1 5
1 10 10

8
5 1 5
1 4 5
1 8 7
5 8 2
1 1 2
8 8 2
1 3 7
5 9 8
*/