prithivMLmods/Coder-Stat huggingface dataset:
Problem p02868 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define FOR(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
#define FORN(i, m, n) for (int(i) = (m); (i) <= (n); (i)++)
#define FORR(i, m, n) for (int(i) = (m); (i) >= (n); (i)--)
#define rep(i, n) FOR(i, 0, n)
#define repn(i, n) FORN(i, 1, n)
#define repr(i, n) FORR(i, n, 0)
#define repnr(i, n) FORR(i, n, 1)
#define co(n) cout << (n) << endl
#define cosp(n) cout << (n) << ' '
#define setp(n) cout << fixed << setprecision(n);
#define all(s) (s).begin(), (s).end()
#define pb push_back
#define mp make_pair
#define fs first
#define sc second
typedef long long ll;
typedef pair<ll, ll> P;

struct Edge {
  int to;
  int cost;
};

const ll INF = 1e9 + 1;
const ll LINF = 1e18 + 1;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const double PI = acos(-1);
const double EPS = 1e-9;
const int MAX_N = 1e5;
vector<vector<Edge>> tree;
ll dist[MAX_N] = {};

int main(void) {
  int n, m;
  cin >> n >> m;
  tree.resize(n);
  fill_n(dist, n, LINF);
  repn(i, n - 1) {
    Edge e = {i - 1, 0};
    tree[i].pb(e);
  }

  rep(i, m) {
    int l, r, c;
    cin >> l >> r >> c;
    l--;
    r--;
    Edge e = {r, c};
    tree[l].pb(e);
  }

  priority_queue<P> q;
  q.push(mp(0, 0));
  while (!q.empty()) {
    P p = q.top();
    q.pop();
    if (p.fs >= dist[p.sc])
      continue;
    dist[p.sc] = p.fs;
    rep(i, tree[p.sc].size()) {
      Edge e = tree[p.sc][i];
      q.push(mp(p.fs + e.cost, e.to));
    }
  }

  if (dist[n - 1] == LINF)
    co(-1);
  else
    co(dist[n - 1]);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define FOR(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
#define FORN(i, m, n) for (int(i) = (m); (i) <= (n); (i)++)
#define FORR(i, m, n) for (int(i) = (m); (i) >= (n); (i)--)
#define rep(i, n) FOR(i, 0, n)
#define repn(i, n) FORN(i, 1, n)
#define repr(i, n) FORR(i, n, 0)
#define repnr(i, n) FORR(i, n, 1)
#define co(n) cout << (n) << endl
#define cosp(n) cout << (n) << ' '
#define setp(n) cout << fixed << setprecision(n);
#define all(s) (s).begin(), (s).end()
#define pb push_back
#define mp make_pair
#define fs first
#define sc second
typedef long long ll;
typedef pair<ll, ll> P;

struct Edge {
  int to;
  int cost;
};

const ll INF = 1e9 + 1;
const ll LINF = 1e18 + 1;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const double PI = acos(-1);
const double EPS = 1e-9;
const int MAX_N = 1e5;
vector<vector<Edge>> tree;
ll dist[MAX_N] = {};

int main(void) {
  int n, m;
  cin >> n >> m;
  tree.resize(n);
  fill_n(dist, n, LINF);
  repn(i, n - 1) {
    Edge e = {i - 1, 0};
    tree[i].pb(e);
  }

  rep(i, m) {
    int l, r, c;
    cin >> l >> r >> c;
    l--;
    r--;
    Edge e = {r, c};
    tree[l].pb(e);
  }

  priority_queue<P, vector<P>, greater<P>> q;
  q.push(mp(0, 0));
  while (!q.empty()) {
    P p = q.top();
    q.pop();
    if (p.fs >= dist[p.sc])
      continue;
    dist[p.sc] = p.fs;
    rep(i, tree[p.sc].size()) {
      Edge e = tree[p.sc][i];
      q.push(mp(p.fs + e.cost, e.to));
    }
  }

  if (dist[n - 1] == LINF)
    co(-1);
  else
    co(dist[n - 1]);

  return 0;
}
Problem p02868 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define popcnt(x) __builtin_popcount(x)
#define inf 0x3f3f3f3f
#define watch(x) cout << (#x) << " is " << (x) << endl
#define rand() (rand() << 15 | rand())
using namespace std;
typedef long long ll;
#define EPS 1e-9
#define PI acos(-1.0)
const int N = 1e5 + 4;

vector<pair<int, int>> add[N];
vector<long long> er[N];
long long dp[N];
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int l, r, c;
    cin >> l >> r >> c;
    add[l].push_back({r, c});
  }

  multiset<long long> ms;
  for (int i = 1; i <= n; ++i) {
    for (int c : er[i])
      ms.erase(ms.find(c));
    if (i > 1)
      dp[i] = (ms.empty() ? -LLONG_MAX / 2 : *ms.begin());
    else
      dp[i] = 0;
    for (auto p : add[i]) {
      ms.insert(dp[i] + p.second);
      er[p.first + 1].push_back(dp[i] + p.second);
    }
  }

  cout << (dp[n] < 0 ? -1 : dp[n]) << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define popcnt(x) __builtin_popcount(x)
#define inf 0x3f3f3f3f
#define watch(x) cout << (#x) << " is " << (x) << endl
#define rand() (rand() << 15 | rand())
using namespace std;
typedef long long ll;
#define EPS 1e-9
#define PI acos(-1.0)
const int N = 1e5 + 4;

vector<pair<int, int>> add[N];
vector<long long> er[N];
long long dp[N];
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int l, r, c;
    cin >> l >> r >> c;
    add[l].push_back({r, c});
  }

  multiset<long long> ms;
  for (int i = 1; i <= n; ++i) {
    for (long long c : er[i])
      ms.erase(ms.find(c));
    if (i > 1)
      dp[i] = (ms.empty() ? -LLONG_MAX / 2 : *ms.begin());
    else
      dp[i] = 0;
    for (auto p : add[i]) {
      ms.insert(dp[i] + p.second);
      er[p.first + 1].push_back(dp[i] + p.second);
    }
  }

  cout << (dp[n] < 0 ? -1 : dp[n]) << "\n";
}
Problem p02868 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const ll N = 1e5 + 5, mod = 1e15;

