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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 5;
int n, m;
ll t[N << 2], lz[N << 2];
vector<pair<int, int>> v[N];
void build(int l, int r, int k) {
  t[k] = lz[k] = 1e18;
  if (l == r && l == 1)
    t[k] = 0;
  if (l == r)
    return;
  int m = l + r >> 1;
  build(l, m, k << 1);
  build(m + 1, r, k << 1 | 1);
}
void update(int k) {
  t[k << 1] = min(t[k << 1], lz[k]);
  t[k << 1 | 1] = min(t[k << 1 | 1], lz[k]);
  lz[k << 1] = min(lz[k << 1], lz[k]);
  lz[k << 1 | 1] = min(lz[k << 1 | 1], lz[k]);
  lz[k] = 1e18;
}
ll query(int l, int r, int k, int x) {
  if (l != r)
    update(k);
  if (l == r)
    return t[k];
  int m = l + r >> 1;
  if (x <= m)
    return query(l, m, k << 1, x);
  else
    return query(m + 1, r, k << 1 | 1, x);
}
void fix(int l, int r, int k, int x, int y, ll v) {
  if (r < x || l > y)
    return;
  if (l != r)
    update(k);
  if (l >= x && r <= y) {
    t[k] = min(t[k], v);
    lz[k] = min(lz[k], v);
    update(k);
    return;
  }
  int m = l + r >> 1;
  fix(l, m, k << 1, x, y, v);
  fix(m + 1, r, k << 1 | 1, x, y, v);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int l, r, c;
    scanf("%d%d%d", &l, &r, &c);
    v[l].push_back({r, c});
  }
  build(1, n, 1);
  for (int i = 1; i <= n; i++) {
    ll mn = query(1, n, 1, i);
    for (int j = 0; j < v[i].size(); j++)
      fix(1, n, 1, i, v[i][j].first, mn + v[i][j].second);
  }
  ll ans = query(1, n, 1, n);
  if (ans == 1e18)
    ans = -1;
  printf("%lld\n", ans);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 5;
int n, m;
ll t[N << 2], lz[N << 2];
vector<pair<int, int>> v[N];
void build(int l, int r, int k) {
  t[k] = lz[k] = 1e18;
  if (l == r && l == 1)
    t[k] = 0;
  if (l == r)
    return;
  int m = l + r >> 1;
  build(l, m, k << 1);
  build(m + 1, r, k << 1 | 1);
}
void update(int k) {
  t[k << 1] = min(t[k << 1], lz[k]);
  t[k << 1 | 1] = min(t[k << 1 | 1], lz[k]);
  lz[k << 1] = min(lz[k << 1], lz[k]);
  lz[k << 1 | 1] = min(lz[k << 1 | 1], lz[k]);
  lz[k] = 1e18;
}
ll query(int l, int r, int k, int x) {
  if (l != r)
    update(k);
  if (l == r)
    return t[k];
  int m = l + r >> 1;
  if (x <= m)
    return query(l, m, k << 1, x);
  else
    return query(m + 1, r, k << 1 | 1, x);
}
void fix(int l, int r, int k, int x, int y, ll v) {
  if (r < x || l > y)
    return;
  if (l != r)
    update(k);
  if (l >= x && r <= y) {
    t[k] = min(t[k], v);
    lz[k] = min(lz[k], v);
    if (l != r)
      update(k);
    return;
  }
  int m = l + r >> 1;
  fix(l, m, k << 1, x, y, v);
  fix(m + 1, r, k << 1 | 1, x, y, v);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int l, r, c;
    scanf("%d%d%d", &l, &r, &c);
    v[l].push_back({r, c});
  }
  build(1, n, 1);
  for (int i = 1; i <= n; i++) {
    ll mn = query(1, n, 1, i);
    for (int j = 0; j < v[i].size(); j++)
      fix(1, n, 1, i, v[i][j].first, mn + v[i][j].second);
  }
  ll ans = query(1, n, 1, n);
  if (ans == 1e18)
    ans = -1;
  printf("%lld\n", ans);
}
Problem p02868 (C++)

Buggy submission (status: Runtime Error):
#include <cassert>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
typedef long long ll;
template <typename T> using vec = vector<T>;
template <typename T> using vvec = vector<vec<T>>;

int main() {
  int N, M;
  cin >> N >> M;
  vec<ll> L(M), R(M), C(M);
  vvec<int> lid(N + 1), rid(N + 1);
  for (int i = 0; i < M; i++) {
    cin >> L[i] >> R[i] >> C[i];
    lid[L[i]].push_back(i);
    rid[R[i]].push_back(i);
  }
  const ll inf = 1e18;
  vec<ll> dp(N + 1, inf);
  vec<ll> val(N + 1, inf);
  dp[1] = 0;
  map<ll, int> m;
  m[0] = 1;
  auto dec = [&](ll x) {
    assert(m.count(x));
    m[x]--;
    if (m[x] == 0)
      m.erase(x);
  };
  for (int i = 1; i <= N; i++) {
    if (m.empty()) {
      cout << -1 << endl;
      return 0;
    }
    dp[i] = m.begin()->first;
    for (auto &id : rid[i]) {
      if (val[id] == inf)
        continue;
      dec(val[id]);
    }
    for (auto &id : lid[i]) {
      //            cerr << i << " " << dp[i]+C[id] << endl;
      m[dp[i] + C[id]]++;
      val[id] = dp[i] + C[id];
    }
    if (i == 1)
      dec(0);
  }
  cout << (dp[N] != inf ? dp[N] : -1) << endl;
}

Fixed submission:
#include <cassert>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
typedef long long ll;
template <typename T> using vec = vector<T>;
template <typename T> using vvec = vector<vec<T>>;

int main() {
  int N, M;
  cin >> N >> M;
  vec<ll> L(M), R(M), C(M);
  vvec<int> lid(N + 1), rid(N + 1);
  for (int i = 0; i < M; i++) {
    cin >> L[i] >> R[i] >> C[i];
    lid[L[i]].push_back(i);
    rid[R[i]].push_back(i);
  }
  const ll inf = 1e18;
  vec<ll> dp(N + 1, inf);
  vec<ll> val(M + 1, inf);
  dp[1] = 0;
  map<ll, int> m;
  m[0] = 1;
  auto dec = [&](ll x) {
    assert(m.count(x));
    m[x]--;
    if (m[x] == 0)
      m.erase(x);
  };
  for (int i = 1; i <= N; i++) {
    if (m.empty()) {
      cout << -1 << endl;
      return 0;
    }
    dp[i] = m.begin()->first;
    for (auto &id : rid[i]) {
      if (val[id] == inf)
        continue;
      dec(val[id]);
    }
    for (auto &id : lid[i]) {
      //            cerr << i << " " << dp[i]+C[id] << endl;
      m[dp[i] + C[id]]++;
      val[id] = dp[i] + C[id];
    }
    if (i == 1)
      dec(0);
  }
  cout << (dp[N] != inf ? dp[N] : -1) << endl;
}
Problem p02868 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <set>
#include <vector>

