alexjercan/bugnet huggingface dataset:
Problem p01288 (C++) — buggy submission, verdict: Time Limit Exceeded

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

struct UnionFind {
  UnionFind(int n = 0) : par(n, -1) {}
  int root(int x) { return par[x] < 0 ? x : par[x] = root(par[x]); }
  void unite(int x, int y) {
    if ((x = root(x)) == (y = root(y)))
      return;
    par[y] = x;
  }

private:
  vector<int> par;
};

int n, q;
int p[100000];
string t[100000];
int v[100000];
int m[100000];

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

  while (cin >> n >> q, n) {
    memset(m, 0, sizeof(m));
    m[0]++;
    UnionFind uf(n);
    for (int i = 1; i < n; i++) {
      cin >> p[i], p[i]--;
    }
    for (int i = 0; i < q; i++) {
      cin >> t[i] >> v[i], v[i]--;
      if (t[i] == "M")
        m[v[i]]++;
    }
    for (int i = 0; i < n; i++) {
      if (!m[i]) {
        uf.unite(p[i], i);
      }
    }
    long long s = 0;
    for (int i = q - 1; i >= 0; i--) {
      if (t[i] == "M") {
        if (!--m[v[i]]) {
          uf.unite(p[v[i]], v[i]);
        }
      } else
        s += uf.root(v[i]) + 1;
    }
    cout << s << endl;
  }
}

Error: TLE

Fixed submission:

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

struct UnionFind {
  UnionFind(int n = 0) : par(n, -1) {}
  int root(int x) { return par[x] < 0 ? x : par[x] = root(par[x]); }
  void unite(int x, int y) {
    if ((x = root(x)) == (y = root(y)))
      return;
    par[y] = x;
  }

private:
  vector<int> par;
};

int n, q;
int p[100000];
string t[100000];
int v[100000];
int m[100000];

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

  while (cin >> n >> q, n) {
    memset(m, 0, sizeof(m));
    m[0]++;
    UnionFind uf(n);
    for (int i = 1; i < n; i++) {
      cin >> p[i], p[i]--;
    }
    for (int i = 0; i < q; i++) {
      cin >> t[i] >> v[i], v[i]--;
      if (t[i] == "M")
        m[v[i]]++;
    }
    for (int i = 0; i < n; i++) {
      if (!m[i])
        uf.unite(p[i], i);
    }
    int s = 0;
    for (int i = q - 1; i >= 0; i--) {
      for (int j = 0; j < n; j++)
        cout << uf.root(j) << " ";
      cout << endl;
      if (t[i] == "M") {
        if (!--m[v[i]]) {
          uf.unite(p[v[i]], v[i]);
        }
      } else
        s += uf.root(v[i]) + 1;
    }
    cout << s << endl;
  }
}
Problem p01288 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long int ll;
static const int MAX_N = 100000;

int N, Q;
int parent[MAX_N];
bool marked[MAX_N];
vector<pair<char, int>> va;

int par[MAX_N];
int rnk[MAX_N];

int rec(int v) {
  if (v == 0)
    return 0;
  if (rnk[v] != -1)
    return rnk[v];
  return rnk[v] = rec(parent[v]) + 1;
}
void init(int N) {
  for (int i = 0; i < N; i++) {
    par[i] = i;
  }
}
int find(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]);
  }
}
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;
  if (rnk[x] > rnk[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (rnk[x] == rnk[y])
      rnk[x]++;
  }
}
bool same(int x, int y) { return find(x) == find(y); }