ll dist[N];
vector<pii> adj[N];
ll n, m;

void dij() {
  for (int i = 0; i < n; i++)
    dist[i] = mod;
  set<pii> s;
  dist[0] = 0;
  s.insert({0, 0});
  while (!s.empty()) {
    int v = (*s.begin()).second;
    s.erase(s.begin());
    for (auto e : adj[v]) {
      int w = e.second, u = e.first;
      if (dist[u] > dist[v] + w) {
        s.erase({dist[u], u});
        dist[u] = dist[v] + w;
        s.insert({dist[u], u});
      }
    }
  }
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  vector<pair<pii, int>> edge;
  for (int i = 0; i < n - 1; i++) {
    adj[i + 1].push_back({i, 0});
  }
  for (int i = 0; i < m; i++) {
    int v, u, w;
    cin >> v >> u >> w;
    v--, u--;
    adj[v].push_back({u, w});
    adj[u].push_back({v, w});
  }
  dij();
  if (dist[n - 1] == mod)
    cout << -1 << "\n";
  else
    cout << dist[n - 1] << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<ll, ll> pii;

const ll N = 1e5 + 5, mod = 1e15;

ll dist[N];
vector<pii> adj[N];
ll n, m;

void dij() {
  for (int i = 0; i < n; i++)
    dist[i] = mod;
  set<pii> s;
  dist[0] = 0;
  s.insert({0, 0});
  while (!s.empty()) {
    int v = (*s.begin()).second;
    s.erase(s.begin());
    for (auto e : adj[v]) {
      int w = e.second, u = e.first;
      if (dist[u] > dist[v] + w) {
        s.erase({dist[u], u});
        dist[u] = dist[v] + w;
        s.insert({dist[u], u});
      }
    }
  }
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  vector<pair<pii, int>> edge;
  for (int i = 0; i < n - 1; i++) {
    adj[i + 1].push_back({i, 0});
  }
  for (int i = 0; i < m; i++) {
    int v, u, w;
    cin >> v >> u >> w;
    v--, u--;
    adj[v].push_back({u, w});
    adj[u].push_back({v, w});
  }
  dij();
  if (dist[n - 1] == mod)
    cout << -1 << "\n";
  else
    cout << dist[n - 1] << "\n";
}
Problem p02868 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string.h>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll INF = 2e18;
const ll MOD = 1e9 + 7;

ll N, M;
struct Edge {
  ll to, cost;
};
vector<Edge> edgeList[100010];
ll d[100010];
int main() {
  cin >> N >> M;
  for (ll i = 0; i < M; i++) {
    ll l, r, c;
    cin >> l >> r >> c;
    l--;
    r--;
    edgeList[l].push_back(Edge{r, c});
  }
  for (ll i = 1; i < N; i++) {
    edgeList[i].push_back(Edge{i - 1, 0});
    d[i] = INF;
  }
  d[0] = 0;
  priority_queue<P> pq;
  pq.push(P(0, 0));
  while (!pq.empty()) {
    ll now = pq.top().second;
    ll nowC = pq.top().first;
    pq.pop();
    if (nowC > d[now])
      continue;
    for (auto e : edgeList[now]) {
      ll newC = nowC + e.cost;
      if (d[e.to] > newC) {
        d[e.to] = newC;
        pq.push(P(newC, e.to));
      }
    }
  }
  cout << ((d[N - 1] == INF) ? (-1) : d[N - 1]);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string.h>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll INF = 2e18;
const ll MOD = 1e9 + 7;

ll N, M;
struct Edge {
  ll to, cost;
};
vector<Edge> edgeList[100010];
ll d[100010];
int main() {
  cin >> N >> M;
  for (ll i = 0; i < M; i++) {
    ll l, r, c;
    cin >> l >> r >> c;
    l--;
    r--;
    edgeList[l].push_back(Edge{r, c});
  }
  for (ll i = 1; i < N; i++) {
    edgeList[i].push_back(Edge{i - 1, 0});
    d[i] = INF;
  }
  d[0] = 0;
  priority_queue<P, vector<P>, greater<P>> pq;
  pq.push(P(0, 0));
  while (!pq.empty()) {
    ll now = pq.top().second;
    ll nowC = pq.top().first;
    pq.pop();
    if (nowC > d[now])
      continue;
    for (auto e : edgeList[now]) {
      ll newC = nowC + e.cost;
      if (d[e.to] > newC) {
        d[e.to] = newC;
        pq.push(P(newC, e.to));
      }
    }
  }
  cout << ((d[N - 1] == INF) ? (-1) : d[N - 1]);
  return 0;
}
Problem p02868 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define RREP(i, m, n) for (int i = (int)(m); i >= (int)(n); i--)
#define rrep(i, n) RREP(i, n - 1, 0)
#define REPL(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++)
#define repl(i, n) REPL(i, 0, n)
#define all(v) v.begin(), v.end()
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
struct edge {
  int to;
  ll cost;
  edge(int t, ll c) : to(t), cost(c){};
};
using P = pair<int, ll>;
int N;
vector<vector<edge>> G;
vector<ll> d;
vector<int> par;
void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  d = vector<ll>(N, longinf);
  par = vector<int>(N, -1);
  d[s] = 0;
  que.push(P(0, s));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    for (int i = 0; i < (int)G[v].size(); ++i) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
        par[e.to] = v;
      }
    }
  }
}
int main() {
  int M;
  cin >> N >> M;
  G = vector<vector<edge>>(N);
  rep(i, N) if (i) G[i].push_back(edge(i - 1, 0));
  rep(i, M) {
    int l, r;
    ll c;
    cin >> l >> r >> c;
    --l;
    --r;
    G[l].push_back(edge(r, c));
  }
  dijkstra(0);
  cout << (d[N - 1] == longinf ? -1 : d[N - 1]) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define RREP(i, m, n) for (int i = (int)(m); i >= (int)(n); i--)
#define rrep(i, n) RREP(i, n - 1, 0)
#define REPL(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++)
#define repl(i, n) REPL(i, 0, n)
#define all(v) v.begin(), v.end()
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
struct edge {
  int to;
  ll cost;
  edge(int t, ll c) : to(t), cost(c){};
};
using P = pair<ll, int>;
int N;
vector<vector<edge>> G;
vector<ll> d;
vector<int> par;
void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  d = vector<ll>(N, longinf);
  par = vector<int>(N, -1);
  d[s] = 0;
  que.push(P(0, s));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    for (int i = 0; i < (int)G[v].size(); ++i) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
        par[e.to] = v;
      }
    }
  }
}
int main() {
  int M;
  cin >> N >> M;
  G = vector<vector<edge>>(N);
  rep(i, N) if (i) G[i].push_back(edge(i - 1, 0));
  rep(i, M) {
    int l, r;
    ll c;
    cin >> l >> r >> c;
    --l;
    --r;
    G[l].push_back(edge(r, c));
  }
  dijkstra(0);
  cout << (d[N - 1] == longinf ? -1 : d[N - 1]) << endl;
  return 0;
}
Problem p02868 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define RREP(i, m, n) for (int i = (int)(m); i >= (int)(n); i--)
#define rrep(i, n) RREP(i, n - 1, 0)
#define all(v) v.begin(), v.end()
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
const ld eps = 1e-10;