using namespace std;

int main() {
  int n, m;
  cin >> n >> m;

  vector<vector<long long>> in(n, vector<long long>(0)),
      out(n, vector<long long>(0));
  vector<long long> c(m);
  for (int i = 0; i < m; i++) {
    int l, r;
    cin >> l >> r >> c[i];
    in[l - 1].push_back(i);
    out[r - 1].push_back(i);
  }

  vector<long long> dist(n, 1LL << 48), dist_c(m, 1LL << 48);
  dist[0] = 0;

  set<long long> dist_set;

  for (int i = 0; i < n; i++) {
    if (dist_set.size() > 0) {
      dist[i] = min(dist[i], *dist_set.begin());
    }

    for (int j = 0; j < static_cast<int>(in[i].size()); j++) {
      dist_c[in[i][j]] = dist[i] + c[in[i][j]];
      dist_set.insert(dist_c[in[i][j]]);
    }

    for (int j = 0; j < static_cast<int>(out[i].size()); j++) {
      dist_set.erase(dist_set.find(dist_c[out[i][j]]));
    }
  }

  if (dist[n - 1] < (1LL << 48)) {
    cout << dist[n - 1] << endl;
  } else {
    cout << "-1" << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <set>
#include <vector>

using namespace std;

int main() {
  int n, m;
  cin >> n >> m;

  vector<vector<long long>> in(n, vector<long long>(0)),
      out(n, vector<long long>(0));
  vector<long long> c(m);
  for (int i = 0; i < m; i++) {
    int l, r;
    cin >> l >> r >> c[i];
    in[l - 1].push_back(i);
    out[r - 1].push_back(i);
  }

  vector<long long> dist(n, 1LL << 48), dist_c(m, 1LL << 48);
  dist[0] = 0;

  multiset<long long> dist_set;

  for (int i = 0; i < n; i++) {
    if (dist_set.size() > 0) {
      dist[i] = min(dist[i], *dist_set.begin());
    }

    for (int j = 0; j < static_cast<int>(in[i].size()); j++) {
      dist_c[in[i][j]] = dist[i] + c[in[i][j]];
      dist_set.insert(dist_c[in[i][j]]);
    }

    for (int j = 0; j < static_cast<int>(out[i].size()); j++) {
      dist_set.erase(dist_set.find(dist_c[out[i][j]]));
    }
  }

  if (dist[n - 1] < (1LL << 48)) {
    cout << dist[n - 1] << endl;
  } else {
    cout << "-1" << endl;
  }
}
Problem p02868 (C++)

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

using namespace std;

typedef long long ll;
const ll INF = 1LL << 60;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

ll modpow(ll a, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

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

  int N, M;
  cin >> N >> M;

  vector<vector<pair<ll, ll>>> G(N);

  for (int i = 1; i < N; i++) {
    G[i].push_back(make_pair(i - 1, 0));
  }

  ll L, R, C;
  for (int i = 0; i < M; i++) {
    cin >> L >> R >> C;
    L--;
    R--;
    G[L].push_back(make_pair(R, C));
  }

  vector<ll> d(N);
  for (int i = 0; i < N; i++) {
    d[i] = INF;
  }
  d[0] = 0;

  priority_queue<pair<ll, ll>> que;
  que.push(make_pair(0, 0));

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

    for (int i = 0; i < G[v].size(); i++) {
      auto e = G[v][i];
      if (d[e.first] > d[v] + e.second) {
        d[e.first] = d[v] + e.second;
        que.push(make_pair(d[e.first], e.first));
      }
    }
  }

  if (d[N - 1] >= INF) {
    cout << -1 << endl;
  } else {
    cout << d[N - 1] << endl;
  }
  return 0;
}


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

using namespace std;

typedef long long ll;
const ll INF = 1LL << 60;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

ll modpow(ll a, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

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

  int N, M;
  cin >> N >> M;

  vector<vector<pair<ll, ll>>> G(N);

  for (int i = 1; i < N; i++) {
    G[i].push_back(make_pair(i - 1, 0));
  }

  ll L, R, C;
  for (int i = 0; i < M; i++) {
    cin >> L >> R >> C;
    L--;
    R--;
    G[L].push_back(make_pair(R, C));
  }

  vector<ll> d(N);
  for (int i = 0; i < N; i++) {
    d[i] = INF;
  }
  d[0] = 0;

  priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> que;
  que.push(make_pair(0, 0));

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

    for (int i = 0; i < G[v].size(); i++) {
      auto e = G[v][i];
      if (d[e.first] > d[v] + e.second) {
        d[e.first] = d[v] + e.second;
        que.push(make_pair(d[e.first], e.first));
      }
    }
  }

  if (d[N - 1] >= INF) {
    cout << -1 << endl;
  } else {
    cout << d[N - 1] << endl;
  }
  return 0;
}
Problem p02868 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <utility>

typedef long long ll;

#define rep(i, n) for (int i = 0; i < n; i++)
#define pb push_back
#define P pair<int, int>
#define PLI pair<ll, int>
#define PLL pair<ll, ll>
#define VI vector<int>
#define VII vector<VI>

using namespace std;

const ll INF = 1e18;