int main() {
  for (;;) {
    cin >> N >> Q;
    if (N == 0 && Q == 0)
      break;
    for (int i = 1; i < N; i++) {
      cin >> parent[i];
      parent[i]--;
    }
    fill(marked, marked + N, false);
    fill(rnk, rnk + N, -1);
    for (int i = 0; i < Q; i++) {
      char ch;
      int v;
      cin >> ch >> v;
      v--;
      if (ch == 'M') {
        if (marked[v])
          continue;
        marked[v] = true;
      }
      va.push_back(make_pair(ch, v));
    }
    reverse(va.begin(), va.end());
    init(N);
    for (int i = 0; i < N; i++)
      rnk[i] = rec(i);
    for (int i = 0; i < N; i++) {
      if (!marked[i])
        unite(parent[i], i);
    }
    ll res = 0LL;
    for (int i = 0; i < va.size(); i++) {
      if (va[i].first == 'M') {
        unite(parent[va[i].second], va[i].second);
      } else {
        res += (ll)(find(va[i].second) + 1);
      }
    }
    cout << res << endl;
    va.clear();
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
typedef long long int ll;
static const int MAX_N = 100000;
static const int MAX_Q = 100000;

int N, Q;
vector<int> G[MAX_N];

int par[MAX_N];
int rnk[MAX_N];

void init(int N) {
  for (int i = 0; i < N; i++) {
    par[i] = i;
    rnk[i] = 0;
  }
}
int find(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]);
  }
}
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;
  if (rnk[x] < rnk[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (rnk[x] == rnk[y])
      rnk[x]++;
  }
}
bool same(int x, int y) { return find(x) == find(y); }

void dfs(int u, int v) {
  unite(u, v);
  for (int i = 0; i < G[v].size(); i++) {
    dfs(u, G[v][i]);
  }
  return;
}

int main() {
  for (;;) {
    cin >> N >> Q;
    if (N == 0 && Q == 0)
      break;
    init(N);
    for (int i = 0; i < N; i++)
      G[i].clear();
    for (int i = 1; i < N; i++) {
      int p;
      cin >> p;
      p--;
      G[p].push_back(i);
    }
    ll res = 0LL;
    for (int i = 0; i < Q; i++) {
      char ch;
      int v;
      cin >> ch >> v;
      v--;
      if (ch == 'M')
        dfs(v, v);
      else
        res += (find(v) == v ? 1 : find(v) + 1);
    }
    cout << res << endl;
  }
  return 0;
}
Problem p01288 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
using namespace std;
#define MAX_N 100005
int par[MAX_N];
int find(int x) {
  if (x == par[x])
    return x;
  else
    return find(par[x]);
}