struct edge {
  int to;
  ll cost;
  edge(int t, ll c) : to(t), cost(c){};
};

using P = pair<ll, int>;

vector<ll> d;
vector<int> par;
void dijkstra(int s, int N, vector<vector<edge>> &G) {
  priority_queue<P, vector<P>, greater<P>> que;
  d = vector<ll>(N, longinf);
  par = vector<int>(N, -1);
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;

    for (int i = 0; i < (int)G[v].size(); i++) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
        par[e.to] = v;
      }
    }
  }
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<vector<edge>> G(n);
  rep(i, n) REP(j, i + 1, n) { G[j].push_back({i, 0}); }
  vector<int> L(m), R(m);
  vector<ll> C(m);
  rep(i, m) {
    cin >> L[i] >> R[i] >> C[i];
    G[--L[i]].push_back({--R[i], C[i]});
  }
  dijkstra(0, n, G);
  cout << (d[n - 1] == longinf ? -1 : d[n - 1]) << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define RREP(i, m, n) for (int i = (int)(m); i >= (int)(n); i--)
#define rrep(i, n) RREP(i, n - 1, 0)
#define all(v) v.begin(), v.end()
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
const ld eps = 1e-10;

struct edge {
  int to;
  ll cost;
  edge(int t, ll c) : to(t), cost(c){};
};

using P = pair<ll, int>;

vector<ll> d;
vector<int> par;
void dijkstra(int s, int N, vector<vector<edge>> &G) {
  priority_queue<P, vector<P>, greater<P>> que;
  d = vector<ll>(N, longinf);
  par = vector<int>(N, -1);
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;

    for (int i = 0; i < (int)G[v].size(); i++) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
        par[e.to] = v;
      }
    }
  }
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<vector<edge>> G(n);
  rep(i, n - 1) G[i + 1].push_back({i, 0});
  vector<int> L(m), R(m);
  vector<ll> C(m);
  rep(i, m) {
    cin >> L[i] >> R[i] >> C[i];
    G[--L[i]].push_back({--R[i], C[i]});
  }
  dijkstra(0, n, G);
  cout << (d[n - 1] == longinf ? -1 : d[n - 1]) << "\n";
  return 0;
}
Problem p02868 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define debug(x) cerr << #x << " = " << x << endl
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;

const int maxN = 1e5 + 5;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;

ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }
ll sq(ll x) { return (x * x) % MOD; }
ll modP(ll a, ll b) {
  return (!b ? 1 : (sq(modP(a, b / 2)) * (b % 2 ? a : 1)) % MOD);
}

vector<pii> G[maxN];
ll cnt[maxN], dis[maxN];
set<pii> S;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  memset(dis, 63, sizeof dis);
  int n, m;
  cin >> n >> m;
  while (m--) {
    int l, r, c;
    cin >> l >> r >> c;
    G[l].push_back({r, c});
    cnt[r]--;
    cnt[l--]++;
  }
  for (int i = 0; i < n; i++) {
    cnt[i + 1] += cnt[i];
    if (cnt[i + 1] > 0)
      G[i + 1].push_back({i, 0});
  }
  dis[1] = 0;
  S.insert({0, 1});
  while (!S.empty()) {
    int v = (*S.begin()).second;
    S.erase(S.begin());
    for (auto pp : G[v]) {
      int u = pp.first;
      int w = pp.second;
      if (dis[u] > dis[v] + w) {
        dis[u] = dis[v] + w;
        S.insert({dis[u], u});
      }
    }
  }
  cout << (dis[n] > INF ? -1 : dis[n]) << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define debug(x) cerr << #x << " = " << x << endl
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;

