Problem p02655 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define db double
#define pint pair<int, int>
#define mk(x, y) make_pair(x, y)
#define pb(x) push_back(x)
#define eb(x, y) emplace_back(x, y)
#define fi first
#define se second
#define Rep(x, y, z) for (int x = y; x <= z; x++)
#define Red(x, y, z) for (int x = y; x >= z; x--)
using namespace std;
const int INF = 1e9, MAXN = 1e5 + 5;
char buf[1 << 12], *pp1 = buf, *pp2 = buf, nc;
int ny;
inline char gc() {
  return pp1 == pp2 &&
                 (pp2 = (pp1 = buf) + fread(buf, 1, 1 << 12, stdin), pp1 == pp2)
             ? EOF
             : *pp1++;
}
// inline char gc(){return getchar();}
inline int read() {
  int x = 0;
  for (ny = 1; nc = gc(), (nc < 48 || nc > 57) && nc != EOF;)
    if (nc == 45)
      ny = -1;
  if (nc < 0)
    return nc;
  for (x = nc - 48; nc = gc(), 47 < nc && nc < 58 && nc != EOF;
       x = (x << 3) + (x << 1) + (nc ^ 48))
    ;
  return x * ny;
}
int n, m, a[MAXN], b[MAXN], c[MAXN], f[MAXN], s[MAXN], cir[MAXN], in[MAXN],
    out[MAXN];
