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

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

int main() {
  int N, M;
  cin >> N >> M;
  vector<pair<int, int>> A(N, pair<int, int>(0, 0));
  for (int i = 0; i < N; i++) {
    cin >> A.at(i).second >> A.at(i).first;
  }
  sort(A.begin(), A.end());
  reverse(A.begin(), A.end());
  vector<int> D(M + 1, 0);
  int count = 0;
  int shortcut = 1;
  int C = M;
  ll sum = 0;
  for (int i = 0; i < N; i++) {
    for (int j = max(A.at(i).second, shortcut); j <= C + 1; j++) {
      if (j == C + 1) {
        C = A.at(i).second;
        break;
      }
      if (D.at(j) == 0) {
        D.at(j) = A.at(i).first;
        sum += D.at(j);
        count++;
        if (shortcut >= A.at(i).second) {
          shortcut = j;
        }
        break;
      }
    }
    if (count == M) {
      break;
    }
  }
  // ll sum=0;
  // for(int i=1;i<=M;i++){
  //   sum += D.at(i);
  // }
  cout << sum << endl;
}

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

int main() {
  int N, M;
  cin >> N >> M;
  vector<pair<int, int>> A(N, pair<int, int>(0, 0));
  for (int i = 0; i < N; i++) {
    cin >> A.at(i).second >> A.at(i).first;
  }
  sort(A.begin(), A.end());
  reverse(A.begin(), A.end());
  vector<int> D(M + 1, 0);
  int count = 0;
  int shortcut = 1;
  int C = M;
  ll sum = 0;
  for (int i = 0; i < N; i++) {
    for (int j = max(A.at(i).second, shortcut); j <= C + 1; j++) {
      if (j == C + 1) {
        C = A.at(i).second - 1;
        break;
      }
      if (D.at(j) == 0) {
        D.at(j) = A.at(i).first;
        sum += D.at(j);
        count++;
        if (shortcut >= A.at(i).second) {
          shortcut = j;
        }
        break;
      }
    }
    if (count == M) {
      break;
    }
  }
  // ll sum=0;
  // for(int i=1;i<=M;i++){
  //   sum += D.at(i);
  // }
  cout << sum << endl;
}
Problem p02948 (C++)

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

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> g[M + 10];
  for (int i = 0; i < N; i++) {
    int a, b;
    cin >> a >> b;
    g[a].push_back(b);
  }
  priority_queue<int> que;
  int ans = 0;
  for (int i = 1; i <= M; i++) {
    for (auto v : g[i]) {
      que.push(v);
    }
    if (!que.empty()) {
      int res = que.top();
      que.pop();
      ans += res;
    }
  }
  cout << ans << endl;
}

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

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> g[100100];
  for (int i = 0; i < N; i++) {
    int a, b;
    cin >> a >> b;
    g[a].push_back(b);
  }
  priority_queue<int> que;
  int ans = 0;
  for (int i = 1; i <= M; i++) {
    for (auto v : g[i]) {
      que.push(v);
    }
    if (!que.empty()) {
      int res = que.top();
      que.pop();
      ans += res;
    }
  }
  cout << ans << endl;
}
Problem p02948 (C++)

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<pair<int, int>> a;
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    if (x <= m) {
      a.push_back(make_pair(x, y));
    }
  }
  ll ans = 0;
  sort(a.begin(), a.end());
  priority_queue<int> pq;
  ll x = 0;
  for (int i = 1; i <= m; i++) {
    while (x < n and a[x].first <= i) {
      pq.push(a[x].second);
      x++;
    }
    if (pq.size()) {
      ans += pq.top();
      pq.pop();
    }
  }
  cout << ans << endl;
  return 0;
}

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<pair<int, int>> a;
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    if (x <= m) {
      a.push_back(make_pair(x, y));
    }
  }
  ll ans = 0;
  sort(a.begin(), a.end());
  priority_queue<int> pq;
  ll x = 0;
  for (int i = 1; i <= m; i++) {
    while (x < a.size() and a[x].first <= i) {
      pq.push(a[x].second);
      x++;
    }
    if (pq.size()) {
      ans += pq.top();
      pq.pop();
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02948 (C++)

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

using namespace std;
int n, m;
const int N = 1e5 + 7;
pair<int, int> prs[N];

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.in", "rt", stdin);
#endif
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    cin >> prs[i].second >> prs[i].first;
  }
  sort(prs, prs + n, greater<>());
  set<int> ss;
  for (int i = 0; i < m; ++i) {
    ss.insert(i + 1);
  }
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    auto it = ss.lower_bound(prs[i].second);
    if (it == ss.end())
      continue;
    ss.erase(it);
    ans += prs[i].first;
  }
  cout << ans << endl;
}

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

using namespace std;
int n, m;
const int N = 1e5 + 7;
pair<int, int> prs[N];

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    cin >> prs[i].second >> prs[i].first;
  }
  sort(prs, prs + n, greater<>());
  set<int> ss;
  for (int i = 0; i < m; ++i) {
    ss.insert(i + 1);
  }
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    auto it = ss.lower_bound(prs[i].second);
    if (it == ss.end())
      continue;
    ss.erase(it);
    ans += prs[i].first;
  }
  cout << ans << endl;
}
Problem p02948 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<ll> vll;
const long long mod = 1000000007LL;