const int maxN = 1e5 + 5;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;

ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }
ll sq(ll x) { return (x * x) % MOD; }
ll modP(ll a, ll b) {
  return (!b ? 1 : (sq(modP(a, b / 2)) * (b % 2 ? a : 1)) % MOD);
}

vector<pii> G[maxN];
ll cnt[maxN], dis[maxN];
set<pll> S;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  memset(dis, 63, sizeof dis);
  int n, m;
  cin >> n >> m;
  while (m--) {
    int l, r, c;
    cin >> l >> r >> c;
    G[l].push_back({r, c});
    cnt[r]--;
    cnt[l--]++;
  }
  for (int i = 0; i < n; i++) {
    cnt[i + 1] += cnt[i];
    if (cnt[i + 1] > 0)
      G[i + 1].push_back({i, 0});
  }
  dis[1] = 0;
  S.insert({0, 1});
  while (!S.empty()) {
    int v = (*S.begin()).second;
    S.erase(S.begin());
    for (auto pp : G[v]) {
      int u = pp.first;
      int w = pp.second;
      if (dis[u] > dis[v] + w) {
        dis[u] = dis[v] + w;
        S.insert({dis[u], u});
      }
    }
  }
  cout << (dis[n] > INF ? -1 : dis[n]) << endl;
  return 0;
}
Problem p02868 (C++)

Buggy submission (status: Runtime Error):
/// awoooooo

#include <bits/stdc++.h>
#define sz(x) (int)((x).size())

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

const int N = (int)2e5 + 5;
const int inf = (int)1e9 + 7;
const ll INF = (ll)1e18;

int n, m;
int l[N], r[N], c[N];
ll de[N];
vector<int> event[N];

int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; ++i) {
    cin >> l[i] >> r[i] >> c[i];
    event[l[i]].push_back(i);
    event[r[i] + 1].push_back(-i);
  }
  set<ll> cur;
  for (int j = 1; j <= n; ++j) {
    for (int i : event[j]) {
      if (i < 0) {
        if (de[-i] != INF)
          cur.erase(cur.find(de[-i] + c[-i]));
      }
    }
    for (int i : event[j]) {
      if (i > 0) {
        if (cur.empty() || l[i] == 1) {
          if (l[i] == 1) {
            de[i] = 0;
          } else {
            de[i] = INF;
          }
        } else {
          de[i] = *cur.begin();
        }
        if (de[i] != INF) {
          cur.insert(de[i] + c[i]);
        }
      }
    }
  }
  ll ans = INF;
  for (int i = 1; i <= m; ++i) {
    if (r[i] == n) {
      ans = min(ans, de[i] + c[i]);
    }
  }
  if (ans == INF)
    ans = -1;
  cout << ans;
  return 0;
}


Fixed submission:
/// awoooooo

#include <bits/stdc++.h>
#define sz(x) (int)((x).size())

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

const int N = (int)2e5 + 5;
const int inf = (int)1e9 + 7;
const ll INF = (ll)1e18;

int n, m;
int l[N], r[N], c[N];
ll de[N];
vector<int> event[N];

int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; ++i) {
    cin >> l[i] >> r[i] >> c[i];
    event[l[i]].push_back(i);
    event[r[i] + 1].push_back(-i);
  }
  multiset<ll> cur;
  for (int j = 1; j <= n; ++j) {
    for (int i : event[j]) {
      if (i < 0) {
        if (de[-i] != INF)
          cur.erase(cur.find(de[-i] + c[-i]));
      }
    }
    for (int i : event[j]) {
      if (i > 0) {
        if (cur.empty() || l[i] == 1) {
          if (l[i] == 1) {
            de[i] = 0;
          } else {
            de[i] = INF;
          }
        } else {
          de[i] = *cur.begin();
        }
        if (de[i] != INF) {
          cur.insert(de[i] + c[i]);
        }
      }
    }
  }
  ll ans = INF;
  for (int i = 1; i <= m; ++i) {
    if (r[i] == n) {
      ans = min(ans, de[i] + c[i]);
    }
  }
  if (ans == INF)
    ans = -1;
  cout << ans;
  return 0;
}
Problem p02868 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

const ll INF = 1e18;
int n, m;
vector<P> edge[100010];

vector<ll> dijkstra(int x) {
  vector<ll> dist(n + 1, INF);
  priority_queue<P, vector<P>, greater<P>> q;
  dist[x] = 0;
  q.push(P(0, x));
  while (!q.empty()) {
    ll d = q.top().first;
    ll p = q.top().second;
    q.pop();
    if (dist[p] < d)
      continue;
    for (P next : edge[p]) {
      ll to = next.first;
      ll co = next.second;
      if (dist[p] + co >= dist[to])
        continue;
      dist[to] = dist[p] + co;
      q.push(P(dist[to], to));
    }
  }
  return dist;
}

int main() {
  cin >> n >> m;
  rep(i, m) {
    int l, r, c;
    cin >> l >> r >> c;
    edge[l].push_back(P(r, c));
  }
  rep(i, n - 1) { edge[i + 1].push_back(P(i, 0)); }
  vector<ll> dist = dijkstra(1);
  if (dist[n] != INF)
    cout << dist[n] << endl;
  else
    cout << -1 << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

const ll INF = 1e18;
int n, m;
vector<P> edge[100010];

vector<ll> dijkstra(int x) {
  vector<ll> dist(n + 1, INF);
  priority_queue<P, vector<P>, greater<P>> q;
  dist[x] = 0;
  q.push(P(0, x));
  while (!q.empty()) {
    ll d = q.top().first;
    ll p = q.top().second;
    q.pop();
    if (dist[p] < d)
      continue;
    for (P next : edge[p]) {
      ll to = next.first;
      ll co = next.second;
      if (dist[p] + co >= dist[to])
        continue;
      dist[to] = dist[p] + co;
      q.push(P(dist[to], to));
    }
  }
  return dist;
}

int main() {
  cin >> n >> m;
  rep(i, m) {
    int l, r, c;
    cin >> l >> r >> c;
    edge[l].push_back(P(r, c));
  }
  rep(i, n - 1) { edge[i + 1].push_back(P(i, 0)); }
  vector<ll> dist = dijkstra(1);
  if (dist[n] != INF)
    cout << dist[n] << endl;
  else
    cout << -1 << endl;
}
Problem p02868 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; ++i)
const long long INF = 1e18;