struct edge {
  int to;
  ll co;
  edge(int t, ll c) : to(t), co(c) {}
  bool operator<(const edge e) const { return co > e.co; }
};
int N, M;
vector<edge> G[100005];
ll cost[100005];
int main() {
  cin >> N >> M;
  rep(i, M) {
    int l, r, c;
    cin >> l >> r >> c;
    l--;
    r--;
    G[l].emplace_back(r, c);
  }
  rep(i, N - 1) { G[i + 1].emplace_back(i, 0); }

  rep(i, N) cost[i] = INF;
  cost[0] = 0;
  priority_queue<edge> pq;
  pq.emplace(0, 0);
  while (!pq.empty()) {
    edge now = pq.top();
    pq.pop();
    if (cost[now.to] < now.co)
      continue;
    for (auto e : G[now.to]) {
      if (cost[e.to] > cost[now.to] + e.co) {
        cost[e.to] = cost[now.to] + e.co;
        pq.emplace(e.to, e.co);
      }
    }
  }
  if (cost[N - 1] == INF)
    cout << -1 << endl;
  else
    cout << cost[N - 1] << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <utility>

typedef long long ll;

#define rep(i, n) for (int i = 0; i < n; i++)
#define pb push_back
#define P pair<int, int>
#define PLI pair<ll, int>
#define PLL pair<ll, ll>
#define VI vector<int>
#define VII vector<VI>

using namespace std;

const ll INF = 1e18;

struct edge {
  int to;
  ll co;
  edge(int t, ll c) : to(t), co(c) {}
  bool operator<(const edge e) const { return co > e.co; }
};
int N, M;
vector<edge> G[100005];
ll cost[100005];
int main() {
  cin >> N >> M;
  rep(i, M) {
    int l, r, c;
    cin >> l >> r >> c;
    l--;
    r--;
    G[l].emplace_back(r, c);
  }
  rep(i, N - 1) { G[i + 1].emplace_back(i, 0); }

  rep(i, N) cost[i] = INF;
  cost[0] = 0;
  priority_queue<edge> pq;
  pq.emplace(0, 0);
  while (!pq.empty()) {
    edge now = pq.top();
    pq.pop();
    if (cost[now.to] < now.co)
      continue;
    for (auto e : G[now.to]) {
      if (cost[e.to] > cost[now.to] + e.co) {
        cost[e.to] = cost[now.to] + e.co;
        pq.emplace(e.to, cost[e.to]);
      }
    }
  }
  if (cost[N - 1] == INF)
    cout << -1 << endl;
  else
    cout << cost[N - 1] << endl;

  return 0;
}
Problem p02868 (C++)

Buggy submission (status: Time Limit Exceeded):
// Copyright 2019 yaito3014
#include "bits/stdc++.h"
using namespace std;
using namespace literals;

struct Edge {
  int to, cost;
  Edge() = default;
  Edge(int t, int c) : to(t), cost(c) {}
};

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int N, M;
  cin >> N >> M;
  vector<vector<Edge>> G(N);
  for (int i = 0; i < N - 1; ++i)
    G[i + 1].push_back(Edge(i, 0)); // N→1にはノーコストで動ける
  for (int i = 0; i < M; ++i) {
    int L, R, C;
    cin >> L >> R >> C;
    G[--L].push_back(Edge(--R, C)); // C使ってL→Rに行ける
  }
  vector<int64_t> cost(N, INT64_MAX);
  cost[0] = 0;
  using P = pair<int64_t, int>; // first: cost, second: index
  priority_queue<P> que;
  que.push(P(0, 0));
  while (!que.empty()) {
    P cur = que.top();
    que.pop();
    for (Edge &nxt : G[cur.second]) {
      int64_t c = cur.first + nxt.cost;
      if (cost[nxt.to] <= c)
        continue;
      cost[nxt.to] = c;
      que.push(P(c, nxt.to));
    }
  }
  int64_t ans = cost[N - 1];
  if (ans == INT64_MAX)
    ans = -1;
  cout << ans << endl;
}


Fixed submission:
// Copyright 2019 yaito3014
#include "bits/stdc++.h"
using namespace std;
using namespace literals;

struct Edge {
  int to, cost;
  Edge() = default;
  Edge(int t, int c) : to(t), cost(c) {}
};

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int N, M;
  cin >> N >> M;
  vector<vector<Edge>> G(N);
  for (int i = 0; i < N - 1; ++i)
    G[i + 1].push_back(Edge(i, 0)); // N→1にはノーコストで動ける
  for (int i = 0; i < M; ++i) {
    int L, R, C;
    cin >> L >> R >> C;
    G[--L].push_back(Edge(--R, C)); // C使ってL→Rに行ける
  }
  vector<int64_t> cost(N, INT64_MAX);
  cost[0] = 0;
  using P = pair<int64_t, int>; // first: cost, second: index
  priority_queue<P, vector<P>, greater<P>> que;
  que.push(P(0, 0));
  while (!que.empty()) {
    P cur = que.top();
    que.pop();
    for (Edge &nxt : G[cur.second]) {
      int64_t c = cur.first + nxt.cost;
      if (cost[nxt.to] <= c)
        continue;
      cost[nxt.to] = c;
      que.push(P(c, nxt.to));
    }
  }
  int64_t ans = cost[N - 1];
  if (ans == INT64_MAX)
    ans = -1;
  cout << ans << endl;
}
Problem p02868 (C++)

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

typedef long double ld;
typedef long long ll;

using namespace std;

const ll inf = 1e18;
struct Segment {
  int l;
  int r;
  int c;
  bool operator<(const Segment &rhs) { return l < rhs.l; }
};

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
  freopen("input.txt", "r", stdin);
  cout << setprecision(20);
  int l, n;
  cin >> l >> n;
  vector<Segment> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i].l >> a[i].r >> a[i].c;
  }
  sort(a.begin(), a.end());
  ll ans = inf;
  set<pair<ll, int>> len;
  int dist = 0;
  int j1 = 0;
  len.insert(make_pair(0, 1));
  auto lol = make_pair(inf, l);
  len.insert(lol);
  while (!len.empty()) {
    if (dist >= len.begin()->second) {
      len.erase(len.begin());
      continue;
    }
    pair<ll, int> x = *len.begin();
    int pos = x.second;
    dist = pos;
    ll cost = x.first;
    int j2 = j1;
    while (j2 < n && a[j2].l <= pos) {
      if (a[j2].r > pos) {
        ll ccost = a[j2].c + cost;
        if (a[j2].r == l) {
          ans = min(ans, ccost);
        }
        len.insert(make_pair(ccost, a[j2].r));
      }
      j2++;
    }
    j1 = j2;
  }
  if (ans == inf) {
    cout << "-1";
  } else {
    cout << ans;
  }
  return 0;
}


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

typedef long double ld;
typedef long long ll;

using namespace std;

