Problem p02868 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
typedef pair<int, int> pii;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define debug(x)                                                               \
  do {                                                                         \
    cerr << #x << ": " << x << "\n";                                           \
  } while (0)
#define debugv(x)                                                              \
  do {                                                                         \
    cerr << #x << ": ";                                                        \
    for (auto &e : (x))                                                        \
      cerr << e << " ";                                                        \
    cerr << "\n";                                                              \
  } while (0)
#define fori(i, n) for (int i = 0; i < (int)(n); ++i)
#define forn(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n); i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define RETURN(x)                                                              \
  do {                                                                         \
    cout << x << '\n';                                                         \
    return 0;                                                                  \
  } while (0)
#define FIRSTWIN                                                               \
  do {                                                                         \
    cout << "Monocarp\n";                                                      \
    return 0;                                                                  \
  } while (0)
#define SECONDWIN                                                              \
  do {                                                                         \
    cout << "Bicarp\n";                                                        \
    return 0;                                                                  \
  } while (0)

const int MAXN = 1e5 + 4;
const int MOD = 1e9 + 7;
const int INF = 1e9 + 1;

int N, M;
vector<pii> V[MAXN];
vector<LL> A[MAXN];

int main() {
#ifdef OJ
  freopen("input.txt", "rt", stdin);
  // freopen("output.txt", "wt", stdout);
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> N >> M;
  fori(i, M) {
    int l, r, c;
    cin >> l >> r >> c;
    l--;
    r--;
    V[l].eb(c, r);
  }

  multiset<LL> S;
  S.insert(0);
  A[1].pb(0);
  fori(i, N) {
    for (LL a : A[i]) {
      assert(S.find(a) != S.end());
      S.erase(a);
    }
    if (!S.size())
      RETURN(-1);

    LL mn = *S.begin();
    if (i == N - 1) {
      cout << mn << endl;
      return 0;
    }

    for (auto v : V[i]) {
      LL tmp = mn + v.fi;
      S.insert(tmp);
      A[v.se + 1].pb(tmp);
    }
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
typedef pair<int, int> pii;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define debug(x)                                                               \
  do {                                                                         \
    cerr << #x << ": " << x << "\n";                                           \
  } while (0)
#define debugv(x)                                                              \
  do {                                                                         \
    cerr << #x << ": ";                                                        \
    for (auto &e : (x))                                                        \
      cerr << e << " ";                                                        \
    cerr << "\n";                                                              \
  } while (0)
#define fori(i, n) for (int i = 0; i < (int)(n); ++i)
#define forn(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n); i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define RETURN(x)                                                              \
  do {                                                                         \
    cout << x << '\n';                                                         \
    return 0;                                                                  \
  } while (0)
#define FIRSTWIN                                                               \
  do {                                                                         \
    cout << "Monocarp\n";                                                      \
    return 0;                                                                  \
  } while (0)
#define SECONDWIN                                                              \
  do {                                                                         \
    cout << "Bicarp\n";                                                        \
    return 0;                                                                  \
  } while (0)

const int MAXN = 1e5 + 4;
const int MOD = 1e9 + 7;
const int INF = 1e9 + 1;

int N, M;
vector<pii> V[MAXN];
vector<LL> A[MAXN];

int main() {
#ifdef OJ
  freopen("input.txt", "rt", stdin);
  // freopen("output.txt", "wt", stdout);
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> N >> M;
  fori(i, M) {
    int l, r, c;
    cin >> l >> r >> c;
    l--;
    r--;
    V[l].eb(c, r);
  }

  multiset<LL> S;
  S.insert(0);
  A[1].pb(0);
  fori(i, N) {
    for (LL a : A[i])
      S.erase(S.find(a));
    if (!S.size())
      RETURN(-1);

    LL mn = *S.begin();
    if (i == N - 1) {
      cout << mn << endl;
      return 0;
    }

    for (auto v : V[i]) {
      LL tmp = mn + v.fi;
      S.insert(tmp);
      A[v.se + 1].pb(tmp);
    }
  }

  return 0;
}