struct Edge {
  int from, to;
  long long cost;
  Edge(int _to, long long _cost) : from(-1), to(_to), cost(_cost) {}
  Edge(int _from, int _to, long long _cost)
      : from(_from), to(_to), cost(_cost) {}
};

template <class T>
vector<T> Dijkstra(const vector<vector<Edge>> &G, int s, T def) {
  using P = pair<T, int>;
  priority_queue<P, vector<P>, greater<P>> pq;
  vector<T> dist((int)G.size(), def);
  pq.push({0, s});
  dist[s] = 0;
  while (!pq.empty()) {
    int cur = pq.top().second;
    pq.pop();
    for (auto &e : G[cur]) {
      if (dist[e.to] > dist[cur] + e.cost) {
        dist[e.to] = dist[cur] + e.cost;
        pq.push({dist[e.to], e.to});
      }
    }
  }
  return dist;
}

void solve() {
  int N, M;
  cin >> N >> M;
  vector<vector<Edge>> G(N);
  for (int i = 0; i < M; ++i) {
    int a, b;
    long long c;
    cin >> a >> b >> c;
    a--, b--;
    G[a].emplace_back(b, c);
    G[b].emplace_back(a, c);
    if (i)
      G[i].emplace_back(i - 1, 0LL);
  }
  auto dist = Dijkstra(G, 0, INF);
  if (dist[N - 1] == INF)
    cout << -1 << '\n';
  else
    cout << dist[N - 1] << '\n';
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; ++i)
const long long INF = 1e18;

struct Edge {
  int from, to;
  long long cost;
  Edge(int _to, long long _cost) : from(-1), to(_to), cost(_cost) {}
  Edge(int _from, int _to, long long _cost)
      : from(_from), to(_to), cost(_cost) {}
};

template <class T>
vector<T> Dijkstra(const vector<vector<Edge>> &G, int s, T def) {
  using P = pair<T, int>;
  priority_queue<P, vector<P>, greater<P>> pq;
  vector<T> dist((int)G.size(), def);
  pq.push({0, s});
  dist[s] = 0;
  while (!pq.empty()) {
    int cur = pq.top().second;
    pq.pop();
    for (auto &e : G[cur]) {
      if (dist[e.to] > dist[cur] + e.cost) {
        dist[e.to] = dist[cur] + e.cost;
        pq.push({dist[e.to], e.to});
      }
    }
  }
  return dist;
}

void solve() {
  int N, M;
  cin >> N >> M;
  vector<vector<Edge>> G(N);
  for (int i = 0; i < M; ++i) {
    int a, b;
    long long c;
    cin >> a >> b >> c;
    a--, b--;
    G[a].emplace_back(b, c);
    G[b].emplace_back(a, c);
  }
  rep(i, N) {
    if (i)
      G[i].emplace_back(i - 1, 0LL);
  }
  auto dist = Dijkstra(G, 0, INF);
  if (dist[N - 1] == INF)
    cout << -1 << '\n';
  else
    cout << dist[N - 1] << '\n';
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
  return 0;
}
Problem p02868 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)

int n, m;
vector<P> to[100005];
vector<ll> dist(100005, LLONG_MAX);
ll solve() {
  priority_queue<P, vector<P>, greater<P>> pq;
  dist[0] = 0;
  pq.push(P(0, 0));
  while (pq.size() > 0) {
    P cur = pq.top();
    pq.pop();
    ll d = cur.first;
    ll u = cur.second;
    if (d > dist[u])
      continue;

    for (P nx : to[u]) {
      ll duv = nx.first;
      ll v = nx.second;
      if (dist[v] > d + duv) {
        dist[v] = d + duv;
        pq.push(P(dist[v], v));
      }
    }
  }
  return dist[n - 1] == LLONG_MAX ? -1 : dist[n - 1];
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> n >> m;
  rep(i, m) {
    ll l, r, c;
    cin >> l >> r >> c;
    --l, --r;
    to[l].push_back(make_pair(c, r));
    for (int j = l; j < r; j++) {
      to[j + 1].push_back(make_pair(0, j));
    }
  }
  cout << solve() << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)

int n, m;
vector<P> to[100005];
vector<ll> dist(100005, LLONG_MAX);
ll solve() {
  priority_queue<P, vector<P>, greater<P>> pq;
  dist[0] = 0;
  pq.push(P(0, 0));
  while (pq.size() > 0) {
    P cur = pq.top();
    pq.pop();
    ll d = cur.first;
    ll u = cur.second;
    if (d > dist[u])
      continue;

    for (P nx : to[u]) {
      ll duv = nx.first;
      ll v = nx.second;
      if (dist[v] > d + duv) {
        dist[v] = d + duv;
        pq.push(P(dist[v], v));
      }
    }
  }
  return dist[n - 1] == LLONG_MAX ? -1 : dist[n - 1];
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> n >> m;
  rep(i, m) {
    ll l, r, c;
    cin >> l >> r >> c;
    --l, --r;
    to[l].push_back(make_pair(c, r));
  }
  for (int j = 0; j < n; j++) {
    to[j + 1].push_back(make_pair(0, j));
  }
  cout << solve() << endl;
  return 0;
}
Problem p02868 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;