const ll inf = 1e18;
struct Segment {
  int l;
  int r;
  int c;
  bool operator<(const Segment &rhs) { return l < rhs.l; }
};

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
  //    freopen("input.txt", "r", stdin);
  cout << setprecision(20);
  int l, n;
  cin >> l >> n;
  vector<Segment> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i].l >> a[i].r >> a[i].c;
  }
  sort(a.begin(), a.end());
  ll ans = inf;
  set<pair<ll, int>> len;
  int dist = 0;
  int j1 = 0;
  len.insert(make_pair(0, 1));
  auto lol = make_pair(inf, l);
  len.insert(lol);
  while (!len.empty()) {
    if (dist >= len.begin()->second) {
      len.erase(len.begin());
      continue;
    }
    pair<ll, int> x = *len.begin();
    int pos = x.second;
    dist = pos;
    ll cost = x.first;
    int j2 = j1;
    while (j2 < n && a[j2].l <= pos) {
      if (a[j2].r > pos) {
        ll ccost = a[j2].c + cost;
        if (a[j2].r == l) {
          ans = min(ans, ccost);
        }
        len.insert(make_pair(ccost, a[j2].r));
      }
      j2++;
    }
    j1 = j2;
  }
  if (ans == inf) {
    cout << "-1";
  } else {
    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 int MAX_N = 100005;
const ll INF = 1e15;

struct edge {
  int to, cost;
};

vector<edge> G[MAX_N];
ll d[MAX_N];

void init() {
  rep(i, MAX_N) { d[i] = INF; }
}

int main() {
  init();
  int N, M;
  edge e;
  cin >> N >> M;
  rep(i, N - 1) {
    e.to = i;
    e.cost = 0;
    G[i + 1].push_back(e);
  }
  rep(i, M) {
    int l, r, c;
    cin >> l >> r >> c;
    e.to = r - 1;
    e.cost = c;
    G[l - 1].push_back(e);
  }

  priority_queue<P, vector<P>, greater<P>> Q;
  d[0] = 0;
  Q.push(P(0, 0));
  while (!Q.empty()) {
    P p = Q.top();
    Q.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;
        Q.push(P(d[e.to], e.to));
      }
    }
  }

  if (d[N - 1] == INF)
    cout << "-1" << endl;
  else
    cout << d[N - 1] << endl;
  return 0;
}

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 int MAX_N = 100005;
const ll INF = 1e15;

struct edge {
  int to, cost;
};

vector<edge> G[MAX_N];
ll d[MAX_N];

void init() {
  rep(i, MAX_N) { d[i] = INF; }
}

int main() {
  init();
  int N, M;
  edge e;
  cin >> N >> M;
  rep(i, N - 1) {
    e.to = i;
    e.cost = 0;
    G[i + 1].push_back(e);
  }
  rep(i, M) {
    int l, r, c;
    cin >> l >> r >> c;
    e.to = r - 1;
    e.cost = c;
    G[l - 1].push_back(e);
  }

  priority_queue<P, vector<P>, greater<P>> Q;
  d[0] = 0;
  Q.push(P(0, 0));
  while (!Q.empty()) {
    P p = Q.top();
    Q.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;
        Q.push(P(d[e.to], e.to));
      }
    }
  }

  if (d[N - 1] == INF)
    cout << "-1" << endl;
  else
    cout << d[N - 1] << endl;
  return 0;
}
Problem p02868 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(v) (v).begin(), (v).end()
#define chmin(x, y) x = min(x, y)
typedef pair<int, int> P;
typedef long long ll;
const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;
const ll MOD = 1e9 + 7;

int main() {
  int N, M;
  cin >> N >> M;

  vector<tuple<int, int, ll>> LRC(M);
  int l, r;
  ll c;
  rep(i, M) {
    cin >> l >> r >> c;
    --l, --r;
    LRC[i] = forward_as_tuple(l, r, c);
  }
  sort(all(LRC));

  vector<ll> dp(N, LINF);
  dp[0] = 0;

  rep(i, M) {
    tie(l, r, c) = LRC[i];
    // cout << l << r << c << endl;
    if (dp[l] == LINF)
      continue;
    for (int j = l + 1; j <= r; j++) {
      dp[j] = min(dp[j], dp[l] + c);
    }
  }

  // rep(j, N) cout << dp[j] << ' ';

  if (dp[N - 1] == LINF)
    cout << -1 << endl;
  else
    cout << dp[N - 1] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(v) (v).begin(), (v).end()
#define chmin(x, y) x = min(x, y)
typedef pair<int, int> P;
typedef long long ll;
const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;
const ll MOD = 1e9 + 7;

int main() {
  int N, M;
  cin >> N >> M;

  vector<tuple<int, int, ll>> LRC(M);
  int l, r;
  ll c;
  rep(i, M) {
    cin >> l >> r >> c;
    --l, --r;
    LRC[i] = forward_as_tuple(l, r, c);
  }
  sort(all(LRC));

  vector<ll> dp(N, LINF);
  dp[0] = 0;

  rep(i, M) {
    tie(l, r, c) = LRC[i];
    // cout << l << r << c << endl;
    if (dp[l] == LINF)
      continue;
    for (int j = r; j > l; j--) {
      if (dp[j] > dp[l] + c)
        dp[j] = dp[l] + c;
      else
        break;
    }
  }

  // rep(j, N) cout << dp[j] << ' ';

  if (dp[N - 1] == LINF)
    cout << -1 << endl;
  else
    cout << dp[N - 1] << endl;
  return 0;
}
Problem p02868 (C++)

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

using P = pair<int, int>;

struct edge {
  int to;
  long long cost;
};

const long long INF = 1ll << 60;
const int MAX_V = 100000;
int V;
vector<edge> G[MAX_V + 1];
long long d[MAX_V + 1];

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));
      }
    }
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  V = n;
  for (int i = 0; i < m; i++) {
    int l, r;
    long long c;
    cin >> l >> r >> c;
    l--;
    r--;
    G[l].push_back(edge{r, c});
  }
  for (int i = 0; i < n - 1; i++) {
    G[i + 1].push_back(edge{i, 0});
  }

  dijkstra(0);

  if (d[n - 1] == INF)
    cout << -1 << endl;
  else
    cout << d[n - 1] << endl;
}

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

using P = pair<long long, int>;

struct edge {
  int to;
  long long cost;
};

const long long INF = 1ll << 60;
const int MAX_V = 100000;
int V;
vector<edge> G[MAX_V + 1];
long long d[MAX_V + 1];

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));
      }
    }
  }
}

