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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define vll vector<ll>
#define vvvl vector<vvl>
#define vvl vector<vector<ll>>
#define VV(a, b, c, d) vector<vector<d>>(a, vector<d>(b, c))
#define VVV(a, b, c, d) vector<vvl>(a, vvl(b, vll(c, d)));
#define re(c, b) for (ll c = 0; c < b; c++)
#define all(obj) (obj).begin(), (obj).end()
typedef long long int ll;
typedef long double ld;
using namespace std;

void solve() {
  ll n;
  std::cin >> n;
  vvl d = VV(n, 3, 0, ll); // l r k

  re(i, n) scanf("%lld %lld %lld", &d[i][2], &d[i][0], &d[i][1]);

  sort(all(d), [](vll a, vll b) { return a[0] - a[1] > b[0] - b[1]; });
  ll ans = 0;
  set<ll> st;
  for (int i = 1; i <= n; i++)
    st.insert(i);
  ll cnt = 0;
  for (int i = 0; i < n; i++)
    if (d[i][0] < d[i][1])
      cnt++;
  for (int i = 0; i < n; i++) {
    if (d[i][0] < d[i][1])
      break;
    // iが可能か？
    auto itr = st.upper_bound(d[i][2]);
    if (itr == st.begin()) {
      ans += d[i][1];
      continue;
    }
    itr--;
    ans += d[i][0];
    st.erase(itr);
  }

  if (cnt == 0) {
    std::cout << ans << '\\n';
    return;
  }

  vvl minus;
  for (int i = 0; i < n; i++) {
    if (d[i][0] < d[i][1]) {
      minus.push_back(d[i]);
    }
  }
  ll m = minus.size();
  st = set<ll>();

  for (int i = 1; i <= m; i++)
    st.insert(i);

  for (int i = 0; i < m; i++) {
    swap(minus[i][0], minus[i][1]);
    minus[i][2] = n - minus[i][2];
  }

  sort(all(minus), [](vll a, vll b) { return a[0] - a[1] > b[0] - b[1]; });

  for (int i = 0; i < cnt; i++) {
    auto itr = st.upper_bound(minus[i][2]);
    if (itr == st.begin()) {
      ans += minus[i][1];
      continue;
    }
    itr--;
    ans += minus[i][0];
    st.erase(itr);
  }
  std::cout << ans << '\\n';
}

int main(int argc, char const *argv[]) {
  ll T;
  std::cin >> T;
  re(t, T) solve();
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define vll vector<ll>
#define vvvl vector<vvl>
#define vvl vector<vector<ll>>
#define VV(a, b, c, d) vector<vector<d>>(a, vector<d>(b, c))
#define VVV(a, b, c, d) vector<vvl>(a, vvl(b, vll(c, d)));
#define re(c, b) for (ll c = 0; c < b; c++)
#define all(obj) (obj).begin(), (obj).end()
typedef long long int ll;
typedef long double ld;
using namespace std;

void solve() {
  ll n;
  std::cin >> n;
  vvl d = VV(n, 3, 0, ll); // l r k

  re(i, n) scanf("%lld %lld %lld", &d[i][2], &d[i][0], &d[i][1]);

  sort(all(d), [](vll a, vll b) { return a[0] - a[1] > b[0] - b[1]; });
  ll ans = 0;
  set<ll> st;
  for (int i = 1; i <= n; i++)
    st.insert(i);
  ll cnt = 0;
  for (int i = 0; i < n; i++)
    if (d[i][0] < d[i][1])
      cnt++;

  ll m = n - cnt; // 正のもの

  for (int i = 0; i < n; i++) {
    if (d[i][0] < d[i][1])
      break;
    // iが可能か？
    auto itr = st.upper_bound(d[i][2]);
    if (itr == st.begin()) {
      ans += d[i][1];
      continue;
    }
    itr--;
    ans += d[i][0];
    st.erase(itr);
  }

  if (cnt == 0) {
    std::cout << ans << '\\n';
    return;
  }

  vvl minus = VV(0, 4, 0, ll);
  for (int i = 0; i < n; i++) {
    if (d[i][0] < d[i][1]) {
      minus.push_back(vll{d[i][1] - d[i][0], d[i][0], d[i][1], d[i][2]});
    }
  }
  sort(all(minus), [](vll a, vll b) { return a[3] < b[3]; });

  multiset<vll> ok, ng;

  ll idx = 0;
  for (int i = 0; i < cnt; i++)
    ng.insert(minus[i]);

  for (int i = m + 1; i <= n; i++) {
    while (idx < minus.size() && minus[idx][3] < i) {
      auto itr = ng.find(minus[idx]);
      ng.erase(itr);
      ok.insert(minus[idx]);
      idx++;
    }
    // okにあればそれ
    if (ok.size()) {
      ans += (*ok.begin())[2];
      ok.erase(ok.begin());
    } else {
      if (ng.size() == 0) {
        std::cout << -1 << '\\n';
        exit(0);
      }
      ans += (*ng.begin())[1];
      ng.erase(ng.begin());
    }
  }
  std::cout << ans << '\\n';
}

int main(int argc, char const *argv[]) {
  ll T;
  std::cin >> T;
  re(t, T) solve();
  return 0;
}