int main() {
  int n, m;
  cin >> n >> m;
  priority_queue<int> pq;
  vector<vi> v(m + 2);
  rep(i, n) {
    int a, b;
    cin >> a >> b;
    v[a].push_back(b);
  }
  int ans = 0;
  for (int i = 1; i <= m; i++) {
    rep(j, v[i].size()) pq.push(v[i][j]);
    if (!pq.empty()) {
      int k = pq.top();
      ans += k;
      pq.pop();
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<ll> vll;
const long long mod = 1000000007LL;

int main() {
  int n, m;
  cin >> n >> m;
  priority_queue<int> pq;
  vector<vi> v(110000);
  rep(i, n) {
    int a, b;
    cin >> a >> b;
    v[a].push_back(b);
  }
  int ans = 0;
  for (int i = 1; i <= m; i++) {
    rep(j, v[i].size()) pq.push(v[i][j]);
    if (!pq.empty()) {
      int k = pq.top();
      ans += k;
      pq.pop();
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02948 (C++)

Buggy submission (status: Time Limit Exceeded):
// #include <bits/stdc++.h>
#include <algorithm> // std::sort, std::min, std::next_permutation, std::lower_bound
#include <cmath>     // std::ceil
#include <cmath>     // std::atan2
#include <iomanip> // std::setprecision
#include <iostream>
#include <limits> // std::numeric_limits<long int>::max()
#include <map>
#include <math.h>
#include <numeric> // std::gcd, std::lcm
#include <stdio.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>

using namespace std;
typedef long long ll;
#define INF 1999999999
#define MODA 1000000007
#define rep(i, n) for (long long i = 0; i < (n); ++i)

int main() {
  ll N, M;
  cin >> N >> M;
  vector<ll> A(N), B(N);
  rep(i, N) cin >> A[i] >> B[i];

  vector<pair<ll, ll>> works;
  rep(i, N) works.push_back(make_pair(B[i], A[i]));

  sort(works.begin(), works.end());

  map<ll, bool> days;
  rep(i, M + 1) days.insert(make_pair(i, true));

  ll reward = 0;
  for (int i = N - 1; i >= 0; --i) {
    // cerr << works[i].first <<" "<< works[i].second << endl;
    ll daylimit = M - works.at(i).second;
    while (daylimit >= 0) {
      if (days.find(daylimit) != days.end()) {
        reward += works.at(i).first;
        days.erase(daylimit);
        break;
      } else {
        daylimit--;
      }
    }
  }

  cout << reward << endl;
  return 0;
}


Fixed submission:
// #include <bits/stdc++.h>
#include <algorithm> // std::sort, std::min, std::next_permutation, std::lower_bound
#include <cmath>     // std::ceil
#include <cmath>     // std::atan2
#include <iomanip> // std::setprecision
#include <iostream>
#include <limits> // std::numeric_limits<long int>::max()
#include <map>
#include <math.h>
#include <numeric> // std::gcd, std::lcm
#include <stdio.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>

using namespace std;
typedef long long ll;
#define INF 1999999999
#define MODA 1000000007
#define rep(i, n) for (long long i = 0; i < (n); ++i)

int main() {
  ll N, M;
  cin >> N >> M;
  vector<ll> A(N), B(N);
  rep(i, N) cin >> A[i] >> B[i];

  vector<pair<ll, ll>> works;
  rep(i, N) works.push_back(make_pair(B[i], A[i]));

  sort(works.begin(), works.end());

  map<ll, bool> days;
  rep(i, M + 1) days.insert(make_pair(i, true));

  ll reward = 0;
  for (int i = N - 1; i >= 0; --i) {
    // cerr << works[i].first <<" "<< works[i].second << endl;
    ll daylimit = M - works.at(i).second;
    auto iter = days.upper_bound(daylimit);
    if (iter != days.begin()) {
      iter--;
      reward += works.at(i).first;
      days.erase(iter);
    }
  }

  cout << reward << endl;
  return 0;
}
Problem p02948 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define WHOLE(v) (v).begin(), (v).end()
#define REV_WHOLE(v) (v).rbegin(), (v).rend()
using i64 = int64_t;
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  vector<pair<int, int>> data;
  for (int i = 0; i < N; i++) {
    int a, b;
    cin >> a >> b;
    data.emplace_back(a, b);
  }
  i64 ans = 0;
  sort(WHOLE(data));
  auto itor = data.begin();
  priority_queue<int> rewards;
  for (int d = 1; d <= M; d++) {
    while (itor->first <= d) {
      rewards.push(itor->second);
      itor++;
    }
    if (rewards.empty())
      continue;
    ans += rewards.top();
    rewards.pop();
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define WHOLE(v) (v).begin(), (v).end()
#define REV_WHOLE(v) (v).rbegin(), (v).rend()
using i64 = int64_t;
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  vector<pair<int, int>> data;
  for (int i = 0; i < N; i++) {
    int a, b;
    cin >> a >> b;
    data.emplace_back(a, b);
  }
  i64 ans = 0;
  sort(WHOLE(data));
  auto itor = data.begin();
  priority_queue<int> rewards;
  for (int d = 1; d <= M; d++) {
    while (itor != data.end() && itor->first <= d) {
      rewards.push(itor->second);
      itor++;
    }
    if (rewards.empty())
      continue;
    ans += rewards.top();
    rewards.pop();
  }
  cout << ans << endl;
  return 0;
}
Problem p02948 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;
#define LL long long
#define mod 1000000007

int N, M, ans = 0;
pair<int, int> AB[111111];
priority_queue<int> pq;

int main() {
  cin >> N >> M;
  for (int i = 0; i < N; i++)
    cin >> AB[i].first >> AB[i].second;
  sort(AB, AB + N);
  int ok = 0;
  for (int i = 1; i <= M; i++) {
    while (AB[ok].first <= i) {
      pq.push(AB[ok].second);
      ok++;
    }
    if (pq.size() == 0)
      continue;
    ans += pq.top();
    pq.pop();
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;
#define LL long long
#define mod 1000000007

int N, M, ans = 0;
pair<int, int> AB[111111];
priority_queue<int> pq;

int main() {
  cin >> N >> M;
  for (int i = 0; i < N; i++)
    cin >> AB[i].first >> AB[i].second;
  sort(AB, AB + N);
  AB[N].first = 999999;
  int ok = 0;
  for (int i = 1; i <= M; i++) {
    while (AB[ok].first <= i) {
      pq.push(AB[ok].second);
      ok++;
    }
    if (pq.size() == 0)
      continue;
    ans += pq.top();
    pq.pop();
  }
  cout << ans << endl;
  return 0;
}
Problem p02948 (C++)

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

using namespace std;

#define int long long
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define all(v) v.begin(), v.end()
#define ld long double
#define ff first
#define ss second
#define pii pair<int, int>
#define pi 3.1415926535897932384626433832
#define mod 1000000007
#define inf 1000000000

const long double PI = acos(-1);

int powm(int a, int b) {
  int res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}

void solve() {
  int n, m;
  cin >> n >> m;

  int t, p;
  priority_queue<pii> q;
  set<int> v;

  for (int i = 1; i <= n; i++) {
    cin >> t >> p;
    q.push({p, t});
  }

  for (int i = m + 1; i <= m + m; i++)
    v.insert(i);
  // for(auto i:v)cout << i << endl;

  map<int, bool> vis;
  int ans = 0;

  while (v.size() && !q.empty()) {
    auto x = q.top();
    q.pop();
    int req = x.second + m;
    // cout << req << endl;
    auto l = lower_bound(v.begin(), v.end(), req);
    if (l == v.end())
      continue;
    // cout << *l << endl;
    // cout << x.second << " " << x.first << endl;
    ans += x.first;
    v.erase(*l);
  }
  cout << ans << endl;
}

signed main() {
  IOS;
  int t = 1;
  // cin >> t;

  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}


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

using namespace std;

#define int long long
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define all(v) v.begin(), v.end()
#define ld long double
#define ff first
#define ss second
#define pii pair<int, int>
#define pi 3.1415926535897932384626433832
#define mod 1000000007
#define inf 1000000000

const long double PI = acos(-1);

int powm(int a, int b) {
  int res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}

void solve() {
  int n, m;
  cin >> n >> m;

  int t, p;
  priority_queue<pii> q;
  set<int> v;

  for (int i = 1; i <= n; i++) {
    cin >> t >> p;
    q.push({p, t});
  }

  for (int i = m + 1; i <= m + m; i++)
    v.insert(i);
  // for(auto i:v)cout << i << endl;

  map<int, bool> vis;
  int ans = 0;

  while (v.size() && !q.empty()) {
    auto x = q.top();
    q.pop();
    int req = x.second + m;
    // cout << req << endl;
    auto l = v.lower_bound(req);
    if (l == v.end())
      continue;
    // cout << *l << endl;
    // cout << x.second << " " << x.first << endl;
    ans += x.first;
    v.erase(*l);
  }
  cout << ans << endl;
}

signed main() {
  IOS;
  int t = 1;
  // cin >> t;

  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}
Problem p02948 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < n; i++)
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> a(m);
  rep(i, n) {
    int p, q;
    cin >> p >> q;
    a[p - 1].push_back(q);
  }
  priority_queue<int> que;
  int ans = 0;
  rep(i, m) {
    for (auto p : a[i])
      que.push(p);
    if (!que.empty()) {
      ans += que.top();
      que.pop();
    }
  }
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < n; i++)
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> a(m);
  rep(i, n) {
    int p, q;
    cin >> p >> q;
    if (p <= m)
      a[p - 1].push_back(q);
  }
  priority_queue<int> que;
  int ans = 0;
  rep(i, m) {
    for (auto p : a[i])
      que.push(p);
    if (!que.empty()) {
      ans += que.top();
      que.pop();
    }
  }
  printf("%d\n", ans);
  return 0;
}
Problem p02948 (C++)

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

typedef long long ll;
typedef complex<double> Vec;

ll N, M;
vector<vector<ll>> New;
multiset<ll, greater<ll>> Now;
ll R;

int main() {
  cin >> N >> M;
  New.resize(M);
  for (ll i = 0; i < N; ++i) {
    ll a, b;
    cin >> a >> b;
    New[a - 1].push_back(b);
  }
  for (ll i = 0; i < M; ++i) {
    for (ll b : New[i])
      Now.insert(b);
    if (Now.empty())
      continue;
    auto it = Now.begin();
    R += *it;
    Now.erase(it);
  }
  cout << R << endl;
  return 0;
}


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

typedef long long ll;
typedef complex<double> Vec;

ll N, M;
vector<vector<ll>> New;
multiset<ll, greater<ll>> Now;
ll R;

int main() {
  cin >> N >> M;
  New.resize(M);
  for (ll i = 0; i < N; ++i) {
    ll a, b;
    cin >> a >> b;
    --a;
    if (a >= M)
      continue;
    New[a].push_back(b);
  }
  for (ll i = 0; i < M; ++i) {
    for (ll b : New[i])
      Now.insert(b);
    if (Now.empty())
      continue;
    auto it = Now.begin();
    R += *it;
    Now.erase(it);
  }
  cout << R << endl;
  return 0;
}
Problem p02948 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
const int N = 1e5 + 4;
int n, m;
int tot;
struct Node {
  int a, b;
  friend bool operator<(const Node &x, const Node &y) { return x.b < y.b; }
} p[N];
bool cmp(Node c, Node d) { return c.a < d.a; }
priority_queue<Node> q;
inline int read() {
  int x = 0;
  char c = getchar();
  while (c < '0' || c > '9')
    c = getchar();
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return x;
}
int main() {
  n = read(), m = read();
  for (register int i = 1; i <= n; ++i) {
    int a = read(), b = read();
    if (a <= m)
      p[++tot].a = a, p[tot].b = b;
  }
  sort(p + 1, p + tot + 1, cmp);
  int cur = 0, sum = 0;
  for (register int i = 1; i <= m; ++i) {
    while (p[cur + 1].a <= i && cur < tot) {
      ++cur;
      q.push(p[cur]);
    }
    sum += q.top().b;
    q.pop();
  }
  printf("%d\n", sum);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
const int N = 1e5 + 4;
int n, m;
int tot;
struct Node {
  int a, b;
  friend bool operator<(const Node &x, const Node &y) { return x.b < y.b; }
} p[N];
bool cmp(Node c, Node d) { return c.a < d.a; }
priority_queue<Node> q;
inline int read() {
  int x = 0;
  char c = getchar();
  while (c < '0' || c > '9')
    c = getchar();
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return x;
}
int main() {
  n = read(), m = read();
  for (register int i = 1; i <= n; ++i) {
    int a = read(), b = read();
    if (a <= m)
      p[++tot].a = a, p[tot].b = b;
  }
  sort(p + 1, p + tot + 1, cmp);
  int cur = 0, sum = 0;
  for (register int i = 1; i <= m; ++i) {
    while (p[cur + 1].a <= i && cur < tot) {
      ++cur;
      q.push(p[cur]);
    }
    if (!q.empty()) {
      sum += q.top().b;
      q.pop();
    }
  }
  printf("%d\n", sum);
  return 0;
}
Problem p02948 (C++)

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

using namespace std;
typedef long long ll;

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)

int main() {
  // ここに処理を記述

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

  vector<pair<int, int>> v;
  int A, B;
  for (int i = 0; i < N; i++) {
    cin >> A >> B;
    v.push_back(make_pair(A, B));
  }

  // 探索はM日後から(M-1日目の仕事から選ぶ) --> A.first <=
  // 1となるもののうち最大報酬を選択
  // 探索が進めば，A.firstの候補が増えるがそのうち最大を選び続ける

  // A.firstに従ってソート
  sort(v.begin(), v.end());

  // その時点で実行可能な仕事を優先度付きキューに追加
  priority_queue<int> task;
  int ans = 0;
  int p = 0;
  REP(i, M) {
    while (v[p].first <= i + 1) {
      task.push(v[p].second);
      p++;
      if (p == v.size())
        break;
    }
    // cout << task.top() << "\n";
    if (task.size() != 0) {
      ans += task.top();
      task.pop();
    }
  }

  cout << ans << "\n";

  return 0;
}

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

using namespace std;
typedef long long ll;

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)

int main() {
  // ここに処理を記述

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

  vector<pair<int, int>> v;
  int A, B;
  for (int i = 0; i < N; i++) {
    cin >> A >> B;
    v.push_back(make_pair(A, B));
  }

  // 探索はM日後から(M-1日目の仕事から選ぶ) --> A.first <=
  // 1となるもののうち最大報酬を選択
  // 探索が進めば，A.firstの候補が増えるがそのうち最大を選び続ける

  // A.firstに従ってソート
  sort(v.begin(), v.end());

  // その時点で実行可能な仕事を優先度付きキューに追加
  priority_queue<int> task;
  int ans = 0;
  int p = 0;
  REP(i, M) {
    while (v[p].first <= i + 1) {
      task.push(v[p].second);
      p++;
      if (p >= v.size())
        break;
    }
    // cout << task.top() << "\n";
    if (task.size() != 0) {
      ans += task.top();
      task.pop();
    }
  }

  cout << ans << "\n";

  return 0;
}
Problem p02948 (C++)

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

#define ll long long
#define ull unsigned long long
#define pb push_back
#define pii pair<int, int>
#define sz size()

#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << "\n";
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " , ";
  __f(comma + 1, args...);
}
#define traceloop(x, a)                                                        \
  {                                                                            \
    cerr << #a << ": ";                                                        \
    for (ll ix = 0; ix < x; ix++)                                              \
      cerr << a[ix] << " ";                                                    \
    cerr << endl;                                                              \
  }

int main() {
  ios::sync_with_stdio(0);

  int n, i, j, m;
  cin >> n >> m;
  vector<pii> v;
  for (i = 0; i < n; ++i) {
    int u, v1;
    cin >> u >> v1;
    v.pb({u, v1});
  }

  sort(v.begin(), v.end());
  priority_queue<int> pq;

  i = j = 0;
  ll ans = 0;
  for (i = 1; i <= m; ++i) {
    while (j < n and v[j].first <= i) {
      pq.push(v[j].second);
      j++;
    }

    ans += pq.top();
    pq.pop();
  }

  cout << ans << "\n";

  return 0;
}

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

#define ll long long
#define ull unsigned long long
#define pb push_back
#define pii pair<int, int>
#define sz size()

#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << "\n";
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " , ";
  __f(comma + 1, args...);
}
#define traceloop(x, a)                                                        \
  {                                                                            \
    cerr << #a << ": ";                                                        \
    for (ll ix = 0; ix < x; ix++)                                              \
      cerr << a[ix] << " ";                                                    \
    cerr << endl;                                                              \
  }

int main() {
  ios::sync_with_stdio(0);

  int n, i, j, m;
  cin >> n >> m;
  vector<pii> v;
  for (i = 0; i < n; ++i) {
    int u, v1;
    cin >> u >> v1;
    v.pb({u, v1});
  }

  sort(v.begin(), v.end());
  priority_queue<int> pq;

  i = j = 0;
  ll ans = 0;
  for (i = 1; i <= m; ++i) {
    while (j < n and v[j].first <= i) {
      pq.push(v[j].second);
      j++;
    }

    if (!pq.empty()) {
      ans += pq.top();
      pq.pop();
    }
  }

  cout << ans << "\n";

  return 0;
}
Problem p02948 (C++)

Buggy submission (status: Runtime Error):
/*

*/
#include <algorithm>
#include <cstdlib>
#include <iomanip>
#include <ios>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repb(i, a, b) for (int i = a; i < b; i++)
#define MAX_N 10000

using namespace std;
typedef pair<int, int> P;
typedef long long int ll;

const int INF = 1 << 30;
const ll MOD = ll(1e9 + 7);

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> jobs(m, vector<int>());
  rep(i, n) {
    int a, b;
    cin >> a >> b;
    jobs[a - 1].push_back(b);
  }
  priority_queue<int> que;
  int ans = 0;
  rep(i, m) {
    for (auto &e : jobs[i])
      que.push(e);
    if (!que.empty()) {
      ans += que.top();
      que.pop();
    }
  }
  cout << ans << endl;
  // system("read -n 1 -s -p \"Press any key to continue...\"");
}


Fixed submission:
/*

*/
#include <algorithm>
#include <cstdlib>
#include <iomanip>
#include <ios>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repb(i, a, b) for (int i = a; i < b; i++)
#define MAX_N 10000

using namespace std;
typedef pair<int, int> P;
typedef long long int ll;

const int INF = 1 << 30;
const ll MOD = ll(1e9 + 7);

int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int>> jobs(m, vector<int>());
  rep(i, n) {
    int a, b;
    cin >> a >> b;
    a--;
    if (a < m)
      jobs[a].push_back(b);
  }
  priority_queue<int> que;
  int ans = 0;
  rep(i, m) {
    for (auto &e : jobs[i])
      que.push(e);
    if (!que.empty()) {
      ans += que.top();
      que.pop();
    }
  }
  cout << ans << endl;
  // system("read -n 1 -s -p \"Press any key to continue...\"");
}
Problem p02948 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define REP(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)