int main() {
  int n, m;
  cin >> n >> m;
  V = n;
  for (int i = 0; i < m; i++) {
    int l, r;
    long long c;
    cin >> l >> r >> c;
    l--;
    r--;
    G[l].push_back(edge{r, c});
  }
  for (int i = 0; i < n - 1; i++) {
    G[i + 1].push_back(edge{i, 0});
  }

  dijkstra(0);

  if (d[n - 1] == INF)
    cout << -1 << endl;
  else
    cout << d[n - 1] << endl;
}
Problem p02868 (C++)

Buggy submission (status: Time Limit Exceeded):
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
using in = int64_t;
#define rep(i, n) for (in i = 0; i < (in)(n); i++)
#define REP(i, a, b) for (in i = (in)(a); i < (in)(b); i++)
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define yes cout << "yes" << endl
#define no cout << "no" << endl
const double PI = 3.14159265358979323846;
const in MOD = 1000000007;
const in INF = 1e18 + 7;
const in inf = 1e9 + 7;
using P = pair<in, in>;
vector<in> dx = {0, 1, -1, 0};
vector<in> dy = {1, 0, 0, -1};

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(10);
  in n, m;
  cin >> n >> m;
  using T = tuple<in, in, in>;
  in x, y, z;
  priority_queue<P, vector<P>, greater<P>> que;
  priority_queue<T, vector<T>, greater<T>> hen;

  rep(i, m) {
    cin >> x >> y >> z;
    x--, y--;
    hen.emplace(x, y, z);
  }
  vector<in> d(n, -1);
  d[0] = 0;
  in now = 1;
  while (1) {
    tie(x, y, z) = hen.top();
    if (x == 0) {
      que.emplace(z, y);
      hen.pop();
    } else
      break;
  }

  while (!que.empty() && d[n - 1] == -1) {
    tie(x, y) = que.top();
    if (y < now) {
      que.pop();
      continue;
    }
    d[now] = x;
    while (!hen.empty()) {
      tie(x, y, z) = hen.top();
      if (x != now)
        break;
      hen.pop();
      que.emplace(z + d[now], y);
    }
    now++;
  }
  cout << d[n - 1] << endl;
} /*
 ./problem.exe
 */

Fixed submission:
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
using in = int64_t;
#define rep(i, n) for (in i = 0; i < (in)(n); i++)
#define REP(i, a, b) for (in i = (in)(a); i < (in)(b); i++)
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define yes cout << "yes" << endl
#define no cout << "no" << endl
const double PI = 3.14159265358979323846;
const in MOD = 1000000007;
const in INF = 1e18 + 7;
const in inf = 1e9 + 7;
using P = pair<in, in>;
vector<in> dx = {0, 1, -1, 0};
vector<in> dy = {1, 0, 0, -1};

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(10);
  in n, m;
  cin >> n >> m;
  using T = tuple<in, in, in>;
  in x, y, z;
  priority_queue<P, vector<P>, greater<P>> que;
  priority_queue<T, vector<T>, greater<T>> hen;

  rep(i, m) {
    cin >> x >> y >> z;
    x--, y--;
    hen.emplace(x, y, z);
  }
  vector<in> d(n, -1);
  d[0] = 0;
  in now = 1;
  while (!hen.empty()) {
    tie(x, y, z) = hen.top();
    if (x == 0) {
      que.emplace(z, y);
      hen.pop();
    } else
      break;
  }

  while (!que.empty() && d[n - 1] == -1) {
    tie(x, y) = que.top();
    if (y < now) {
      que.pop();
      continue;
    }
    d[now] = x;
    while (!hen.empty()) {
      tie(x, y, z) = hen.top();
      if (x != now)
        break;
      hen.pop();
      que.emplace(z + d[now], y);
    }
    now++;
  }
  cout << d[n - 1] << endl;
} /*
 ./problem.exe
 */
Problem p02869 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("O3")
#pragma GCC target("avx")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

const int dm = 1 << 20;
char dn[dm], *di = dn;
inline void putint(int X) {
  int keta = 0;
  char C[10];
  while (X) {
    *(C + keta) = '0' + X % 10;
    X /= 10;
    keta++;
  }
  for (int i = keta - 1; i >= 0; i--)
    *di++ = (*(C + i));
  *di++ = '\n';
}

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

  int N, K;
  scanf("%d", &N);
  scanf("%d", &K);

  if ((N + 1) / 2 >= K) {
    int B = K + N;
    int C = K + N + N;
    int b = N / 2;
    int n = !(N & 1);

    rep(i, (N + 1) / 2) {
      putint(K + i);
      putint(B + b + i);
      putint(C + i * 2);
    }
    for (int i = (N + 1) / 2; i < N; i++) {
      putint(K + i);
      putint(K + b + i);
      putint(B + i * 2 + n);
    }
    fwrite(dn, 1, di - dn, stdout);
  } else {
    printf("-1");
    return 0;
  }

  Would you please return 0;
}

Fixed submission:
#pragma GCC optimize("O3")
#pragma GCC target("avx")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

const int dm = 1 << 21;
char dn[dm], *di = dn;
inline void putint(int X) {
  int keta = 0;
  char C[10];
  while (X) {
    *(C + keta) = '0' + X % 10;
    X /= 10;
    keta++;
  }
  for (int i = keta - 1; i >= 0; i--)
    *di++ = (*(C + i));
  *di++ = '\n';
}

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

  int N, K;
  scanf("%d", &N);
  scanf("%d", &K);

  if ((N + 1) / 2 >= K) {
    int B = K + N;
    int C = K + N + N;
    int b = N / 2;
    int n = !(N & 1);

    rep(i, (N + 1) / 2) {
      putint(K + i);
      putint(B + b + i);
      putint(C + i * 2);
    }
    for (int i = (N + 1) / 2; i < N; i++) {
      putint(K + i);
      putint(K + b + i);
      putint(B + i * 2 + n);
    }
    fwrite(dn, 1, di - dn, stdout);
  } else {
    printf("-1");
    return 0;
  }

  Would you please return 0;
}
Problem p02869 (C++)

Buggy submission (status: Runtime Error):
#include "algorithm"
#include "bitset"
#include "cassert"
#include "climits"
#include "cmath"
#include "cstdio"
#include "ctime"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"

using namespace std;