vector<int> S[3];
int gf(int x) { return f[x] == x ? x : f[x] = gf(f[x]); }
inline void Merge(int x, int y) {
  x = gf(x), y = gf(y);
  if (x == y)
    return;
  f[y] = x, s[x] += s[y], cir[y] = 0;
}
inline bool cmp(int x, int y) { return x > y; }
inline int Solve(int cnt, int opt) {
  sort(S[1].begin(), S[1].end(), cmp), sort(S[2].begin(), S[2].end(), cmp);
  for (auto o : S[0])
    cnt -= o;
  int ans = INF;
  if (!opt) {
    if (!S[1].size())
      return INF;
    cnt -= S[1][0], S[1].erase(S[1].begin());
    int cur = S[2].size();
    ll sum = 0;
    for (auto o : S[2])
      sum += o;
    while (cur >= 1 && sum - S[2][cur - 1] >= cnt)
      sum -= S[2][--cur];
    if (sum >= cnt)
      ans = min(ans, 1 + 2 * cur);
    for (int i = 0; i < S[1].size(); i++) {
      for (cnt -= S[1][i]; cur >= 1 && sum - S[2][cur - 1] >= cnt;
           sum -= S[2][--cur])
        ;
      if (sum >= cnt)
        ans = min(ans, 1 + i + 1 + 2 * cur);
    }
    return ans;
  } else {
    if (cnt <= 0)
      return 0;
    int cur = S[2].size();
    ll sum = 0;
    for (auto o : S[2])
      sum += o;
    while (cur >= 1 && sum - S[2][cur - 1] >= cnt)
      sum -= S[2][--cur];
    if (sum >= cnt)
      ans = min(ans, 2 * cur);
    for (int i = 0; i < S[1].size(); i++) {
      for (cnt -= S[1][i]; cur >= 1 && sum - S[2][cur - 1] >= cnt;
           sum -= S[2][--cur])
        ;
      if (sum >= cnt)
        ans = min(ans, i + 1 + 2 * cur);
    }
    return ans;
  }
}
int main() {
  //	freopen("std.in","r",stdin);
  //	freopen("std.out","w",stdout);
  n = read(), m = read();
  Rep(i, 1, n) f[i] = i, s[i] = 1, cir[i] = 1;
  Rep(i, 1, m) {
    a[i] = read(), b[i] = read(), c[i] = read();
    Merge(a[i], b[i]), in[b[i]]++, out[a[i]]++;
  }
  Rep(i, 1, n) if (in[i] != 1 || out[i] != 1) cir[gf(i)] = 0;
  int cnt = 0, ans = 0, lft, key = 0;
  Rep(i, 1, n) if (f[i] == i) cnt += cir[i] && s[i] >= 2;
  lft = cnt;
  Rep(i, 1, m) {
    ans += a[i] != b[i];
    if (c[i] >= 2) {
      if (cir[gf(a[i])])
        if (s[gf(a[i])] == 1)
          S[2].pb(c[i] - 2);
        else
          S[0].pb(c[i] - 1);
      else if (a[i] == b[i])
        S[1].pb(c[i] - 1);
      else
        lft -= c[i] - 1, key = 1;
    }
  }
  if (cnt == 0)
    cout << ans;
  else {
    int tmp = Solve(lft, key);
    if (tmp == INF)
      puts("-1");
    else
      cout << ans + tmp + cnt;
  }
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define db double
#define pint pair<int, int>
#define mk(x, y) make_pair(x, y)
#define pb(x) push_back(x)
#define eb(x, y) emplace_back(x, y)
#define fi first
#define se second
#define Rep(x, y, z) for (int x = y; x <= z; x++)
#define Red(x, y, z) for (int x = y; x >= z; x--)
using namespace std;
const int INF = 1e9, MAXN = 1e5 + 5;
char buf[1 << 12], *pp1 = buf, *pp2 = buf, nc;
int ny;
inline char gc() {
  return pp1 == pp2 &&
                 (pp2 = (pp1 = buf) + fread(buf, 1, 1 << 12, stdin), pp1 == pp2)
             ? EOF
             : *pp1++;
}
// inline char gc(){return getchar();}
inline int read() {
  int x = 0;
  for (ny = 1; nc = gc(), (nc < 48 || nc > 57) && nc != EOF;)
    if (nc == 45)
      ny = -1;
  if (nc < 0)
    return nc;
  for (x = nc - 48; nc = gc(), 47 < nc && nc < 58 && nc != EOF;
       x = (x << 3) + (x << 1) + (nc ^ 48))
    ;
  return x * ny;
}
int n, m, a[MAXN], b[MAXN], c[MAXN], f[MAXN], s[MAXN], cir[MAXN], in[MAXN],
    out[MAXN];
vector<int> S[3];
int gf(int x) { return f[x] == x ? x : f[x] = gf(f[x]); }
inline void Merge(int x, int y) {
  x = gf(x), y = gf(y);
  if (x == y)
    return;
  f[y] = x, s[x] += s[y], cir[y] = 0;
}
inline bool cmp(int x, int y) { return x > y; }
inline int Solve(int cnt, int opt) {
  sort(S[1].begin(), S[1].end(), cmp), sort(S[2].begin(), S[2].end(), cmp);
  for (auto o : S[0])
    cnt -= o;
  int ans = INF;
  if (!opt) {
    if (!S[1].size())
      return INF;
    cnt -= S[1][0], S[1].erase(S[1].begin());
    int cur = S[2].size();
    ll sum = 0;
    for (auto o : S[2])
      sum += o;
    while (cur >= 1 && sum - S[2][cur - 1] >= cnt)
      sum -= S[2][--cur];
    if (sum >= cnt)
      ans = min(ans, 1 + 2 * cur);
    Rep(i, 0, S[1].size() - 1) {
      for (cnt -= S[1][i]; cur >= 1 && sum - S[2][cur - 1] >= cnt;
           sum -= S[2][--cur])
        ;
      ans = min(ans, 1 + i + 1 + 2 * cur);
    }
    return ans;
  } else {
    if (cnt <= 0)
      return 0;
    int cur = S[2].size();
    ll sum = 0;
    for (auto o : S[2])
      sum += o;
    while (cur >= 1 && sum - S[2][cur - 1] >= cnt)
      sum -= S[2][--cur];
    if (sum >= cnt)
      ans = min(ans, 2 * cur);
    Rep(i, 0, S[1].size() - 1) {
      for (cnt -= S[1][i]; cur >= 1 && sum - S[2][cur - 1] >= cnt;
           sum -= S[2][--cur])
        ;
      ans = min(ans, i + 1 + 2 * cur);
    }
    return ans;
  }
}
int main() {
  //	freopen("std.in","r",stdin);
  //	freopen("std.out","w",stdout);
  n = read(), m = read();
  Rep(i, 1, n) f[i] = i, s[i] = 1, cir[i] = 1;
  Rep(i, 1, m) {
    a[i] = read(), b[i] = read(), c[i] = read();
    Merge(a[i], b[i]), in[b[i]]++, out[a[i]]++;
  }
  Rep(i, 1, n) if (in[i] != 1 || out[i] != 1) cir[gf(i)] = 0;
  int cnt = 0, ans = 0, lft, key = 0;
  Rep(i, 1, n) if (f[i] == i) cnt += cir[i] && s[i] >= 2;
  lft = cnt;
  Rep(i, 1, m) {
    ans += a[i] != b[i];
    if (c[i] >= 2) {
      if (cir[gf(a[i])])
        if (s[gf(a[i])] == 1)
          S[2].pb(c[i] - 2);
        else
          S[0].pb(c[i] - 1);
      else if (a[i] == b[i])
        S[1].pb(c[i] - 1);
      else
        lft -= c[i] - 1, key = 1;
    }
  }
  if (cnt == 0)
    cout << ans;
  else {
    int tmp = Solve(lft, key);
    if (tmp == INF)
      puts("-1");
    else
      cout << ans + tmp + cnt;
  }
  return 0;
}