int main() {
  priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>>
      q;
  int n, m, l, r;
  vector<pair<pair<int, int>, ll>> e;
  ll c, dis[100001];
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    scanf("%d%d%lld", &l, &r, &c);
    e.push_back({{l, r}, c});
  }
  sort(e.begin(), e.end());
  for (int i = 0; i <= n; i++) {
    dis[i] = 1000000000000000;
  }
  dis[1] = 0;
  int p = 0;
  l = 0;
  q.push({0LL, -1});
  while (!q.empty() && l < n && p < m) {
    while (!q.empty() && -q.top().second <= l) {
      q.pop();
    }
    l = -q.top().second;
    q.pop();
    while (e[p].first.first <= l && p < m) {
      if (dis[e[p].first.second] > dis[l] + e[p].second) {
        dis[e[p].first.second] = dis[l] + e[p].second;
        // printf("%lld %lld %lld\n",dis[e[p].first.second],dis[l],e[p].second);
        q.push({dis[e[p].first.second], -e[p].first.second});
      }
      p++;
    }
  }
  if (dis[n] == 1000000000000000) {
    printf("-1\n");
  } else {
    printf("%lld\n", dis[n]);
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;

int main() {
  priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>>
      q;
  int n, m, l, r;
  vector<pair<pair<int, int>, ll>> e;
  ll c, dis[100001];
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    scanf("%d%d%lld", &l, &r, &c);
    e.push_back({{l, r}, c});
  }
  sort(e.begin(), e.end());
  for (int i = 0; i <= n; i++) {
    dis[i] = 1000000000000000;
  }
  dis[1] = 0;
  int p = 0;
  l = 0;
  q.push({0LL, -1});
  while (!q.empty() && l < n && p < m) {
    while (!q.empty() && -q.top().second <= l) {
      q.pop();
    }
    if (q.empty()) {
      break;
    }
    l = -q.top().second;
    q.pop();
    while (e[p].first.first <= l && p < m) {
      if (dis[e[p].first.second] > dis[l] + e[p].second) {
        dis[e[p].first.second] = dis[l] + e[p].second;
        // printf("%lld %lld %lld\n",dis[e[p].first.second],dis[l],e[p].second);
        q.push({dis[e[p].first.second], -e[p].first.second});
      }
      p++;
    }
  }
  if (dis[n] == 1000000000000000) {
    printf("-1\n");
  } else {
    printf("%lld\n", dis[n]);
  }
}
Problem p02868 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n; i >= 0; --i)
const int inf = 1e9 + 7;
const ll mod = 1e9 + 7;
const ll big = 1e18;
const double PI = 2 * asin(1);

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<pair<int, ll>>> edge(N);
  int L, R;
  ll C;
  for (int i = 0; i < M; ++i) {
    cin >> L >> R >> C;
    L--;
    R--;
    edge[L].push_back(mp(R, C));
  }
  for (int i = 0; i < N - 1; ++i) {
    edge[i + 1].push_back(mp(i, 0));
  }
  priority_queue<pair<ll, int>> que;
  que.push(mp(0, 0));
  pair<ll, int> state;
  ll cost;
  int node;
  map<int, ll> amap;
  for (int i = 0; i < N; ++i) {
    amap[i] = big;
  }
  while (!que.empty()) {
    state = que.top();
    que.pop();
    node = state.se;
    cost = state.fi;
    if (amap[node] <= cost)
      continue;
    amap[node] = cost;
    for (int i = 0; i < edge[node].size(); ++i) {
      if (amap[edge[node][i].fi] <= cost + edge[node][i].se)
        continue;
      que.push(mp(cost + edge[node][i].se, edge[node][i].fi));
    }
  }
  if (amap[N - 1] == big)
    cout << -1 << endl;
  else
    cout << amap[N - 1] << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n; i >= 0; --i)
const int inf = 1e9 + 7;
const ll mod = 1e9 + 7;
const ll big = 1e18;
const double PI = 2 * asin(1);

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<pair<int, ll>>> edge(N);
  int L, R;
  ll C;
  for (int i = 0; i < M; ++i) {
    cin >> L >> R >> C;
    L--;
    R--;
    edge[L].push_back(mp(R, C));
  }
  for (int i = 0; i < N - 1; ++i) {
    edge[i + 1].push_back(mp(i, 0));
  }
  priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>>
      que;
  que.push(mp(0, 0));
  pair<ll, int> state;
  ll cost;
  int node;
  map<int, ll> amap;
  for (int i = 0; i < N; ++i) {
    amap[i] = big;
  }
  while (!que.empty()) {
    state = que.top();
    que.pop();
    node = state.se;
    cost = state.fi;
    if (amap[node] <= cost)
      continue;
    amap[node] = cost;
    for (int i = 0; i < edge[node].size(); ++i) {
      if (amap[edge[node][i].fi] <= cost + edge[node][i].se)
        continue;
      que.push(mp(cost + edge[node][i].se, edge[node][i].fi));
    }
  }
  if (amap[N - 1] == big)
    cout << -1 << endl;
  else
    cout << amap[N - 1] << endl;
}
Problem p02868 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n; i >= 0; --i)
const int inf = 1e9 + 7;
const ll mod = 1e9 + 7;
const ll mod1 = 998244353;
const ll big = 1e18;
const double PI = 2 * asin(1);

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<pair<int, int>>> edge(N);
  for (int i = 1; i < N; ++i) {
    edge[i].push_back(mp(i - 1, 0));
  }
  int L, R, C;
  for (int i = 0; i < M; ++i) {
    cin >> L >> R >> C;
    L--;
    R--;
    edge[L].push_back(mp(R, C));
  }
  priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>>
      que;
  pair<ll, int> state;
  que.push(mp(0, 0));
  map<int, ll> amap;
  for (int i = 0; i < N; ++i)
    amap[i] = -1;
  int node, cost;
  while (!que.empty()) {
    state = que.top();
    que.pop();
    node = state.se;
    cost = state.fi;
    if (amap[node] > -1)
      continue;
    amap[node] = cost;
    for (int i = 0; i < edge[node].size(); ++i) {
      if (amap[edge[node][i].fi] > -1 &&
          amap[edge[node][i].fi] <= cost + edge[node][i].se)
        continue;
      que.push(mp(cost + edge[node][i].se, edge[node][i].fi));
    }
  }
  cout << amap[N - 1] << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n; i >= 0; --i)