constexpr long long int MOD = 1000000007;
// constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
// constexpr long long int MOD = 998244353;
constexpr long double EPS = 1e-12;

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

  long long int N, K;
  cin >> K >> N;
  if (2 * N * K + (2 * N - 1) * (2 * N) / 2 >
      N * K + 2 * N * N + N * (N - 1) / 2) {
    cout << -1 << endl;
    return 0;
  }
  vector<int> a(N);
  vector<int> b(N);
  vector<int> c(N);
  int cnt = 0;
  for (int i = 0; i < N; i += 2) {
    a[i] = K + cnt++;
  }
  for (int i = 1; i < N; i += 2) {
    a[i] = K + cnt++;
  }
  cnt = 2 * N;
  if (N & 1) {
    for (int i = N - 1; i >= 0; i -= 2) {
      b[i] = K + --cnt;
    }
    for (int i = N - 2; i >= 0; i -= 2) {
      b[i] = K + --cnt;
    }
  } else {
    for (int i = N - 2; i >= 0; i -= 2) {
      b[i] = K + --cnt;
    }
    for (int i = N - 1; i >= 0; i -= 2) {
      b[i] = K + --cnt;
    }
  }
  cnt = 2 * N;
  for (int i = 0; i < N; i++) {
    c[i] = K + cnt++;
  }
  for (int i = 0; i < N; i++) {
    cout << a[i] << " " << b[i] << " " << c[i] << endl;
  }
}

Fixed submission:
#include "algorithm"
#include "bitset"
#include "cassert"
#include "climits"
#include "cmath"
#include "cstdio"
#include "ctime"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"

using namespace std;

constexpr long long int MOD = 1000000007;
// constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
// constexpr long long int MOD = 998244353;
constexpr long double EPS = 1e-12;

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

  long long int N, K;
  cin >> N >> K;
  if (2 * N * K + (2 * N - 1) * (2 * N) / 2 >
      N * K + 2 * N * N + N * (N - 1) / 2) {
    cout << -1 << endl;
    return 0;
  }
  vector<int> a(N);
  vector<int> b(N);
  vector<int> c(N);
  int cnt = 0;
  for (int i = 0; i < N; i += 2) {
    a[i] = K + cnt++;
  }
  for (int i = 1; i < N; i += 2) {
    a[i] = K + cnt++;
  }
  cnt = 2 * N;
  if (N & 1) {
    for (int i = N - 1; i >= 0; i -= 2) {
      b[i] = K + --cnt;
    }
    for (int i = N - 2; i >= 0; i -= 2) {
      b[i] = K + --cnt;
    }
  } else {
    for (int i = N - 2; i >= 0; i -= 2) {
      b[i] = K + --cnt;
    }
    for (int i = N - 1; i >= 0; i -= 2) {
      b[i] = K + --cnt;
    }
  }
  cnt = 2 * N;
  for (int i = 0; i < N; i++) {
    c[i] = K + cnt++;
  }
  for (int i = 0; i < N; i++) {
    cout << a[i] << " " << b[i] << " " << c[i] << endl;
  }
}
Problem p02869 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define watch(x) cout << (#x) << "=" << (x) << '\n'
#define mset(d, val) memset(d, val, sizeof(d))
#define setp(x) cout << fixed << setprecision(x)
#define forn(i, a, b) for (int i = a; i < b; i++)
#define fore(i, a, b) for (int i = a; i <= b; i++)
#define pb push_back
#define F first
#define S second
#define INF ll(1e18)
#define MOD 998244353
#define pqueue priority_queue
#define fbo find_by_order
#define ook order_of_key
typedef long long ll;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef long double ld;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

#define MAXN 100005

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  ll n, k;
  cin >> n >> k;
  if (2 * k - 1 > n) {
    cout << -1 << '\n';
    return 0;
  }

  ll cur = k + 2 * n;
  ll i = k;
  ll j = cur - i;

  bool used[MAXN];
  mset(used, 0);

  while (j > i) {
    cout << i << " " << j << " " << cur << '\n';

    used[i - k] = used[j - k] = 1;
    cur++;
    i += 2;
    j--;
  }

  i = k + 1;
  j = k + 2 * n - 1;
  while (cur < k + 3 * n) {
    while (used[j - k])
      j--;

    used[i - k] = used[j - k] = 1;
    cout << i << " " << j << " " << cur << '\n';

    cur++;
    i += 2;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define watch(x) cout << (#x) << "=" << (x) << '\n'
#define mset(d, val) memset(d, val, sizeof(d))
#define setp(x) cout << fixed << setprecision(x)
#define forn(i, a, b) for (int i = a; i < b; i++)
#define fore(i, a, b) for (int i = a; i <= b; i++)
#define pb push_back
#define F first
#define S second
#define INF ll(1e18)
#define MOD 998244353
#define pqueue priority_queue
#define fbo find_by_order
#define ook order_of_key
typedef long long ll;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef long double ld;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

#define MAXN 300005

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  ll n, k;
  cin >> n >> k;
  if (2 * k - 1 > n) {
    cout << -1 << '\n';
    return 0;
  }

  ll cur = k + 2 * n;
  ll i = k;
  ll j = cur - i;

  bool used[MAXN];
  mset(used, 0);

  while (j > i) {
    cout << i << " " << j << " " << cur << '\n';

    used[i - k] = used[j - k] = 1;
    cur++;
    i += 2;
    j--;
  }

  i = k + 1;
  j = k + 2 * n - 1;
  while (cur < k + 3 * n) {
    while (used[j - k])
      j--;

    used[i - k] = used[j - k] = 1;
    cout << i << " " << j << " " << cur << '\n';

    cur++;
    i += 2;
  }

  return 0;
}
Problem p02869 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#define Maxn 100000
int k, n;
int ans[Maxn + 5][3];
int main() {
  scanf("%d%d", &k, &n);
  if (n < 2 * k - 1) {
    puts("-1");
    return 0;
  }
  int len = 0;
  for (int i = 0; i < n / 2; i++) {
    ans[++len][0] = k + n / 2 - 1 - i;
    ans[len][1] = k + n + i * 2 + 1;
    ans[len][2] = k + n * 2 + i;
  }
  for (int i = 0; i < (n + 1) / 2; i++) {
    ans[++len][0] = k + n - 1 - i;
    ans[len][1] = k + n + i * 2;
    ans[len][2] = k + n * 2 + n / 2 + i;
  }
  for (int i = 1; i <= len; i++) {
    printf("%d %d %d\n", ans[i][0], ans[i][1], ans[i][2]);
  }
  return 0;
}


Fixed submission:
#include <cstdio>
#define Maxn 100000
int k, n;
int ans[Maxn + 5][3];
int main() {
  scanf("%d%d", &n, &k);
  if (n < 2 * k - 1) {
    puts("-1");
    return 0;
  }
  int len = 0;
  for (int i = 0; i < n / 2; i++) {
    ans[++len][0] = k + n / 2 - 1 - i;
    ans[len][1] = k + n + i * 2 + 1;
    ans[len][2] = k + n * 2 + i;
  }
  for (int i = 0; i < (n + 1) / 2; i++) {
    ans[++len][0] = k + n - 1 - i;
    ans[len][1] = k + n + i * 2;
    ans[len][2] = k + n * 2 + n / 2 + i;
  }
  for (int i = 1; i <= len; i++) {
    printf("%d %d %d\n", ans[i][0], ans[i][1], ans[i][2]);
  }
  return 0;
}
Problem p02869 (C++)

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

using namespace std;

template <class T> inline T updmax(T &a, T b) { return a = max(a, b); }
template <class T> inline T updmin(T &a, T b) { return a = min(a, b); }

class Solution {
public:
  void solve() {
    long long n, k;
    cin >> n >> k;
    if (2 * k - 1 > n) {
      cout << "-1\n";
      return;
    }
    vector<vector<long long>> ans(n, vector<long long>(3, 0));
    for (int i = 0; i < n; i++)
      ans[i][2] = k + 2 * n + i;
    int idx = 0;
    for (int i = 0; i < n; i++) {
      ans[idx][1] = k + n + i;
      idx += 2;
      if (idx >= n)
        idx = 1;
    }
    idx = 1;
    for (int i = 0; i < n; i++) {
      ans[idx][0] = k + i;
      idx += 2;
      if (idx >= n)
        idx = 0;
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 3; j++)
        cout << ans[i][j] << " ";
      cout << "\n";
    }
    return;
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  Solution solution;
  solution.solve();
  return 0;
}

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

using namespace std;

template <class T> inline T updmax(T &a, T b) { return a = max(a, b); }
template <class T> inline T updmin(T &a, T b) { return a = min(a, b); }

class Solution {
public:
  void solve() {
    long long n, k;
    cin >> n >> k;
    if (2 * k - 1 > n) {
      cout << "-1\n";
      return;
    }
    vector<vector<long long>> ans(n, vector<long long>(3, 0));
    for (int i = 0; i < n; i++)
      ans[i][2] = k + 2 * n + i;
    int x = k + n;
    for (int i = 0; i < n; i += 2, x++)
      ans[i][1] = x;
    for (int i = 1; i < n; i += 2, x++)
      ans[i][1] = x;
    x = k;
    for (int i = 1; i < n; i += 2, x++)
      ans[i][0] = x;
    for (int i = 0; i < n; i += 2, x++)
      ans[i][0] = x;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 3; j++)
        cout << ans[i][j] << " ";
      cout << "\n";
    }
    return;
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  Solution solution;
  solution.solve();
  return 0;
}
Problem p02869 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
const int maxn = 1e5 + 10;

int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = (x << 3) + (x << 1) + c - 48;
    c = getchar();
  }
  return x * f;
}