#define ALL(x) (x).begin(), (x).end()
typedef long long ll;

#define y0 y3487465
#define y1 y8687969
#define j0 j1347829
#define j1 j234892
#define next asdnext
#define prev asdprev

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> *v = new vector<int>[M + 1];
  REP(i, N) {
    int a, b;
    cin >> a >> b;
    v[a].push_back(b);
  }

  priority_queue<int> pq;
  int ans = 0;
  REP(i, 1, M + 1) {
    for (auto x : v[i])
      pq.push(x);
    if (pq.empty())
      continue;
    ans += pq.top();
    pq.pop();
  }

  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define REP(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)

#define ALL(x) (x).begin(), (x).end()
typedef long long ll;

#define y0 y3487465
#define y1 y8687969
#define j0 j1347829
#define j1 j234892
#define next asdnext
#define prev asdprev

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> v[100001];
  REP(i, N) {
    int a, b;
    cin >> a >> b;
    v[a].push_back(b);
  }

  priority_queue<int> pq;
  int ans = 0;
  REP(i, 1, M + 1) {
    for (auto x : v[i])
      pq.push(x);
    if (pq.empty())
      continue;
    ans += pq.top();
    pq.pop();
  }

  cout << ans << endl;
}
Problem p02948 (C++)

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

#define rep(i, n) for (int i = 0; i < (n); i++)

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> P[100000];
  int A, B;
  rep(i, N) {
    cin >> A >> B;
    P[A].emplace_back(B);
  }
  priority_queue<int> que;
  long long res = 0;
  for (int i = 1; i <= M; i++) {
    for (int p : P[i])
      que.push(p);
    if (!que.empty()) {
      res += que.top();
      que.pop();
    }
  }
  cout << res << endl;

  return 0;
}

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