const int inf = 1e9 + 7;
const ll mod = 1e9 + 7;
const ll mod1 = 998244353;
const ll big = 1e18;
const double PI = 2 * asin(1);

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<pair<int, int>>> edge(N);
  for (int i = 1; i < N; ++i) {
    edge[i].push_back(mp(i - 1, 0));
  }
  int L, R, C;
  for (int i = 0; i < M; ++i) {
    cin >> L >> R >> C;
    L--;
    R--;
    edge[L].push_back(mp(R, C));
  }
  priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>>
      que;
  pair<ll, int> state;
  que.push(mp(0, 0));
  map<int, ll> amap;
  for (int i = 0; i < N; ++i)
    amap[i] = -1;
  ll node, cost;
  while (!que.empty()) {
    state = que.top();
    que.pop();
    node = state.se;
    cost = state.fi;
    if (amap[node] > -1)
      continue;
    amap[node] = cost;
    for (int i = 0; i < edge[node].size(); ++i) {
      if (amap[edge[node][i].fi] > -1 &&
          amap[edge[node][i].fi] <= cost + edge[node][i].se)
        continue;
      que.push(mp(cost + edge[node][i].se, edge[node][i].fi));
    }
  }
  cout << amap[N - 1] << endl;
}
Problem p02868 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define INF 1e18
#define int long long
#define Rep(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) Rep(i, 0, n)
#define all(a) (a).begin(), (a).end()
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> PP;
const int mod = 1000000007;

struct edge {
  int to, cost;
};
typedef pair<int, int> P; // firstは最短距離,secondは頂点の番号
#define V 100
vector<edge> G[V];
int d[V];

void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + V, INF);
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    for (int i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, m;
  cin >> n >> m;
  rep(i, m) {
    int l, r, c;
    cin >> l >> r >> c;
    l--, r--;
    G[l].push_back({r, c});
  }
  Rep(i, 1, n) { G[i].push_back({i - 1, 0}); }
  dijkstra(0);
  int ans = d[n - 1];
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define INF 1e18
#define int long long
#define Rep(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) Rep(i, 0, n)
#define all(a) (a).begin(), (a).end()
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> PP;
const int mod = 1000000007;

struct edge {
  int to, cost;
};
typedef pair<int, int> P; // firstは最短距離,secondは頂点の番号
#define V 100010
vector<edge> G[V];
int d[V];

void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + V, INF);
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    for (int i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, m;
  cin >> n >> m;
  rep(i, m) {
    int l, r, c;
    cin >> l >> r >> c;
    l--, r--;
    G[l].push_back({r, c});
  }
  Rep(i, 1, n) { G[i].push_back({i - 1, 0}); }
  dijkstra(0);
  int ans = d[n - 1];
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
}
Problem p02868 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using i64 = long long;