int k, m, n;
int a[maxn];
int main() {
  n = read();
  k = read();
  long long sum = 0, ans = 0;
  for (register int i = 0; i < 2 * n; i++)
    sum += k + i;
  for (register int i = 2 * n; i < 3 * n; i++)
    ans += k + i;
  if (ans < sum) {
    puts("-1");
    return 0;
  }
  for (register int i = 0; i < n * 2; i++) {
    if (i % 2)
      a[i] = i / 2 + k;
    else
      a[i] = i / 2 + k + n;
  }
  if (n % 2 == 0) {
    for (register int i = 0; i < n; i++) {
      if (i % 2 == 1)
        a[i] += n;
      else
        a[i] -= n;
    }
  }
  for (register int i = 0; i < n; i++)
    printf("%d %d %d\n", a[i], a[i + n], k + n * 2 + i);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
const int maxn = 1e6 + 10;

int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = (x << 3) + (x << 1) + c - 48;
    c = getchar();
  }
  return x * f;
}

int k, m, n;
int a[maxn];
int main() {
  n = read();
  k = read();
  long long sum = 0, ans = 0;
  for (register int i = 0; i < 2 * n; i++)
    sum += k + i;
  for (register int i = 2 * n; i < 3 * n; i++)
    ans += k + i;
  if (ans < sum) {
    puts("-1");
    return 0;
  }
  for (register int i = 0; i < n * 2; i++) {
    if (i % 2)
      a[i] = i / 2 + k;
    else
      a[i] = i / 2 + k + n;
  }
  if (n % 2 == 0) {
    for (register int i = 0; i < n; i++) {
      if (i % 2 == 1)
        a[i] += n;
      else
        a[i] -= n;
    }
  }
  for (register int i = 0; i < n; i++)
    printf("%d %d %d\n", a[i], a[i + n], k + n * 2 + i);
  return 0;
}
Problem p02873 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>

using namespace std;

