Problem p02868 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long LL;
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X)                                                                 \
  int(X);                                                                      \
  scanf("%d", &X)
#define DRII(X, Y)                                                             \
  int X, Y;                                                                    \
  scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z)                                                         \
  int X, Y, Z;                                                                 \
  scanf("%d%d%d", &X, &Y, &Z)
#define RS(X) scanf("%s", (X))
#define CASET                                                                  \
  int ___T, case_n = 1;                                                        \
  scanf("%d ", &___T);                                                         \
  while (___T-- > 0)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define PII pair<int, int>
#define VI vector<int>
#define VL vector<long long>
#define VPII vector<pair<int, int>>
#define PLL pair<long long, long long>
#define VPLL vector<pair<long long, long long>>
#define F first
#define S second

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  set<pair<LL, int>> paths;
  set<pair<int, pair<LL, int>>> eds;
  DRII(n, m);
  VI res(n);
  REP(i, m) {
    DRIII(l, r, c);
    l--;
    r--;
    eds.insert({l, {c, r}});
  }
  paths.insert({0, 0});
  int curr = 0;
  auto it = eds.begin();
  LL cc = 0;
  while (curr < n - 1 and !paths.empty()) {
    res[curr] = cc;
    while (it != eds.end() and it->F <= curr) {
      paths.insert({cc + (it->S).F, (it->S).S});
      it++;
    }
    auto it2 = paths.begin();
    while (it2 != paths.end() and it2->S <= curr)
      paths.erase(it2++);
    int next = it2->S;
    cc = it2->F;
    REPP(i, curr + 1, next + 1) { res[i] = cc; }
    curr = next;
  }
  if (curr == n - 1) {
    printf("%lld\n", cc);
  } else {
    printf("-1\n");
  }
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long LL;
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X)                                                                 \
  int(X);                                                                      \
  scanf("%d", &X)
#define DRII(X, Y)                                                             \
  int X, Y;                                                                    \
  scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z)                                                         \
  int X, Y, Z;                                                                 \
  scanf("%d%d%d", &X, &Y, &Z)
#define RS(X) scanf("%s", (X))
#define CASET                                                                  \
  int ___T, case_n = 1;                                                        \
  scanf("%d ", &___T);                                                         \
  while (___T-- > 0)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define PII pair<int, int>
#define VI vector<int>
#define VL vector<long long>
#define VPII vector<pair<int, int>>
#define PLL pair<long long, long long>
#define VPLL vector<pair<long long, long long>>
#define F first
#define S second

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  set<pair<LL, int>> paths;
  set<pair<int, pair<LL, int>>> eds;
  DRII(n, m);
  VI res(n);
  REP(i, m) {
    DRIII(l, r, c);
    l--;
    r--;
    eds.insert({l, {c, r}});
  }
  paths.insert({0, 0});
  int curr = 0;
  auto it = eds.begin();
  LL cc = 0;
  while (curr < n - 1 and !paths.empty()) {
    res[curr] = cc;
    while (it != eds.end() and it->F <= curr) {
      paths.insert({cc + (it->S).F, (it->S).S});
      it++;
    }
    auto it2 = paths.begin();
    while (it2 != paths.end() and it2->S <= curr)
      paths.erase(it2++);
    if (paths.empty())
      continue;
    int next = it2->S;
    cc = it2->F;
    REPP(i, curr + 1, next + 1) { res[i] = cc; }
    curr = next;
  }
  if (curr == n - 1) {
    printf("%lld\n", cc);
  } else {
    printf("-1\n");
  }
}