int main() {
  constexpr i64 inf = std::numeric_limits<i64>::max() / 2;
  int n, m;
  std::cin >> n >> m;
  std::vector<std::tuple<int, int, i64, int>> evs;
  for (int i = 0; i < m; i++) {
    int l, r;
    i64 c;
    std::cin >> l >> r >> c;
    evs.emplace_back(l - 1, 0, c, i);
    evs.emplace_back(r - 1, 1, c, i);
  }

  std::sort(evs.begin(), evs.end());

  std::vector<i64> dist(n, inf), rr(n);
  dist[0] = 0;
  auto it = evs.begin();
  std::set<std::pair<i64, int>> ss;
  for (int i = 0; i < n; i++) {
    if (i) {
      if (ss.empty()) {
        std::cout << -1 << std::endl;
        return 0;
      }
      dist[i] = ss.begin()->first;
    }
    while (it != evs.end() && std::get<0>(*it) == i) {
      int index = std::get<3>(*it);
      if (std::get<1>(*it) == 0) {
        i64 cc = dist[i] + std::get<2>(*it);
        ss.emplace(cc, index);
        rr[index] = cc;
      } else {
        ss.erase(ss.lower_bound(std::make_pair(rr[index], index)));
      }
      it++;
    }
  }

  std::cout << dist[n - 1] << std::endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using i64 = long long;

int main() {
  constexpr i64 inf = std::numeric_limits<i64>::max() / 2;
  int n, m;
  std::cin >> n >> m;
  std::vector<std::tuple<int, int, i64, int>> evs;
  for (int i = 0; i < m; i++) {
    int l, r;
    i64 c;
    std::cin >> l >> r >> c;
    evs.emplace_back(l - 1, 0, c, i);
    evs.emplace_back(r - 1, 1, c, i);
  }

  std::sort(evs.begin(), evs.end());

  std::vector<i64> dist(n, inf), rr(m);
  dist[0] = 0;
  auto it = evs.begin();
  std::set<std::pair<i64, int>> ss;
  for (int i = 0; i < n; i++) {
    if (i) {
      if (ss.empty()) {
        std::cout << -1 << std::endl;
        return 0;
      }
      dist[i] = ss.begin()->first;
    }
    while (it != evs.end() && std::get<0>(*it) == i) {
      int index = std::get<3>(*it);
      if (std::get<1>(*it) == 0) {
        i64 cc = dist[i] + std::get<2>(*it);
        ss.emplace(cc, index);
        rr[index] = cc;
      } else {
        ss.erase(ss.lower_bound(std::make_pair(rr[index], index)));
      }
      it++;
    }
  }

  std::cout << dist[n - 1] << std::endl;

  return 0;
}
Problem p02868 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

class Edge {
public:
  long long to;
  long long cost;
  Edge() {}
  Edge(long long _to, long long _cost) {
    to = _to;
    cost = _cost;
  }
};

class Mov {
public:
  long long id;
  long long cost;
  Mov(long long _id, long long _cost) {
    id = _id;
    cost = _cost;
  }
  Mov(const Mov &m) {
    id = m.id;
    cost = m.cost;
  }
};

class Node {
public:
  long long cost;
  vector<Edge> edge;
  Node() { cost = LLONG_MAX / 2; }
};

bool has_larger_cost(const Mov &a, const Mov &b) { return a.cost > b.cost; }

int main() {
  long long nnode, nedge;
  cin >> nnode >> nedge;
  vector<Node> node(nnode);
  for (long long x = 0; x < nnode; x++) {
    for (long long y = 1; y < x; y++) {
      node.at(x).edge.emplace_back(y, 0);
    }
  }
  for (long long i = 0; i < nedge; i++) {
    long long from, to, cost;
    cin >> from >> to >> cost;
    from--;
    to--;
    node.at(from).edge.emplace_back(to, cost);
  }
  priority_queue<Mov, vector<Mov>, decltype(&has_larger_cost)> pq(
      &has_larger_cost);
  pq.emplace(0LL, 0LL);
  while (!pq.empty()) {
    auto mov = pq.top();
    pq.pop();
    if (node.at(mov.id).cost <= mov.cost) {
      continue;
    }
    node.at(mov.id).cost = mov.cost;
    for (const auto &e : node.at(mov.id).edge) {
      pq.emplace(e.to, e.cost + mov.cost);
    }
  }
  if (node.back().cost == LLONG_MAX / 2) {
    cout << "-1\n";
  } else {
    cout << node.back().cost << '\n';
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

class Edge {
public:
  long long to;
  long long cost;
  Edge() {}
  Edge(long long _to, long long _cost) {
    to = _to;
    cost = _cost;
  }
};

class Mov {
public:
  long long id;
  long long cost;
  Mov(long long _id, long long _cost) {
    id = _id;
    cost = _cost;
  }
  Mov(const Mov &m) {
    id = m.id;
    cost = m.cost;
  }
};

class Node {
public:
  long long cost;
  vector<Edge> edge;
  Node() { cost = LLONG_MAX / 2; }
};

bool has_larger_cost(const Mov &a, const Mov &b) { return a.cost > b.cost; }

int main() {
  long long nnode, nedge;
  cin >> nnode >> nedge;
  vector<Node> node(nnode);
  for (long long x = 1; x < nnode; x++) {
    node.at(x).edge.emplace_back(x - 1, 0);
  }
  for (long long i = 0; i < nedge; i++) {
    long long from, to, cost;
    cin >> from >> to >> cost;
    from--;
    to--;
    node.at(from).edge.emplace_back(to, cost);
  }
  priority_queue<Mov, vector<Mov>, decltype(&has_larger_cost)> pq(
      &has_larger_cost);
  pq.emplace(0LL, 0LL);
  while (!pq.empty()) {
    auto mov = pq.top();
    pq.pop();
    if (node.at(mov.id).cost <= mov.cost) {
      continue;
    }
    node.at(mov.id).cost = mov.cost;
    for (const auto &e : node.at(mov.id).edge) {
      pq.emplace(e.to, e.cost + mov.cost);
    }
  }
  if (node.back().cost == LLONG_MAX / 2) {
    cout << "-1\n";
  } else {
    cout << node.back().cost << '\n';
  }
  return 0;
}
Problem p02868 (C++)

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

typedef long long ll;

ll N, M;
vector<tuple<ll, ll, ll>> E;
typedef pair<ll, ll> P;

ll solve() {
  ll v = 0, d = 0, e = 0;
  priority_queue<P, vector<P>, greater<P>> que;
  while (v != N - 1) {
    for (; get<0>(E[e]) <= v; ++e) {
      ll l, r, c;
      tie(l, r, c) = E[e];
      que.push(make_pair(d + c, r));
    }
    if (que.empty())
      return -1;
    ll dx, r;
    tie(dx, r) = que.top();
    que.pop();
    d = dx;
    v = r;
  }
  return d;
}
int main() {
  cin >> N >> M;
  for (ll i = 0; i < M; ++i) {
    ll l, r, c;
    cin >> l >> r >> c;
    --l;
    --r;
    E.push_back(make_tuple(l, r, c));
  }
  sort(E.begin(), E.end());
  cout << solve() << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

ll N, M;
vector<tuple<ll, ll, ll>> E;
typedef pair<ll, ll> P;

ll solve() {
  ll v = 0, d = 0, e = 0;
  priority_queue<P, vector<P>, greater<P>> que;
  while (v != N - 1) {
    for (; e < E.size() && get<0>(E[e]) <= v; ++e) {
      ll l, r, c;
      tie(l, r, c) = E[e];
      que.push(make_pair(d + c, r));
    }
    if (que.empty())
      return -1;
    ll dx, r;
    tie(dx, r) = que.top();
    que.pop();
    d = dx;
    v = r;
  }
  return d;
}
int main() {
  cin >> N >> M;
  for (ll i = 0; i < M; ++i) {
    ll l, r, c;
    cin >> l >> r >> c;
    --l;
    --r;
    E.push_back(make_tuple(l, r, c));
  }
  sort(E.begin(), E.end());
  cout << solve() << endl;
  return 0;
}