int main() {
  int a[100000], min;
  char s[100000];
  long int sum;
  scanf("%s", s);
  a[0] = 0;
  for (int i = 0; s[i] != '\0'; i++) {
    if (s[i] == '<')
      a[i + 1] = a[i] + 1;
    else
      a[i + 1] = a[i] - 1;
  }
  /*
  printf("%d",a[0]);
  for(int i=0;s[i]!='\0';i++) printf(",%d",a[i+1]);
  printf("\n");
  */
  for (int i = 0;; i++) {
    if (i > 0 && s[i - 1] == '\0')
      break;
    if (s[i - 1] == '>' && (s[i] == '<' || s[i] == '\0')) {
      min = -1 * a[i];
      a[i] = 0;
      for (int j = 1; s[i - j] == '>' && i - j >= 0; j++) {
        a[i - j] += min;
      }
      for (int j = 0; s[i + j] == '<' && s[i] != '\0'; j++) {
        a[i + j + 1] += min;
      }
    }
  }
  /*
  printf("%d",a[0]);
  for(int i=0;s[i]!='\0';i++)
          if(s[i]=='<'&&s[i+1]=='>'){
                  if(a[i]>a[i+2])  printf(",%d",a[i]+1);
                  else   printf(",%d",a[i+2]+1);
          }
          else  printf(",%d",a[i+1]);
  printf("\n");
  */
  sum = a[0];
  for (int i = 0; s[i] != '\0'; i++) {
    if (s[i] == '<' && s[i + 1] == '>') {
      if (a[i] > a[i + 2])
        sum += a[i] + 1;
      else
        sum += a[i + 2] + 1;
    } else
      sum += a[i + 1];
  }
  printf("%ld\n", sum);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>

using namespace std;

int main() {
  int a[500000], min;
  char s[500000];
  long int sum;
  scanf("%s", s);
  a[0] = 0;
  for (int i = 0; s[i] != '\0'; i++) {
    if (s[i] == '<')
      a[i + 1] = a[i] + 1;
    else
      a[i + 1] = a[i] - 1;
  }
  /*
  printf("%d",a[0]);
  for(int i=0;s[i]!='\0';i++) printf(",%d",a[i+1]);
  printf("\n");
  */
  for (int i = 0;; i++) {
    if (i > 0 && s[i - 1] == '\0')
      break;
    if (s[i - 1] == '>' && (s[i] == '<' || s[i] == '\0')) {
      min = -1 * a[i];
      a[i] = 0;
      for (int j = 1; s[i - j] == '>' && i - j >= 0; j++) {
        a[i - j] += min;
      }
      for (int j = 0; s[i + j] == '<' && s[i] != '\0'; j++) {
        a[i + j + 1] += min;
      }
    }
  }
  /*
  printf("%d",a[0]);
  for(int i=0;s[i]!='\0';i++)
          if(s[i]=='<'&&s[i+1]=='>'){
                  if(a[i]>a[i+2])  printf(",%d",a[i]+1);
                  else   printf(",%d",a[i+2]+1);
          }
          else  printf(",%d",a[i+1]);
  printf("\n");
  */
  sum = a[0];
  for (int i = 0; s[i] != '\0'; i++) {
    if (s[i] == '<' && s[i + 1] == '>') {
      if (a[i] > a[i + 2])
        sum += a[i] + 1;
      else
        sum += a[i + 2] + 1;
    } else
      sum += a[i + 1];
  }
  printf("%ld\n", sum);

  return 0;
}
Problem p02873 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define nmax 500005
#define oo 1000000007
#define reset(x, y) memset(x, y, sizeof(x))
#define s second
#define f first
#define PB(x) push_back(x)
#define Task ""
#define For(i, x, y) for (int i = x; i <= y; i++)
#define Fod(i, x, y) for (int i = x; i >= y; i--)
#define FOR(i, x) for (int i : x)

using namespace std;

typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vti;
typedef vector<vti> vtv;
typedef vector<ll> vli;

string s;
int n;
vti e[nmax];
int deg[nmax];
int Q[nmax];

void Enter() {
  cin >> s;
  n = s.length();
  s = ' ' + s;
  reset(deg, 0);
  For(i, 1, n) if (s[i] == '<') e[i].PB(i + 1);
  else e[i + 1].PB(i);
}

void Solve() {
  queue<int> q;
  For(i, 1, n + 1) if (!deg[i]) q.push(i);
  int dem = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int v : e[u]) {
      Q[v] = Q[u] + 1;
      q.push(v);
    }
  }
  ll res = 0;
  For(i, 1, n + 1) res += Q[i];
  cout << res;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // freopen(Task".inp", "r", stdin);
  // freopen(Task".out", "w", stdout);
  Enter();
  Solve();
}


Fixed submission:
#include <bits/stdc++.h>
#define nmax 500005
#define oo 1000000007
#define reset(x, y) memset(x, y, sizeof(x))
#define s second
#define f first
#define PB(x) push_back(x)
#define Task ""
#define For(i, x, y) for (int i = x; i <= y; i++)
#define Fod(i, x, y) for (int i = x; i >= y; i--)
#define FOR(i, x) for (int i : x)

using namespace std;

typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vti;
typedef vector<vti> vtv;
typedef vector<ll> vli;

string s;
int n;
vti e[nmax];
int deg[nmax];
int Q[nmax];

void Enter() {
  cin >> s;
  n = s.length();
  s = ' ' + s;
  reset(deg, 0);
  For(i, 1, n) if (s[i] == '<') {
    e[i].PB(i + 1);
    deg[i + 1]++;
  }
  else {
    e[i + 1].PB(i);
    deg[i]++;
  }
}

void Solve() {
  queue<int> q;
  For(i, 1, n + 1) if (!deg[i]) q.push(i);
  int dem = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int v : e[u]) {
      Q[v] = Q[u] + 1;
      q.push(v);
    }
  }
  ll res = 0;
  For(i, 1, n + 1) res += Q[i];
  cout << res;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // freopen(Task".inp", "r", stdin);
  // freopen(Task".out", "w", stdout);
  Enter();
  Solve();
}
Problem p02873 (C++)

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

const int maxn = 1e6 + 10;

int a[maxn];

long long get(int x) { return 1ll * (x - 1) * x / 2; }

int main() {
  freopen("inp.txt", "r", stdin);
  ios::sync_with_stdio(false);
  cin.tie(0);
  string s;
  cin >> s;
  int n = s.length();

  long long res = 0;
  for (int i = 0; i < n; i++) {
    int j = i;
    while (j < n && s[j] == '<') {
      j++;
    }

    int len = j - i;
    int i1 = j;
    while (j < n && s[j] == '>') {
      j++;
    }
    int len1 = j - i1;
    //		cerr << len <<" " << len1 << endl;
    res += get(min(len, len1)) + get(max(len, len1) + 1);
    i = j - 1;
  }

  cout << res;
  return 0;
}


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

const int maxn = 1e6 + 10;

int a[maxn];

long long get(int x) { return 1ll * (x - 1) * x / 2; }

int main() {
  //	freopen("inp.txt", "r", stdin);
  ios::sync_with_stdio(false);
  cin.tie(0);
  string s;
  cin >> s;
  int n = s.length();

  long long res = 0;
  for (int i = 0; i < n; i++) {
    int j = i;
    while (j < n && s[j] == '<') {
      j++;
    }

    int len = j - i;
    int i1 = j;
    while (j < n && s[j] == '>') {
      j++;
    }
    int len1 = j - i1;
    //		cerr << len <<" " << len1 << endl;
    res += get(min(len, len1)) + get(max(len, len1) + 1);
    i = j - 1;
  }

  cout << res;
  return 0;
}