int main() {
  int n, q;
  while (true) {
    scanf("%d %d", &n, &q);
    if (n == 0 && q == 0)
      break;
    int x;
    long long sum = 0;
    for (int i = 2; i <= n; i++) {

      scanf("%d", &x);
      par[i] = x;
    }
    par[1] = 1;
    // flag[1] = true;

    getchar();
    char c;
    int p;
    while (q--) {
      scanf("%c%d%*c", &c, &p);
      if (c == 'Q') {
        sum += find(p);
      } else {
        par[p] = p;
      }
    }
    printf("%lld\\n", sum);
  }
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;
#define MAX_N 100005
int par[MAX_N];
bool flag[MAX_N];
int ran[MAX_N];
void init(int n) {
  for (int i = 2; i <= n; i++) {
    flag[i] = false;
    ran[i] = 0;
  }
}
int find(int x) {
  if (flag[x])
    return x;
  else
    find(par[x]);
}

int main() {
  int n, q;
  while (true) {
    scanf("%d %d", &n, &q);
    if (n == 0 && q == 0)
      break;
    int x;
    long long sum = 0;
    for (int i = 2; i <= n; i++) {

      scanf("%d", &x);
      par[i] = x;
    }
    //		par[1]= 1;
    flag[1] = true;
    init(n);

    getchar();
    char c;
    int p;
    while (q--) {
      scanf("%c%d%*c", &c, &p);
      if (c == 'Q') {
        sum += find(par[p]);
      } else {
        flag[p] = true;
      }
    }
    printf("%lld\\n", sum);
  }
}
Problem p01288 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
#define min(a, b) a < b ? a : b
using namespace std;

const int INF = 0x7f7f7f7f;
const int MAXN = 1e5 + 111;

int p[MAXN];
int qt[MAXN], qv[MAXN], mark[MAXN];
int t;

int find(int x) {
  return mark[x] < t
             ? x
             : p[x] = find(
                   p[x]); // ?°?????????´?????¨??\\??¢???????????????????¢????
}

int main() {
  int n, q, i;
  while (~scanf("%d%d", &n, &q) && (n | q)) {
    for (i = 2; i <= n; ++i) {
      scanf("%d", p + i);
      mark[i] = INF;
    }

    int cnt = 0, x;
    char op[2];
    for (i = 1; i <= q; ++i) {
      scanf("%s%d", op, &x);
      if (op[0] == 'M')
        mark[x] = min(mark[x], i); // ??°?????????????????¶??´
      else {
        qt[cnt] = i;
        qv[cnt++] = x;
      }
    }

    long long ans = 0;
    while (cnt--) {
      t = qt[cnt]; // ??\\??¢???????????¶??´
      ans += find(qv[cnt]);
    }
    printf("%lld\\n", ans);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <stdio.h>
#define maxN 100000 + 16

using namespace std;

int par[maxN], mark[maxN];
int qx[maxN], qt[maxN], tt;

int find(int x) { return mark[x] < tt ? x : par[x] = find(par[x]); }

int main() {
  int n, t, i, x, nn;
  char c;
  long long sum;
  while (scanf("%d%d", &n, &t) && (n || t)) {
    par[1] = 1;
    mark[1] = 0;
    for (i = 2; i <= n; i++) {
      scanf("%d", &x);
      par[i] = x;
      mark[i] = maxN;
    }
    getchar();
    for (i = nn = 1; i <= t; i++) {
      scanf("%c%d%*c", &c, &x);
      if (c == 'M')
        mark[x] = i;
      else {
        qx[nn] = x;
        qt[nn++] = i;
      }
    }
    for (i = nn - 1, sum = 0; i >= 1; i--) {
      tt = qt[i];
      sum += find(qx[i]);
    }
    printf("%lld\\n", sum);
  }
  return 0;
}
Problem p01288 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

const int MAX = 100005;
typedef pair<char, int> P;

class UnionFind {
public:
  int par[MAX];
  int rank[MAX];
  int mark[MAX];
  void init(int n) {
    for (int i = 1; i <= n; i++) {
      par[i] = i;
      rank[i] = mark[i] = 0;
    }
  }

  int find(int x) {
    if (mark[x] > 0)
      return x;
    else
      return par[x] = find(par[x]);
  }
};

int N, M;
UnionFind T;
vector<P> V;

void init() {
  T.init(N);
  T.mark[1] = MAX;
  V.clear();
}

void input() {
  for (int i = 0; i < N - 1; i++) {
    int in;
    cin >> in;
    T.par[i + 2] = in;
  }

  for (int i = 0; i < M; i++) {
    char c;
    int in;
    cin >> c >> in;
    if (c == 'M')
      T.mark[in]++;
    V.push_back(P(c, in));
  }
}

void solve() {
  long long ans = 0;
  for (int i = V.size() - 1; i >= 0; i--) {
    if (V[i].first == 'M')
      T.mark[V[i].second]--;
    else
      ans += T.find(V[i].second);
  }
  cout << ans << endl;
}
int main() {
  while (cin >> N >> M && N + M) {
    init();
    input();
    solve();
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

const int MAX = 10005;
class UnionFind {
public:
  int par[MAX];
  int rank[MAX];
  int mark[MAX];
  void init(int n) {
    for (int i = 0; i < n; i++) {
      par[i] = i;
      rank[i] = 0;
    }

    for (int i = 0; i < MAX; i++)
      mark[i] = 0;
  }

  int find(int x) {
    if (mark[x] > 0) {
      mark[x]--;
      return x;
    } else
      return par[x] = find(par[x]);
  }
};

int N, M;
UnionFind T;
vector<int> V;

void init() {
  T.init(N);
  T.mark[1] = MAX + 5;
  V.clear();
}

void input() {
  for (int i = 0; i < N - 1; i++) {
    int in;
    cin >> in;
    T.par[i + 2] = in;
  }

  for (int i = 0; i < M; i++) {
    char c;
    int in;
    cin >> c >> in;
    if (c == 'M')
      T.mark[in]++;
    else
      V.push_back(in);
  }
}

void solve() {
  int ans = 0;
  for (int i = V.size() - 1; i >= 0; i--) {
    ans += T.find(V[i]);
  }
  cout << ans << endl;
}
int main() {
  while (cin >> N >> M && N + M) {
    init();
    input();
    solve();
  }
  return 0;
}
Problem p01288 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
struct node {
  int parent;
  bool marked;
};
node V[100000];
int query(int v) {
  if (V[v].marked)
    return v + 1;
  else
    return query(V[v].parent);
}
int main() {
  int n, q;
  while (cin >> n >> q, n) {
    V[0].marked = true;
    for (int i = 1; i < n; ++i) {
      int p;
      cin >> p;
      V[i].parent = --p;
      V[i].marked = false;
    }
    long long sum = 0;
    for (int i = 0; i < q; ++i) {
      char q;
      int v;
      cin >> q >> v;
      --v;
      if (q == 'M')
        V[v].marked = true;
      else {
        sum += query(v);
      }
    }
    cout << sum << endl;
  }
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
struct node {
  int parent;
  bool marked;
};
node V[10000];
int query(int v) {
  if (V[v].marked)
    return v + 1;
  else
    return query(V[v].parent);
}
int main() {
  int n, q;
  while (cin >> n >> q, n) {
    V[0].marked = true;
    for (int i = 1; i < n; ++i) {
      int p;
      cin >> p;
      V[i].parent = --p;
      V[i].marked = false;
    }
    int sum = 0;
    for (int i = 0; i < q; ++i) {
      char q;
      int v;
      cin >> q >> v;
      --v;
      if (q == 'M')
        V[v].marked = true;
      else {
        sum += query(v);
      }
    }
    cout << sum << endl;
  }
}
Problem p01288 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

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

using namespace std;

bool marked[100000];
int prt[100000];

int gm(int i) {
  if (marked[i])
    return i;
  return prt[i] = gm(prt[i]);
}

int main() {
  while (1) {
    int n, q;
    cin >> n >> q;
    if (!n)
      break;
    REP(i, n) marked[i] = false;
    marked[0] = true;
    REP(i, n - 1) {
      cin >> prt[i + 1];
      --prt[i + 1];
    }
    vector<tuple<int, int>> ql;
    REP(i, q) {
      string c;
      int v;
      cin >> c >> v;
      --v;
      if (c == "M") {
        if (marked[v])
          continue;
        marked[v] = true;
      }
      ql.emplace_back(c == "Q" ? 1 : 0, v);
    }
    reverse(begin(ql), end(ql));
    int64_t sum = 0;
    REP(i, ql.size()) {
      int c, v;
      tie(c, v) = ql[i];
      if (c)
        sum += gm(v) + 1;
      else
        marked[v] = false;
    }
    cout << sum << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

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

using namespace std;

bool marked[100000];
int prt[100000];

int gm(int i) {
  if (marked[i])
    return i;
  return prt[i] = gm(prt[i]);
}

int main() {
  while (1) {
    int n, q;
    cin >> n >> q;
    if (!n)
      break;
    REP(i, n) marked[i] = false;
    marked[0] = true;
    REP(i, n - 1) {
      cin >> prt[i + 1];
      --prt[i + 1];
    }
    vector<tuple<int, int>> ql;
    REP(i, q) {
      string c;
      int v;
      cin >> c >> v;
      --v;
      if (c == "M") {
        if (marked[v])
          continue;
        marked[v] = true;
      }
      ql.emplace_back(c == "Q", v);
    }
    reverse(begin(ql), end(ql));
    int sum = 0;
    REP(i, q) {
      int c, v;
      tie(c, v) = ql[i];
      if (c) // Q
        sum += gm(v) + 1;
      else
        marked[v] = false;
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p01288 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <cstring>
#include <iostream>
const int MAX_N = 100000 + 16;
int a[MAX_N * 2];
bool mark[MAX_N * 2];
int N, Q;
int main() {
  // freopen("1.txt","r",stdin);
  while (scanf("%d%d", &N, &Q) != EOF) {
    if (N == 0 && Q == 0)
      break;
    memset(mark, false, sizeof(bool) * (N + 10));
    a[1] = 0;
    mark[1] = 1;
    for (int i = 2; i <= N; i++) {
      scanf("%d", &a[i]);
    }
    long ans = 0;
    while (Q--) {
      char c[2];
      int x;
      scanf("%s %d", c, &x);
      if (c[0] == 'Q') {
        while (!mark[x]) {
          x = a[x];
        }
        ans += x;
      } else {
        mark[x] = true;
      }
    }
    printf("%ld\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
const int MAX_N = 100000 + 1;
int a[MAX_N];
int N, Q;
int main() {
  // freopen("1.txt","r",stdin);
  while (scanf("%d%d", &N, &Q) != EOF) {
    if (N == 0 && Q == 0)
      break;
    a[1] = 1;
    for (int i = 2; i <= N; i++) {
      scanf("%d", &a[i]);
    }
    long long ans = 0;
    while (N--) {
      char c[2];
      int x;
      scanf("%s%d", c, &x);
      if (c[0] == 'Q') {
        while (x != a[x]) {
          x = a[x];
        }
        ans += x;
      } else {
        a[x] = x;
      }
    }
    printf("%lld\\n", ans);
  }
  return 0;
}
Problem p01294 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int N, D;
vector<int> C[100];
int num[100];
int power[100];

bool isValid() {
  int mini, maxi;
  mini = maxi = power[0];
  for (int i = 1; i < N; ++i) {
    mini = min(mini, power[i]);
    maxi = max(maxi, power[i]);
  }
  return maxi - mini <= D;
}

bool iscomplete() {
  for (int i = 0; i < N; ++i) {
    if (num[i] != C[i].size())
      return false;
  }
  return true;
}

bool solve() {
  fill(num, num + N, 0);
  fill(power, power + N, 0);
  while (!iscomplete()) {
    for (int i = 0; i <= N; ++i) {
      if (i == N)
        return false;
      if (num[i] == C[i].size())
        continue;
      power[i] += C[i][num[i]++];
      if (isValid())
        break;
      power[i] -= C[i][--num[i]];
    }
  }
  return true;
}

int main() {
  while (cin >> N >> D && (N | D)) {
    for (int i = 0; i < 100; ++i)
      C[i].clear();
    for (int i = 0; i < N; ++i) {
      int m;
      cin >> m;
      for (int j = 0; j < m; ++j) {
        int c;
        cin >> c;
        C[i].push_back(c);
      }
    }
    cout << (solve() ? "Yes" : "No") << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;
int N, D;

bool isValid(int a[]) {
  int m, M;
  m = M = a[0];
  for (int i = 1; i < N; ++i) {
    m = min(m, a[i]);
    M = max(M, a[i]);
  }
  return M - m <= D;
}

bool solve(vector<int> C[]) {
  int a[100], p[100], cnt;
  for (int i = 0; i < N; ++i) {
    a[i] = 0;
    for (int j = 0; j < C[i].size(); ++j)
      a[i] += C[i][j];
  }
  fill(p, p + N, 0);
  cnt = 0;
  while (cnt != N) {
    bool changed = false;
    for (int i = 0; i < N; ++i) {
      if (p[i] < 0)
        continue;
      a[i] -= C[i][p[i]];
      if (!isValid(a)) {
        a[i] += C[i][p[i]];
        continue;
      }
      ++p[i];
      changed = true;
      if (p[i] == C[i].size()) {
        p[i] = -1;
        ++cnt;
      }
      break;
    }
    if (!changed)
      return false;
  }
  return true;
}

int main() {
  while (cin >> N >> D && (N || D)) {
    vector<int> C[100];
    for (int i = 0; i < N; ++i) {
      int m;
      cin >> m;
      for (int j = 0; j < m; ++j) {
        int c;
        cin >> c;
        C[i].push_back(c);
      }
      reverse(C[i].begin(), C[i].end());
    }
    cout << (solve(C) ? "Yes" : "No") << endl;
  }
  return 0;
}
Problem p01294 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  int n, d;
  while (cin >> n >> d && (n || d)) {
    vector<int> si[100];
    int max_potential = -1;
    for (int i = 0; i < n; i++) {
      int m;
      cin >> m;
      for (int j = 0; j < m; j++) {
        int cap;
        cin >> cap;
        if (si[i].empty())
          si[i].push_back(cap);
        else
          si[i].push_back(cap + si[i][si[i].size() - 1]);
      }
      if (!si[i].empty() && max_potential < si[i][si[i].size() - 1])
        max_potential = si[i][si[i].size() - 1];
    }
    bool update = true;
    while (update) {
      update = false;
      for (int i = 0; i < n; i++) {
        max_potential = 0;
        if (si[i].empty())
          continue;
        for (int j = 0; j < n; j++) {
          if (j != i && !si[j].empty())
            max_potential = max(max_potential, si[j][si[j].size() - 1]);
        }
        while (!si[i].empty() &&
               ((si[i].size() == 1 && d >= max_potential) ||
                (si[i].size() > 1 &&
                 si[i][si[i].size() - 2] + d >= max_potential))) {
          int c = si[i][si[i].size() - 1];
          si[i].pop_back();
          update = true;
        }
      }
    }
    bool f = true;
    for (int i = 0; i < n; i++) {
      if (!si[i].empty())
        f = false;
    }
    if (f)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  int n, d;
  while (cin >> n >> d && (n || d)) {
    vector<int> si[100];
    int max_potential = -1;
    for (int i = 0; i < n; i++) {
      int m;
      cin >> m;
      for (int j = 0; j < m; j++) {
        int cap;
        cin >> cap;
        if (si[i].empty())
          si[i].push_back(cap);
        else
          si[i].push_back(cap + si[i][si[i].size() - 1]);
      }
      if (max_potential < si[i][si[i].size() - 1])
        max_potential = si[i][si[i].size() - 1];
    }
    bool update = true;
    while (update) {
      update = false;
      for (int i = 0; i < n; i++) {
        max_potential = 0;
        if (si[i].empty())
          continue;
        for (int j = 0; j < n; j++) {
          if (j != i && !si[j].empty())
            max_potential = max(max_potential, si[j][si[j].size() - 1]);
        }
        while (!si[i].empty() &&
               ((si[i].size() == 1 && d >= max_potential) ||
                (si[i].size() > 1 &&
                 si[i][si[i].size() - 2] + d >= max_potential))) {
          int c = si[i][si[i].size() - 1];
          si[i].pop_back();
          update = true;
        }
      }
    }
    bool f = true;
    for (int i = 0; i < n; i++) {
      if (!si[i].empty())
        f = false;
    }
    if (f)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}
Problem p01294 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define EPS 1e-9
#define INF MOD
#define MOD 1000000007LL
#define fir first
#define iss istringstream
#define sst stringstream
#define ite iterator
#define ll long long
#define mp make_pair
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < n; i++)
#define pi pair<int, int>
#define pb push_back
#define sec second
#define sh(i) (1LL << i)
#define sz size()
#define vi vector<int>
#define vc vector
#define vl vector<ll>
#define vs vector<string>

int n, d, m[110], s[110], x;

int main() {
  while (cin >> n >> d && n) {
    vi c[110];
    fill(s, s + n, 0);
    rep(i, n) {
      cin >> m[i];
      rep(j, m[i]) cin >> x, c[i].pb(x), s[i] += x;
    }
    int f;
    while (1) {
      f = 0;
      rep(i, n) if (m[i]) {
        s[i] -= c[i][m[i] - 1];
        if (*max_element(s, s + n) - *min_element(s, s + n) <= d) {
          c[i].erase(c[i].end() - 1), m[i]--, f = 1;
        } else
          s[i] += c[i][m[i] - 1];
      }
      if (!f || !accumulate(m, m + n, 0))
        break;
    }
    cout << (f ? "Yes" : "No") << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define EPS 1e-9
#define INF MOD
#define MOD 1000000007LL
#define fir first
#define iss istringstream
#define sst stringstream
#define ite iterator
#define ll long long
#define mp make_pair
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < n; i++)
#define pi pair<int, int>
#define pb push_back
#define sec second
#define sh(i) (1LL << i)
#define sz size()
#define vi vector<int>
#define vc vector
#define vl vector<ll>
#define vs vector<string>

int n, d, m[110], s[110], x;

int main() {
  while (cin >> n >> d && n) {
    vi c[110];
    fill(s, s + n, 0);
    rep(i, n) {
      cin >> m[i];
      rep(j, m[i]) cin >> x, c[i].pb(x), s[i] += x;
    }
    int f;
    while (1) {
      f = 0;
      rep(i, n) {
        s[i] -= c[i][m[i] - 1];
        if (*max_element(s, s + n) - *min_element(s, s + n) <= d) {
          c[i].erase(c[i].end() - 1), m[i]--, f = 1;
        } else
          s[i] += c[i][m[i] - 1];
      }
      if (!f || !accumulate(m, m + n, 0))
        break;
    }
    cout << (f ? "Yes" : "No") << endl;
  }
}