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

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FFOR(i, a, b) for (int i = (a); i <= (b); i++)
#define REP(i, b) FOR(i, 0, b)
#define RREP(i, b) FFOR(i, 1, b)
#define PB push_back
#define F first
#define S second
#define BE(c) c.begin(), c.end()
using namespace std;
typedef long long LL;
typedef LL ut;
typedef long double ld;
typedef pair<ut, ut> pr;
typedef vector<pr> Vpr;
typedef vector<ut> VI;
typedef pair<ut, pr> ppr;
typedef vector<ppr> Vppr;
typedef priority_queue<pr, Vpr, greater<pr>> PQ;
typedef priority_queue<ppr, Vppr, greater<ppr>> PpprQ;
const int SIZE = 5 + 1e+6;
const LL INF = 1LL << 60;
const ld eps = 1e-6;
const LL p = 7 + 1e+9;
PpprQ qu;
PQ legi;
LL least[SIZE];
LL N, M, K, D, SS, a, b, c;
int main() {
  cin >> N >> M >> K >> D >> SS;
  REP(i, SIZE)
  least[i] = 0;
  if (N > M) {
    cout << D * K << endl;
    return 0;
  }
  REP(i, M) {
    scanf("%lld %lld %lld", &a, &b, &c);
    qu.push(ppr(a, pr(b, c)));
  }
  FOR(i, 1, N + 1) legi.push(pr(0, i));

  LL legid = 0, score = 0;
  LL loops = 0;
  qu.push(ppr(SS + D, pr(INF, INF)));
  while (!qu.empty() && legid < K) {
    LL target = qu.top().S.S;
    LL times = qu.top().S.F;
    LL start = qu.top().F;
    qu.pop();
    // if(50000<loops++) return 0;
    if (target == INF) {
      while (!legi.empty() && legi.top().F != least[legi.top().S]) {

        // if(50000<loops++) return 0;
        legi.pop();
      }
      if (!legi.empty())
        start = max(start, legi.top().F);
      else
        start = start;
      score = start;
      qu.push(ppr(start + D, pr(INF, INF)));
      legid++;
    } else {
      target = min(target, SIZE - 1LL);
      least[target] = max(least[target], start) + times;
      legi.push(pr(least[target], target));
    }
  }
  cout << score - SS << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FFOR(i, a, b) for (int i = (a); i <= (b); i++)
#define REP(i, b) FOR(i, 0, b)
#define RREP(i, b) FFOR(i, 1, b)
#define PB push_back
#define F first
#define S second
#define BE(c) c.begin(), c.end()
using namespace std;
typedef long long LL;
typedef LL ut;
typedef long double ld;
typedef pair<ut, ut> pr;
typedef vector<pr> Vpr;
typedef vector<ut> VI;
typedef pair<ut, pr> ppr;
typedef vector<ppr> Vppr;
typedef priority_queue<pr, Vpr, greater<pr>> PQ;
typedef priority_queue<ppr, Vppr, greater<ppr>> PpprQ;
const int SIZE = 5 + 1e+6;
const LL INF = 1LL << 60;
const ld eps = 1e-6;
const LL p = 7 + 1e+9;
PpprQ qu;
PQ legi;
LL least[SIZE];
LL N, M, K, D, SS, a, b, c;
int main() {
  cin >> N >> M >> K >> D >> SS;
  REP(i, M) {
    scanf("%lld %lld %lld", &a, &b, &c);
    qu.push(ppr(a, pr(b, c)));
  }
  FOR(i, 1, N + 1) legi.push(pr(0, i));
  if (N > M) {
    cout << D * K << endl;
    return 0;
  }
  LL legid = 0, score = 0;
  LL loops = 0;
  qu.push(ppr(SS + D, pr(INF, INF)));
  while (!qu.empty() && legid < K) {
    LL target = qu.top().S.S;
    LL times = qu.top().S.F;
    LL start = qu.top().F;
    qu.pop();
    // if(50000<loops++) return 0;
    if (target == INF) {
      while (!legi.empty() && legi.top().F != least[legi.top().S]) {

        // if(50000<loops++) return 0;
        legi.pop();
      }
      if (!legi.empty())
        start = max(start, legi.top().F);
      else
        start = start;
      score = start;
      qu.push(ppr(start + D, pr(INF, INF)));
      legid++;
    } else {
      target = min(target, SIZE - 1LL);
      least[target] = max(least[target], start) + times;
      legi.push(pr(least[target], target));
    }
  }
  cout << score - SS << endl;
  return 0;
}