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

#include <algorithm>
#include <assert.h>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <queue>
#include <set>
#include <string.h>
#include <unordered_map>
#include <vector>
using namespace std;
using ll = long long;
using ld = long double;
const int mxn = 2e5 + 20;

vector<pair<int, int>> adj[mxn];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int tc;
  cin >> tc;
  while (tc--) {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++)
      adj[i].clear();
    vector<pair<int, pair<int, int>>> camels;
    for (int i = 0; i < n; i++) {
      int k, l, r;
      pair<int, pair<int, int>> tmp;
      cin >> k >> l >> r;
      tmp.first = k;
      tmp.second = {l, r};
      camels.push_back(tmp);
    }
    for (auto x : camels)
      if (x.second.first >= x.second.second)
        adj[x.first].push_back(x.second);

    multiset<int> Set;
    ll added = 0ll;
    for (int i = 1; i <= n; i++) {
      for (auto x : adj[i]) {
        Set.insert(x.first - x.second);
        assert(x.first - x.second >= 0);
        added += 1ll * (x.first - x.second);
      }
      while (i < (int)Set.size()) {
        added -= 1ll * (*Set.begin());
        Set.erase(Set.begin());
      }
    }
    // cout << "added is " << added << '\\n';

    for (int i = 1; i <= n; i++)
      adj[i].clear();

    for (auto x : camels)
      if (x.second.first < x.second.second)
        adj[x.first + 1].push_back(x.second);

    Set.clear();
    ll addedmore = 0ll;
    for (int i = n + 1; i >= 2; i--) {
      for (auto x : adj[i]) {
        Set.insert(x.second - x.first);
        assert(x.second - x.first > 0);
        addedmore += 1ll * (x.second - x.first);
      }
      while (n + 1 - i < (int)Set.size()) {
        addedmore -= 1ll * (*Set.begin());
        Set.erase(Set.begin());
      }
    }
    // cout << "addedmore is " << addedmore << '\\n';

    ll ans = 0;
    for (auto x : camels)
      ans += 1ll * min(x.second.first, x.second.second);

    // cout << "Original ans = " << ans << '\\n';
    ans += added;
    ans += addedmore;
    cout << ans << '\\n';
  }

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <queue>
#include <set>
#include <string.h>
#include <unordered_map>
#include <vector>
using namespace std;
using ll = long long;
using ld = long double;
const int mxn = 1e5 + 10;

vector<pair<int, int>> adj[mxn];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int tc;
  cin >> tc;
  while (tc--) {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++)
      adj[i].clear();
    vector<pair<int, pair<int, int>>> camels;
    for (int i = 0; i < n; i++) {
      int k, l, r;
      pair<int, pair<int, int>> tmp;
      cin >> k >> l >> r;
      tmp.first = k;
      tmp.second = {l, r};
      camels.push_back(tmp);
    }
    for (auto x : camels)
      if (x.second.first >= x.second.second)
        adj[x.first].push_back(x.second);

    multiset<int> Set;
    ll added = 0ll;
    for (int i = 1; i <= n; i++) {
      for (auto x : adj[i]) {
        Set.insert(x.first - x.second);
        assert(x.first - x.second >= 0);
        added += 1ll * (x.first - x.second);
      }
      while (i < (int)Set.size()) {
        added -= 1ll * (*Set.begin());
        Set.erase(Set.begin());
      }
    }
    // cout << "added is " << added << '\\n';

    for (int i = 1; i <= n; i++)
      adj[i].clear();

    for (auto x : camels)
      if (x.second.first < x.second.second)
        adj[x.first + 1].push_back(x.second);

    Set.clear();
    ll addedmore = 0ll;
    for (int i = n + 1; i >= 2; i--) {
      for (auto x : adj[i]) {
        Set.insert(x.second - x.first);
        assert(x.second - x.first > 0);
        addedmore += 1ll * (x.second - x.first);
      }
      while (n + 1 - i < (int)Set.size()) {
        addedmore -= 1ll * (*Set.begin());
        Set.erase(Set.begin());
      }
    }
    // cout << "addedmore is " << addedmore << '\\n';

    ll ans = 0;
    for (auto x : camels)
      ans += 1ll * min(x.second.first, x.second.second);

    // cout << "Original ans = " << ans << '\\n';
    ans += added;
    ans += addedmore;
    cout << ans << '\\n';
  }

  return 0;
}