#define rep(i, n) for (int i = 0; i < (n); i++)

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> P[100001];
  int A, B;
  rep(i, N) {
    cin >> A >> B;
    P[A].emplace_back(B);
  }
  priority_queue<int> que;
  long long res = 0;
  for (int i = 1; i <= M; i++) {
    for (int p : P[i])
      que.push(p);
    if (!que.empty()) {
      res += que.top();
      que.pop();
    }
  }
  cout << res << endl;

  return 0;
}
Problem p02948 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#define maxn 100005
using namespace std;
priority_queue<int> q;
vector<int> G[maxn];
int read() {
  int cnt = 0;
  char ch = getchar();
  while (ch < '0' || '9' < ch)
    ch = getchar();
  while ('0' <= ch && ch <= '9') {
    cnt = cnt * 10 + ch - '0';
    ch = getchar();
  }
  return cnt;
}
int main() {
  int n = read(), m = read();
  for (int i = 1; i <= n; ++i) {
    int a = read(), b = read();
    G[m - a + 1].push_back(b);
  }
  int ans = 0;
  for (int i = m; i; --i) {
    for (int j = 0; j < G[i].size(); ++j)
      q.push(G[i][j]);
    if (q.size()) {
      ans += q.top();
      q.pop();
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#define maxn 100005
using namespace std;
priority_queue<int> q;
vector<int> G[maxn];
int read() {
  int cnt = 0;
  char ch = getchar();
  while (ch < '0' || '9' < ch)
    ch = getchar();
  while ('0' <= ch && ch <= '9') {
    cnt = cnt * 10 + ch - '0';
    ch = getchar();
  }
  return cnt;
}
int main() {
  int n = read(), m = read();
  for (int i = 1; i <= n; ++i) {
    int a = read(), b = read();
    if (a <= m)
      G[m - a + 1].push_back(b);
  }
  int ans = 0;
  for (int i = m; i; --i) {
    for (int j = 0; j < G[i].size(); ++j)
      q.push(G[i][j]);
    if (q.size()) {
      ans += q.top();
      q.pop();
    }
  }
  cout << ans << endl;
}
Problem p02949 (C++)

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

const int INF = 1001001001;

vector<int> to[2505];
vector<int> rto[2505];
bool reachableFrom1[2505];
bool reachableToN[2505];
bool ok[2505];

void dfs(int v) {
  if (reachableFrom1[v])
    return;
  reachableFrom1[v] = true;
  for (int u : to[v]) {
    dfs(u);
  }
}
void rdfs(int v) {
  if (reachableToN[v])
    return;
  reachableToN[v] = true;
  for (int u : rto[v]) {
    rdfs(u);
  }
}

int main() {
  int n, m, p;
  cin >> n >> m >> p;
  vector<tuple<int, int, int>> edges;
  rep(i, m) {
    int a, b, c;
    --a;
    --b;
    c -= p;
    c = -c;
    edges.emplace_back(a, b, c);
    to[a].push_back(b);
    rto[b].push_back(a);
  }
  dfs(0);
  rdfs(n - 1);
  rep(i, n) ok[i] = reachableFrom1[i] & reachableToN[i];
  { // bellman-ford
    vector<int> d(n, INF);
    d[0] = 0;
    bool upd = true;
    int step = 0;
    while (upd) {
      upd = false;
      rep(i, m) {
        int a, b, c;
        tie(a, b, c) = edges[i];
        if (!ok[a])
          continue;
        if (!ok[b])
          continue;
        int newD = d[a] + c;
        if (newD < d[b]) {
          upd = true;
          d[b] = newD;
        }
      }
      step++;
      if (step > n) {
        puts("-1");
        return 0;
      }
    }
    int ans = -d[n - 1];
    ans = max(ans, 0);
    cout << ans << 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;

const int INF = 1001001001;

vector<int> to[2505];
vector<int> rto[2505];
bool reachableFrom1[2505];
bool reachableToN[2505];
bool ok[2505];

void dfs(int v) {
  if (reachableFrom1[v])
    return;
  reachableFrom1[v] = true;
  for (int u : to[v]) {
    dfs(u);
  }
}
void rdfs(int v) {
  if (reachableToN[v])
    return;
  reachableToN[v] = true;
  for (int u : rto[v]) {
    rdfs(u);
  }
}

int main() {
  int n, m, p;
  cin >> n >> m >> p;
  vector<tuple<int, int, int>> edges;
  rep(i, m) {
    int a, b, c;
    cin >> a >> b >> c;
    --a;
    --b;
    c -= p;
    c = -c;
    edges.emplace_back(a, b, c);
    to[a].push_back(b);
    rto[b].push_back(a);
  }
  dfs(0);
  rdfs(n - 1);
  rep(i, n) ok[i] = reachableFrom1[i] & reachableToN[i];
  { // bellman-ford
    vector<int> d(n, INF);
    d[0] = 0;
    bool upd = true;
    int step = 0;
    while (upd) {
      upd = false;
      rep(i, m) {
        int a, b, c;
        tie(a, b, c) = edges[i];
        if (!ok[a])
          continue;
        if (!ok[b])
          continue;
        int newD = d[a] + c;
        if (newD < d[b]) {
          upd = true;
          d[b] = newD;
        }
      }
      step++;
      if (step > n) {
        puts("-1");
        return 0;
      }
    }
    int ans = -d[n - 1];
    ans = max(ans, 0);
    cout << ans << endl;
  }
  return 0;
}
Problem p02949 (C++)

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

using namespace std;

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

#define FOR(i, n, m) for (ll(i) = (m); (i) < (n); ++(i))
#define REP(i, n) FOR(i, n, 0)
#define OF64 std::setprecision(10)

const ll MOD = 1000000007;
const ll INF = (ll)1e15;

struct Edge {
  ll to;
  ll from;
  ll cost;
};

Edge E[5005];
ll C[2005];
bool U[2005];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N, M, P;
  cin >> N >> M >> P;
  REP(i, M) {
    ll a, b, c;
    cin >> a >> b >> c;
    E[i].to = b - 1;
    E[i].from = a - 1;
    E[i].cost = -(c - P);
  }

  REP(i, N) { C[i] = INF; }
  C[0] = 0;
  memset(U, 0, sizeof(U));
  queue<ll> q;
  q.push(N - 1);
  U[N - 1] = true;
  while (!q.empty()) {
    ll t = q.front();
    q.pop();
    REP(i, M) {
      if (E[i].to != t)
        continue;
      ll n = E[i].from;
      if (U[n])
        continue;
      U[n] = true;
      q.push(n);
    }
  }

  REP(_, N + 5) {
    bool update = false;
    REP(i, M) {
      const auto &e = E[i];
      if (C[e.from] == INF)
        continue;
      ll cost = C[e.from] + e.cost;
      if (C[e.to] > cost) {
        update = true;
        C[e.to] = cost;
      }
    }
    if (!update)
      break;
  }

  REP(i, M) {
    const auto &e = E[i];
    if (!U[e.to])
      continue;
    if (C[e.from] == INF)
      continue;
    ll cost = C[e.from] + e.cost;
    if (C[e.to] > cost) {
      cout << -1 << endl;
      return 0;
    }
  }

  cout << std::max(0LL, -C[N - 1]) << endl;
  return 0;
}

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

using namespace std;

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

#define FOR(i, n, m) for (ll(i) = (m); (i) < (n); ++(i))
#define REP(i, n) FOR(i, n, 0)
#define OF64 std::setprecision(10)

const ll MOD = 1000000007;
const ll INF = (ll)1e15;

struct Edge {
  ll to;
  ll from;
  ll cost;
};

Edge E[5005];
ll C[2505];
bool U[2505];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N, M, P;
  cin >> N >> M >> P;
  REP(i, M) {
    ll a, b, c;
    cin >> a >> b >> c;
    E[i].to = b - 1;
    E[i].from = a - 1;
    E[i].cost = -(c - P);
  }

  REP(i, N) { C[i] = INF; }
  C[0] = 0;
  memset(U, 0, sizeof(U));
  queue<ll> q;
  q.push(N - 1);
  U[N - 1] = true;
  while (!q.empty()) {
    ll t = q.front();
    q.pop();
    REP(i, M) {
      if (E[i].to != t)
        continue;
      ll n = E[i].from;
      if (U[n])
        continue;
      U[n] = true;
      q.push(n);
    }
  }

  REP(_, N + 5) {
    bool update = false;
    REP(i, M) {
      const auto &e = E[i];
      if (C[e.from] == INF)
        continue;
      ll cost = C[e.from] + e.cost;
      if (C[e.to] > cost) {
        update = true;
        C[e.to] = cost;
      }
    }
    if (!update)
      break;
  }

  REP(i, M) {
    const auto &e = E[i];
    if (!U[e.to])
      continue;
    if (C[e.from] == INF)
      continue;
    ll cost = C[e.from] + e.cost;
    if (C[e.to] > cost) {
      cout << -1 << endl;
      return 0;
    }
  }

  cout << std::max(0LL, -C[N - 1]) << endl;
  return 0;
}
Problem p02949 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <queue>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ll long long
using namespace std;
vector<int> to[2505];
vector<int> rto[2505];
bool from1[2505];
bool toN[2505];

void dfs(int v) {
  if (from1[v])
    return;
  from1[v] = true;
  for (int u : to[v]) {
    dfs(u);
  }
}
void rdfs(int v) {
  if (toN[v])
    return;
  toN[v] = true;
  for (int u : rto[v]) {
    rdfs(u);
  }
}

bool ok[2525];

int main() {
  int n, m, p;
  cin >> n >> m >> p;
  vector<tuple<int, int, int>> edges;
  rep(i, m) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    c -= p;
    c = -c;
    edges.emplace_back(a, b, c);
    to[a].push_back(b);
    rto[b].push_back(a);
  }
  dfs(0);
  rdfs(n - 1);
  rep(i, n) ok[i] = from1[i] & toN[i];
  // bellman_ford
  vector<int> d(n, 1001001001);
  d[0] = 0;
  bool upd = true;
  int step = 0;
  while (upd) {
    upd = false;
    rep(i, m) {
      int a, b, c;
      tie(a, b, c) = edges[i];
      if (!ok[a])
        continue;
      if (!ok[b])
        continue;
      int newD = d[a] + c;
      if (newD < d[b]) {
        upd = true;
        d[b] = newD;
      }
    }
    step++;
  }
  if (step > n) {
    cout << -1 << endl;
    return 0;
  }
  int ans = -d[n - 1];
  ans = max(0, ans);
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <queue>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ll long long
using namespace std;
vector<int> to[2505];
vector<int> rto[2505];
bool from1[2505];
bool toN[2505];

void dfs(int v) {
  if (from1[v])
    return;
  from1[v] = true;
  for (int u : to[v]) {
    dfs(u);
  }
}
void rdfs(int v) {
  if (toN[v])
    return;
  toN[v] = true;
  for (int u : rto[v]) {
    rdfs(u);
  }
}

bool ok[2525];

int main() {
  int n, m, p;
  cin >> n >> m >> p;
  vector<tuple<int, int, int>> edges;
  rep(i, m) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    c -= p;
    c = -c;
    edges.emplace_back(a, b, c);
    to[a].push_back(b);
    rto[b].push_back(a);
  }
  dfs(0);
  rdfs(n - 1);
  rep(i, n) ok[i] = from1[i] & toN[i];
  // bellman_ford
  vector<int> d(n, 1001001001);
  d[0] = 0;
  bool upd = true;
  int step = 0;
  while (upd) {
    upd = false;
    rep(i, m) {
      int a, b, c;
      tie(a, b, c) = edges[i];
      if (!ok[a])
        continue;
      if (!ok[b])
        continue;
      int newD = d[a] + c;
      if (newD < d[b]) {
        upd = true;
        d[b] = newD;
      }
    }
    step++;
    if (step > n) {
      cout << -1 << endl;
      return 0;
    }
  }
  int ans = -d[n - 1];
  ans = max(0, ans);
  cout << ans << endl;
  return 0;
}
Problem p02949 (C++)

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

#ifdef LOCAL_DEBUG
#include "LOCAL_DEBUG.hpp"
#endif

struct edge {
  int to, cost;
};
vector<edge> G[3010];
vector<int> RG[3010], can_use(3010, 0);

vector<int> dist;
int bellmanford(int n, int s) { // 頂点数、始点
  const int INF = 1LL << 60;
  dist = vector<int>(n, -INF);
  dist[s] = 0;
  for (int i = 0; i < n; i++) {
    for (int u = 0; u < n; u++) {
      if (!can_use[u])
        continue;
      for (auto e : G[u]) {
        if (dist[u] != -INF && dist[e.to] < dist[u] + e.cost) {
          dist[e.to] = dist[u] + e.cost;
          // n回目にも更新があるなら負の閉路が存在
          if (i == n - 1)
            return 1;
        }
      }
    }
  }
  return 0;
}

signed main() {

  int n, m, p;
  cin >> n >> m >> p;
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--, b--;
    G[a].push_back({b, c - p});
    RG[b].push_back(a);
  }

  function<void(int, int)> dfs = [&](int u, int pre) {
    can_use[u] = 1;
    for (int v : RG[u]) {
      if (v == pre)
        continue;
      dfs(v, u);
    }
  };
  dfs(n - 1, -9);

  if (bellmanford(n, 0)) {
    cout << -1 << endl;
  } else {
    cout << max(0LL, dist[n - 1]) << endl;
  }

  return 0;
}


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

#ifdef LOCAL_DEBUG
#include "LOCAL_DEBUG.hpp"
#endif

struct edge {
  int to, cost;
};
vector<edge> G[3010];
vector<int> RG[3010], can_use(3010, 0);

vector<int> dist;
int bellmanford(int n, int s) { // 頂点数、始点
  const int INF = 1LL << 60;
  dist = vector<int>(n, -INF);
  dist[s] = 0;
  for (int i = 0; i < n; i++) {
    for (int u = 0; u < n; u++) {
      if (!can_use[u])
        continue;
      for (auto e : G[u]) {
        if (dist[u] != -INF && dist[e.to] < dist[u] + e.cost) {
          dist[e.to] = dist[u] + e.cost;
          // n回目にも更新があるなら負の閉路が存在
          if (i == n - 1)
            return 1;
        }
      }
    }
  }
  return 0;
}

signed main() {

  int n, m, p;
  cin >> n >> m >> p;
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--, b--;
    G[a].push_back({b, c - p});
    RG[b].push_back(a);
  }

  function<void(int, int)> dfs = [&](int u, int pre) {
    if (can_use[u])
      return;
    can_use[u] = 1;
    for (int v : RG[u]) {
      if (v == pre)
        continue;
      dfs(v, u);
    }
  };
  dfs(n - 1, -9);

  if (bellmanford(n, 0)) {
    cout << -1 << endl;
  } else {
    cout << max(0LL, dist[n - 1]) << endl;
  }

  return 0;
}
Problem p02949 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#define N 2007
#define M 5007
#define int long long
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 3) + (x << 1) + (ch ^ 48);
    ch = getchar();
  }
  return x * f;
}
const int INF = 1e18; // longlongµÄINF²»ÊÇ0x3f3f3f
int n, m, p;
int f[N];
struct Edge {
  int u, v, d;
} E[M];
signed main() {
  n = read(), m = read(), p = read();
  for (int i = 1, u, v, d; i <= m; ++i) {
    u = read(), v = read(), d = read();
    E[i] = (Edge){u, v, d - p};
  }
  for (int i = 1; i <= n; ++i)
    f[i] = -INF;
  f[1] = 0;
  for (int i = 1; i <= n * 2; ++i) {
    for (int j = 1; j <= m; ++j) {
      int u = E[j].u, v = E[j].v, d = E[j].d;
      if (f[u] == -INF)
        continue;
      if (f[u] + d > f[v]) {
        f[v] = i <= n ? f[u] + d : INF;
      }
    }
  }
  if (f[n] == INF)
    puts("-1");
  else if (f[n] < 0)
    puts("0");
  else
    printf("%lld\n", f[n]);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#define N 2507
#define M 5007
#define int long long
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 3) + (x << 1) + (ch ^ 48);
    ch = getchar();
  }
  return x * f;
}
const int INF = 1e18; // longlongµÄINF²»ÊÇ0x3f3f3f
int n, m, p;
int f[N];
struct Edge {
  int u, v, d;
} E[M];
signed main() {
  n = read(), m = read(), p = read();
  for (int i = 1, u, v, d; i <= m; ++i) {
    u = read(), v = read(), d = read();
    E[i] = (Edge){u, v, d - p};
  }
  for (int i = 1; i <= n; ++i)
    f[i] = -INF;
  f[1] = 0;
  for (int i = 1; i <= n * 2; ++i) {
    for (int j = 1; j <= m; ++j) {
      int u = E[j].u, v = E[j].v, d = E[j].d;
      if (f[u] == -INF)
        continue;
      if (f[u] + d > f[v]) {
        f[v] = i <= n ? f[u] + d : INF;
      }
    }
  }
  if (f[n] == INF)
    puts("-1");
  else if (f[n] < 0)
    puts("0");
  else
    printf("%lld\n", f[n]);
  return 0;
}
Problem p02949 (C++)

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

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

  int a[5010], b[5010];

  long long c[5010], dist[5010];

  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i] >> c[i];

    c[i] = -c[i];
    c[i] += p;
    a[i]--, b[i]--;
  }

  dist[0] = 0;

  for (int i = 1; i < n; i++) {
    dist[i] = 1000000009;
  }

  for (int j = 0; j < n - 1; j++) {

    for (int i = 0; i < m; i++) {
      if (dist[a[i]] == 1000000009)
        continue;

      if (dist[b[i]] > dist[a[i]] + c[i]) {
        dist[b[i]] = dist[a[i]] + c[i];
      }
    }
  }

  long long ans = dist[n - 1];

  bool abc[1010];

  for (int i = 0; i < n; i++) {
    abc[i] = false;
  }

  for (int j = 0; j < n; j++) {
    for (int i = 0; i < m; i++) {
      if (dist[a[i]] == 1000000009)
        continue;
      if (dist[b[i]] > dist[a[i]] + c[i]) {
        dist[b[i]] = dist[a[i]] + c[i];

        abc[b[i]] = true;
      }
      if (abc[a[i]])
        abc[b[i]] = true;
    }
  }

  if (abc[n - 1])
    cout << "-1" << endl;
  else
    cout << max((long long)0, -ans) << endl;
}


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

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

  int a[5010], b[5010];

  long long c[5010], dist[5010];

  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i] >> c[i];

    c[i] = -c[i];
    c[i] += p;
    a[i]--, b[i]--;
  }

  dist[0] = 0;

  for (int i = 1; i < n; i++) {
    dist[i] = 1000000009;
  }

  for (int j = 0; j < n - 1; j++) {

    for (int i = 0; i < m; i++) {
      if (dist[a[i]] == 1000000009)
        continue;

      if (dist[b[i]] > dist[a[i]] + c[i]) {
        dist[b[i]] = dist[a[i]] + c[i];
      }
    }
  }

  long long ans = dist[n - 1];

  bool abc[5010];

  for (int i = 0; i < n; i++) {
    abc[i] = false;
  }

  for (int j = 0; j < n; j++) {
    for (int i = 0; i < m; i++) {
      if (dist[a[i]] == 1000000009)
        continue;
      if (dist[b[i]] > dist[a[i]] + c[i]) {
        dist[b[i]] = dist[a[i]] + c[i];

        abc[b[i]] = true;
      }
      if (abc[a[i]])
        abc[b[i]] = true;
    }
  }

  if (abc[n - 1])
    cout << "-1" << endl;
  else
    cout << max((long long)0, -ans) << endl;
}