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

Buggy submission (status: Runtime Error):
#include <iostream>
#define REP(i, a, n) for (int i = (a); i < (n); i++)
using namespace std;
typedef long long ll;

struct sector {
  ll l, r, f;
};

ll N, I, S, P;
char C;
sector a[1000];
int k;

void write() {
  cin >> I >> S;
  REP(i, 0, k) if (a[i].f == -1 && S > 0) {
    if (S >= a[i].r - a[i].l) {
      a[i].f = I;
      S -= a[i].r - a[i].l;
    } else {
      for (int j = k; j > i; j--) {
        a[j + 1].l = a[j].l;
        a[j + 1].r = a[j].r;
        a[j + 1].f = a[j].f;
      }
      a[i + 1].r = a[i].r;
      a[i + 1].f = -1;
      a[i].f = I;
      a[i].r = a[i].l + S;
      a[i + 1].l = a[i].r;
      S = 0;
      k++;
      break;
    }
  }
  if (S > 0) {
    int l = k > 0 ? a[k - 1].r : 0;
    a[k++] = (sector){l, l + S, I};
  }
}

void destroy() {
  cin >> I;
  REP(i, 0, k) if (a[i].f == I) a[i].f = -1;
}

ll read() {
  cin >> P;
  REP(i, 0, k) if (a[i].l <= P && P < a[i].r) return a[i].f;
  return -1;
}

int main(void) {
  while (cin >> N, N) {
    k = 0;
    REP(i, 0, N) {
      cin >> C;
      if (C == 'W')
        write();
      if (C == 'D')
        destroy();
      if (C == 'R')
        cout << read() << endl;

      // cout << i << ": " << C << " " << k << endl;
      // REP(i, 0, k) {
      //   cout << a[i].l << " - " << a[i].r << ": " << a[i].f << endl;
      // }
      // cout << endl;
    }
    cout << endl;
  }
}

Fixed submission:
#include <iostream>
#define REP(i, a, n) for (int i = (a); i < (n); i++)
using namespace std;
typedef long long ll;

struct sector {
  ll l, r, f;
};

ll N, I, S, P;
char C;
sector a[20000];
int k;

void write() {
  cin >> I >> S;
  REP(i, 0, k) if (a[i].f == -1 && S > 0) {
    if (S >= a[i].r - a[i].l) {
      a[i].f = I;
      S -= a[i].r - a[i].l;
    } else {
      for (int j = k; j > i; j--) {
        a[j + 1].l = a[j].l;
        a[j + 1].r = a[j].r;
        a[j + 1].f = a[j].f;
      }
      a[i + 1].r = a[i].r;
      a[i + 1].f = -1;
      a[i].f = I;
      a[i].r = a[i].l + S;
      a[i + 1].l = a[i].r;
      S = 0;
      k++;
      break;
    }
  }
  if (S > 0) {
    int l = k > 0 ? a[k - 1].r : 0;
    a[k++] = (sector){l, l + S, I};
  }
}

void destroy() {
  cin >> I;
  REP(i, 0, k) if (a[i].f == I) a[i].f = -1;
}

ll read() {
  cin >> P;
  REP(i, 0, k) if (a[i].l <= P && P < a[i].r) return a[i].f;
  return -1;
}

int main(void) {
  while (cin >> N, N) {
    k = 0;
    REP(i, 0, N) {
      cin >> C;
      if (C == 'W')
        write();
      if (C == 'D')
        destroy();
      if (C == 'R')
        cout << read() << endl;

      // cout << i << ": " << C << " " << k << endl;
      // REP(i, 0, k) {
      //   cout << a[i].l << " - " << a[i].r << ": " << a[i].f << endl;
      // }
      // cout << endl;
    }
    cout << endl;
  }
}
Problem p01273 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
using namespace std;

int main() {
  int n, m;
  while (cin >> n >> m, n || m) {
    bool infect[n + 1];
    rep(i, n + 1) infect[i] = false;
    infect[1] = true;
    long long ans = 1;
    long long t[m];
    int s[m], d[m];
    rep(i, m) cin >> t[i] >> s[i] >> d[i];
    for (int i = m; i >= 0; i--) {
      for (int j = 0; j < i - 1; j++) {
        if (t[j] > t[j + 1]) {
          long long alt = t[j];
          t[j] = t[j + 1];
          t[j + 1] = alt;
          alt = s[j];
          s[j] = s[j + 1];
          s[j + 1] = alt;
          alt = d[j];
          d[j] = d[j + 1];
          d[j + 1] = alt;
        }
      }
    }
    rep(i, n) {
      if (infect[s[i]]) {
        if (!infect[d[i]])
          ans++;
        infect[d[i]] = true;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
using namespace std;

int main() {
  int n, m;
  while (cin >> n >> m, n || m) {
    bool infect[n + 1];
    rep(i, n + 1) infect[i] = false;
    infect[1] = true;
    long long ans = 1;
    long long t[m];
    int s[m], d[m];
    rep(i, m) cin >> t[i] >> s[i] >> d[i];
    for (int i = m; i >= 0; i--) {
      for (int j = 0; j < i - 1; j++) {
        if (t[j] > t[j + 1]) {
          long long alt = t[j];
          t[j] = t[j + 1];
          t[j + 1] = alt;
          alt = s[j];
          s[j] = s[j + 1];
          s[j + 1] = alt;
          alt = d[j];
          d[j] = d[j + 1];
          d[j + 1] = alt;
        }
      }
    }
    rep(i, m) {
      if (infect[s[i]]) {
        if (!infect[d[i]])
          ans++;
        infect[d[i]] = true;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01273 (C++)

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

using namespace std;

struct Packet {
  int t, s, d;

  Packet() = default;
  Packet(int t, int s, int d) : t(t), s(s), d(d) {}

  bool operator<(const Packet &p) const { return t < p.t; }
};

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

    if (N + M == 0) {
      break;
    }

    bool infected[20001] = {true};

    vector<Packet> packet(M);
    for (int i = 0; i < M; i++) {
      int t, s, d;
      cin >> t >> s >> d;
      packet[i] = Packet(t, s - 1, d - 1);
    }
    sort(packet.begin(), packet.end());

    for (int i = 0; i < M; i++) {
      if (infected[packet[i].s]) {
        infected[packet[i].t] = true;
      }
    }

    int ans = 0;
    for (int i = 0; i < N; i++) {
      if (infected[i]) {
        ans++;
      }
    }

    cout << ans << endl;
  }

  return 0;
}

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

using namespace std;

struct Packet {
  int t, s, d;

  Packet() = default;
  Packet(int t, int s, int d) : t(t), s(s), d(d) {}

  bool operator<(const Packet &p) const { return t < p.t; }
};

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

    if (N + M == 0) {
      break;
    }

    bool infected[20001] = {true};

    vector<Packet> packet(M);
    for (int i = 0; i < M; i++) {
      int t, s, d;
      cin >> t >> s >> d;
      packet[i] = Packet(t, s - 1, d - 1);
    }
    sort(packet.begin(), packet.end());

    for (int i = 0; i < M; i++) {
      if (infected[packet[i].s]) {
        infected[packet[i].d] = true;
      }
    }

    int ans = 0;
    for (int i = 0; i < N; i++) {
      if (infected[i]) {
        ans++;
      }
    }

    cout << ans << endl;
  }

  return 0;
}
Problem p01274 (C++)

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

int HP[100], M;
long long dp1[100001], dp2[100001];

int solve(int N) {
  memset(dp1, 0, sizeof(dp1));
  memset(dp2, 0, sizeof(dp2));

  for (int i = 0; i < N; i++) {
    cin >> HP[i];
  }
  cin >> M;

  bool end = false;
  while (M--) {
    string Name, Target;
    int MP, Damage;
    cin >> Name >> MP >> Target >> Damage;
    if (MP == 0) {
      if (Damage > 0)
        end = true;
      continue;
    }
    long long *dp = Target.size() == 3 ? dp1 : dp2;
    for (int i = MP; i <= 100000; i++) {
      dp[i] = max(dp[i], dp[i - MP] + Damage);
    }
  }
  if (end)
    return (0);

  int best = 1 << 30, pos = *max_element(HP, HP + N);
  for (int i = 0; i <= 100000 && HP[i] <= pos; i++) {
    int ret = 0;
    for (int j = 0; j < N; j++) {
      ret += lower_bound(dp2, dp2 + 100001, HP[j] - dp1[i]) - dp2;
    }
    best = min(best, i + ret);
  }
  return (best);
}

int main() {
  int N;
  while (cin >> N, N) {
    cout << solve(N) << endl;
  }
}

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

int HP[100], M;
long long dp1[100001], dp2[100001];

int solve(int N) {
  memset(dp1, 0, sizeof(dp1));
  memset(dp2, 0, sizeof(dp2));

  for (int i = 0; i < N; i++) {
    cin >> HP[i];
  }
  cin >> M;

  bool end = false;
  while (M--) {
    string Name, Target;
    int MP, Damage;
    cin >> Name >> MP >> Target >> Damage;
    if (MP == 0) {
      if (Damage > 0)
        end = true;
      continue;
    }
    long long *dp = Target.size() == 3 ? dp1 : dp2;
    for (int i = MP; i <= 100000; i++) {
      dp[i] = max(dp[i], dp[i - MP] + Damage);
    }
  }
  if (end)
    return (0);

  int best = 1 << 30;
  for (int i = 0; i <= 100000; i++) {
    int ret = 0;
    for (int j = 0; j < N; j++) {
      ret += lower_bound(dp2, dp2 + 100001, HP[j] - dp1[i]) - dp2;
    }
    best = min(best, i + ret);
  }
  return (best);
}

int main() {
  int N;
  while (cin >> N, N) {
    cout << solve(N) << endl;
  }
}
Problem p01274 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

int multi[100010], single[100010], hp[110];

int main() {
  int N;
  while (cin >> N && N) {
    for (int i = 0; i < 100010; i++) {
      multi[i] = 1e9;
      single[i] = 1e9;
    }
    multi[0] = 0;
    single[0] = 0;
    for (int i = 0; i < N; i++)
      cin >> hp[i];
    int M;
    cin >> M;
    for (int i = 0; i < M; i++) {
      string name;
      int mp;
      string command;
      int dmp;
      cin >> name >> mp >> command >> dmp;
      if (command == "Single") {
        for (int j = 0; j <= 100000; j++) {
          int s = min(100000, j + dmp);
          single[s] = min(single[s], single[j] + mp);
        }
      } else {
        for (int j = 0; j <= 100000; j++) {
          int s = min(100000, j + dmp);
          multi[s] = min(multi[s], multi[j] + mp);
        }
      }
    }
    for (int i = 100000; i >= 0; i--) {
      multi[i] = min(multi[i], multi[i + 1]);
      single[i] = min(single[i], single[i + 1]);
    }
    long long ans = 1e9;
    for (int i = 0; i <= 100000; i++) {
      long long u = multi[i];
      for (int j = 0; j < N; j++) {
        int v = min(0, hp[j] - i);
        u += single[v];
      }
      ans = min(u, ans);
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

int multi[100010], single[100010], hp[110];

int main() {
  int N;
  while (cin >> N && N) {
    for (int i = 0; i < 100010; i++) {
      multi[i] = 1e9;
      single[i] = 1e9;
    }
    multi[0] = 0;
    single[0] = 0;
    for (int i = 0; i < N; i++)
      cin >> hp[i];
    int M;
    cin >> M;
    for (int i = 0; i < M; i++) {
      string name;
      int mp;
      string command;
      int dmp;
      cin >> name >> mp >> command >> dmp;
      if (command == "Single") {
        for (int j = 0; j <= 100000; j++) {
          int s = min(100000, j + dmp);
          single[s] = min(single[s], single[j] + mp);
        }
      } else {
        for (int j = 0; j <= 100000; j++) {
          int s = min(100000, j + dmp);
          multi[s] = min(multi[s], multi[j] + mp);
        }
      }
    }
    for (int i = 100000; i >= 0; i--) {
      multi[i] = min(multi[i], multi[i + 1]);
      single[i] = min(single[i], single[i + 1]);
    }
    long long ans = 1e9;
    for (int i = 0; i <= 100000; i++) {
      long long u = multi[i];
      for (int j = 0; j < N; j++) {
        int v = max(0, hp[j] - i);
        u += single[v];
      }
      ans = min(u, ans);
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01275 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define INF INT_MAX / 2

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef string Node;
typedef pair<Node, int> pni;

struct Comp {
  bool operator()(pni a, pni b) { return a.second > b.second; }
};

string rot(string a, int from, int to, int n) {
  rep(i, from, to + 1) {
    int j = a[i] - '0';
    j = (j + n + 10) % 10;
    a[i] = '0' + j;
  }

  return a;
}

int solve(int k) {
  string start, end;
  cin >> start >> end;

  priority_queue<pni, vector<pni>, Comp> que;
  map<Node, int> dist;
  set<Node> done;

  que.push(pni(start, 0));
  while (!que.empty()) {
    Node n = que.top().first;
    int c = que.top().second;
    que.pop();

    if (done.find(n) != done.end())
      continue;
    done.insert(n);

    if (n == end)
      return c;

    int from = 0;
    while (n[from] == end[from])
      from++;

    rep(i, from, k) rep(j, i, k) rep(jj, -9, 10) {
      if (jj == 0)
        continue;
      string nnode = rot(n, i, j, jj);
      int nc = c + 1;
      if (dist.find(nnode) == dist.end())
        dist[nnode] = INF;
      if (nc < dist[nnode]) {
        dist[nnode] = nc;
        que.push(pni(nnode, nc));
      }
    }
  }
}

int main() {
  while (1) {
    int k;
    cin >> k;
    if (k == 0)
      return 0;
    cout << solve(k) << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define INF INT_MAX / 2

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef string Node;
typedef pair<Node, int> pni;

struct Comp {
  bool operator()(pni a, pni b) { return a.second > b.second; }
};

string rot(string a, int from, int to, int n) {
  rep(i, from, to + 1) {
    int j = a[i] - '0';
    j = (j + n + 10) % 10;
    a[i] = '0' + j;
  }

  return a;
}

int solve(int k) {
  string start, end;
  cin >> start >> end;

  priority_queue<pni, vector<pni>, Comp> que;
  map<Node, int> dist;
  set<Node> done;

  que.push(pni(start, 0));
  while (!que.empty()) {
    Node n = que.top().first;
    int c = que.top().second;
    que.pop();

    if (done.find(n) != done.end())
      continue;
    done.insert(n);

    if (n == end)
      return c;

    int from = 0;
    while (n[from] == end[from])
      from++;
    int delta = (end[from] - '0') - (n[from] - '0');
    rep(to, from, k) {
      Node nnode = rot(n, from, to, delta);
      int nc = c + 1;
      if (dist.find(nnode) == dist.end())
        dist[nnode] = INF;
      if (nc < dist[nnode]) {
        dist[nnode] = nc;
        que.push(pni(nnode, nc));
      }
    }
  }
}

int main() {
  while (1) {
    int k;
    cin >> k;
    if (k == 0)
      return 0;
    cout << solve(k) << endl;
  }
}
Problem p01275 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;

int solve(vector<int> D, vector<int> imos, int i, int d) {
  int n = D.size();
  if (i == n)
    return 0;
  d += imos[i];
  D[i] += 10 - d;
  D[i] %= 10;
  int ret = 1e9;
  if (D[i]) {
    for (int j = i + 1; j <= n; ++j) {
      imos[j] -= D[i];
      ret = min(ret, 1 + solve(D, imos, i + 1, (d + D[i]) % 10));
      imos[j] += D[i];
    }
  } else {
    ret = min(ret, solve(D, imos, i + 1, d));
  }
  return ret;
}

int main() {
  int N;
  while (cin >> N, N) {
    string S, T;
    cin >> S >> T;
    vector<int> D(N);
    for (int i = 0; i < N; ++i) {
      D[i] = (T[i] - S[i] + 10) % 10;
    }
    cout << solve(D, vector<int>(N, 0), 0, 0) << endl;
  }
  return 0;
}


Fixed submission:
#include <iostream>
#include <vector>
using namespace std;

int solve(vector<int> D, vector<int> imos, int i, int d) {
  int n = D.size();
  if (i == n)
    return 0;
  d += imos[i];
  D[i] += 10 - d;
  D[i] %= 10;
  int ret = 1e9;
  if (D[i]) {
    for (int j = i + 1; j <= n; ++j) {
      imos[j] -= D[i];
      ret = min(ret, 1 + solve(D, imos, i + 1, (d + D[i]) % 10));
      imos[j] += D[i];
    }
  } else {
    ret = min(ret, solve(D, imos, i + 1, d));
  }
  return ret;
}

int main() {
  int N;
  while (cin >> N, N) {
    string S, T;
    cin >> S >> T;
    vector<int> D(N);
    for (int i = 0; i < N; ++i) {
      D[i] = (T[i] - S[i] + 10) % 10;
    }
    cout << solve(D, vector<int>(N + 1, 0), 0, 0) << endl;
  }
  return 0;
}
Problem p01275 (C++)

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

using namespace std;

int N;

int solve(string &s, string &t) {
  queue<string> Q;
  map<string, int> dist;
  Q.push(s);
  dist[s] = 0;
  while (!Q.empty()) {
    string v = Q.front();
    Q.pop();
    if (v == t)
      return dist[v];
    for (int i = 0; i < N; i++) {
      if (v[i] == t[i])
        continue;
      string next = v;
      int diff = (t[i] - v[i] + 10) % 10;
      for (int j = i; j < N; j++) {
        next[j] = ((next[j] - '0' + diff) % 10) + '0';
        if (dist.find(next) == dist.end()) {
          dist[next] = dist[v] + 1;
          Q.push(next);
        }
      }
    }
  }
}

int main() {
  string s, t;
  while (cin >> N, N) {
    cin >> s >> t;
    cout << solve(s, t) << endl;
  }
  return 0;
}

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

using namespace std;

int N;

int solve(string &s, string &t) {
  queue<string> Q;
  map<string, int> dist;
  Q.push(s);
  dist[s] = 0;
  while (!Q.empty()) {
    string v = Q.front();
    Q.pop();
    if (v == t)
      return dist[v];
    int i;
    for (i = 0; i < N; i++) {
      if (v[i] != t[i])
        break;
    }
    string next = v;
    int diff = (t[i] - v[i] + 10) % 10;
    for (int j = i; j < N; j++) {
      next[j] = ((next[j] - '0' + diff) % 10) + '0';
      if (dist.find(next) == dist.end()) {
        dist[next] = dist[v] + 1;
        Q.push(next);
      }
    }
  }
}

int main() {
  string s, t;
  while (cin >> N, N) {
    cin >> s >> t;
    cout << solve(s, t) << endl;
  }
  return 0;
}
Problem p01275 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1e8;
int main() {
  int n;
  while (cin >> n, n) {
    string s, t;
    cin >> s >> t;
    map<string, int> m;
    m[s] = 0;
    queue<string> q;
    q.push(s);
    while (!s.empty()) {
      string a = q.front();
      q.pop();
      int g = 0;
      while (a[g] == t[g])
        g++;
      int r = (10 + t[g] - a[g]) % 10;
      string b = a;
      loop(i, g, n) {
        b[i] = (b[i] + r - '0') % 10 + '0';
        //				cout<<b<<" "<<r<<endl;
        if (m[b] == 0 || m[a] + 1 < m[b]) {
          m[b] = m[a] + 1;
          q.push(b);
          if (b == t) {
            cout << m[b] << endl;
            goto end;
          }
        }
      }
    }
  end:;
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1e8;
int main() {
  int n;
  while (cin >> n, n) {
    string s, t;
    cin >> s >> t;
    if (s == t) {
      cout << 0 << endl;
      continue;
    }
    map<string, int> m;
    m[s] = 0;
    queue<string> q;
    q.push(s);
    while (!s.empty()) {
      string a = q.front();
      q.pop();
      int g = 0;
      while (a[g] == t[g])
        g++;
      int r = (10 + t[g] - a[g]) % 10;
      string b = a;
      loop(i, g, n) {
        b[i] = (b[i] + r - '0') % 10 + '0';
        //				cout<<b<<" "<<r<<endl;
        if (m[b] == 0 || m[a] + 1 < m[b]) {
          m[b] = m[a] + 1;
          q.push(b);
          if (b == t) {
            cout << m[b] << endl;
            goto end;
          }
        }
      }
    }
  end:;
  }
}
Problem p01275 (C++)

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

int n, ans;
string A, B;

string cal(string a, int x, int l, int r) {
  for (int i = l; i < r; i++) {
    int p = a[i] - '0';
    p = (p + x) % 10;
    a[i] = '0' + p;
  }
  return a;
}

void dfs(string a, string b, int d, int sum) {

  // cout<<a<<' '<<b<<' '<<d<<endl;

  if (a == b) {
    ans = min(ans, sum);
    return;
  }

  if (a[d] != b[d]) {
    for (int i = 1; i <= 9; i++) {
      string nex = cal(a, i, d, d + 1);
      if (nex[d] != b[d])
        continue;
      for (int r = d + 1; r <= n; r++) {
        nex = cal(a, i, d, r);
        dfs(nex, b, d + 1, sum + 1);
      }
    }
  } else {
    dfs(a, b, d + 1, sum);
  }
}

signed main() {
  while (cin >> n, n) {
    cin >> A >> B;
    ans = 1e9;
    dfs(A, B, 0, 0);
    cout << ans << endl;
  }
}


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

int n, ans;
string A, B;

string cal(string a, int x, int l, int r) {
  for (int i = l; i < r; i++) {
    int p = a[i] - '0';
    p = (p + x) % 10;
    a[i] = '0' + p;
  }
  return a;
}

void dfs(string a, string b, int d, int sum) {

  // cout<<a<<' '<<b<<' '<<d<<endl;

  if (ans <= sum)
    return;

  if (a == b) {
    ans = min(ans, sum);
    return;
  }

  if (a[d] != b[d]) {
    for (int i = 1; i <= 9; i++) {
      string nex = cal(a, i, d, d + 1);
      if (nex[d] != b[d])
        continue;
      for (int r = d + 1; r <= n; r++) {
        nex = cal(a, i, d, r);
        dfs(nex, b, d + 1, sum + 1);
      }
    }
  } else {
    dfs(a, b, d + 1, sum);
  }
}

signed main() {
  while (cin >> n, n) {
    cin >> A >> B;
    ans = 1e9;
    dfs(A, B, 0, 0);
    cout << ans << endl;
  }
}
Problem p01277 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <random>
#include <set>
#include <vector>
using namespace std;
using ll = long long;
struct P {
  ll x;
  ll y;
  bool operator<(const P &p) const { return (x != p.x) ? x < p.x : y < p.y; }
};
vector<P> pos;
int N;

bool ok(const int i, const int j) {
  const ll x = (pos[i].x + pos[j].x) / 2;
  const ll y = (pos[i].y + pos[j].y) / 2;
  const ll dx = pos[j].x - x;
  const ll dy = pos[j].y - y;
  set<P> p;
  int cnt = 0;
  ll h = 0;
  for (int k = 0; k < N; k++) {
    if (k == i or k == j) {
      continue;
    }
    const ll X = pos[k].x - x;
    const ll Y = pos[k].y - y;
    const ll S = X * dx + Y * dy;
    const ll T = -X * dy + Y * dx;
    h = max(h, abs(T));
    if (S == 0) {
      cnt++;
    }
    p.insert(P{S, T});
  }
  if (cnt >= 3 or h == 0) {
    return false;
  }
  for (const auto &pp : p) {
    if (p.find(P{-pp.x, pp.y}) == p.end()) {
      return false;
    }
  }
  return true;
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    ll x, y;
    cin >> x >> y;
    pos.push_back(P{2 * x, 2 * y});
  }
  mt19937 mt{random_device{}()};
  uniform_int_distribution<int> dist{0, N - 1};
  for (int i = 0; i < 100 * N; i++) {
    int u = 0;
    int v = 0;
    while (u == v) {
      u = dist(mt);
      v = dist(mt);
    }
    if (ok(u, v)) {
      cout << "Yes" << endl;
      return 0;
    }
  }
  cout << "No" << endl;

  return 0;
}


Fixed submission:
#include <iostream>
#include <random>
#include <set>
#include <vector>
using namespace std;
using ll = long long;
struct P {
  ll x;
  ll y;
  bool operator<(const P &p) const { return (x != p.x) ? x < p.x : y < p.y; }
};
vector<P> pos;
int N;

bool ok(const int i, const int j) {
  const ll x = (pos[i].x + pos[j].x) / 2;
  const ll y = (pos[i].y + pos[j].y) / 2;
  const ll dx = pos[j].x - x;
  const ll dy = pos[j].y - y;
  set<P> p;
  int cnt = 0;
  ll h = 0;
  for (int k = 0; k < N; k++) {
    if (k == i or k == j) {
      continue;
    }
    const ll X = pos[k].x - x;
    const ll Y = pos[k].y - y;
    const ll S = X * dx + Y * dy;
    const ll T = -X * dy + Y * dx;
    h = max(h, abs(T));
    if (S == 0) {
      cnt++;
    }
    p.insert(P{S, T});
  }
  if (cnt >= 3 or h == 0) {
    return false;
  }
  for (const auto &pp : p) {
    if (p.find(P{-pp.x, pp.y}) == p.end()) {
      return false;
    }
  }
  return true;
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    ll x, y;
    cin >> x >> y;
    pos.push_back(P{2 * x, 2 * y});
  }
  mt19937 mt{random_device{}()};
  uniform_int_distribution<int> dist{0, N - 1};
  for (int i = 0; i < 30 * N; i++) {
    int u = 0;
    int v = 0;
    while (u == v) {
      u = dist(mt);
      v = dist(mt);
    }
    if (ok(u, v)) {
      cout << "Yes" << endl;
      return 0;
    }
  }
  cout << "No" << endl;

  return 0;
}
Problem p01279 (C++)

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

typedef complex<double> Point;

int n, m;
Point p[100];
double v[100];

vector<vector<int>> g;
vector<int> match;
vector<bool> used;

bool dfs(int v) {
  used[v] = true;
  for (int u : g[v]) {
    int w = match[u];
    if (w < 0 || !used[w] && dfs(w)) {
      match[v] = u;
      match[u] = v;
      return true;
    }
  }
  return false;
}

int solve() {
  int res = 0;
  match.assign(n + m, -1);
  for (int v = 0; v < m + n; v++) {
    if (match[v] < 0) {
      used.assign(n + m, false);
      if (dfs(v))
        res++;
    }
  }
  return res;
}

bool check(double x) {
  g.assign(n + m, vector<int>());

  for (int i = 0; i < n; i++) {
    for (int j = n; j < n + m; j++) {
      double dist = abs(p[i] - p[j]);
      if (dist <= x * v[i]) {
        g[i].push_back(j);
        g[j].push_back(i);
      }
    }
  }

  return solve() == m;
}

int main() {
  while (scanf("%d %d", &n, &m), n) {
    for (int i = 0; i < n + m; i++) {
      double x, y;
      scanf("%lf %lf", &x, &y);
      p[i] = Point(x, y);
      if (i < n)
        scanf("%lf", v + i);
    }

    double l = 0.0, r = 20000.0;
    for (int i = 0; i < 100; i++) {
      double m = (l + r) / 2;
      if (check(m))
        r = m;
      else
        l = m;
    }

    printf("%.20f\n", l);
  }
}

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

typedef complex<double> Point;

int n, m;
Point p[200];
double v[200];

vector<vector<int>> g;
vector<int> match;
vector<bool> used;

bool dfs(int v) {
  used[v] = true;
  for (int u : g[v]) {
    int w = match[u];
    if (w < 0 || !used[w] && dfs(w)) {
      match[v] = u;
      match[u] = v;
      return true;
    }
  }
  return false;
}

int solve() {
  int res = 0;
  match.assign(n + m, -1);
  for (int v = 0; v < m + n; v++) {
    if (match[v] < 0) {
      used.assign(n + m, false);
      if (dfs(v))
        res++;
    }
  }
  return res;
}

bool check(double x) {
  g.assign(n + m, vector<int>());

  for (int i = 0; i < n; i++) {
    for (int j = n; j < n + m; j++) {
      double dist = abs(p[i] - p[j]);
      if (dist <= x * v[i]) {
        g[i].push_back(j);
        g[j].push_back(i);
      }
    }
  }

  return solve() == m;
}

int main() {
  while (scanf("%d %d", &n, &m), n) {
    for (int i = 0; i < n + m; i++) {
      double x, y;
      scanf("%lf %lf", &x, &y);
      p[i] = Point(x, y);
      if (i < n)
        scanf("%lf", v + i);
    }

    double l = 0.0, r = 20000.0;
    for (int i = 0; i < 100; i++) {
      double m = (l + r) / 2;
      if (check(m))
        r = m;
      else
        l = m;
    }

    printf("%.20f\n", l);
  }
}
Problem p01283 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <iostream>
using namespace std;
int n, a[256];
int main() {
  while (cin >> n, n) {
    for (int i = 0; i < n; i++)
      cin >> a[i];
    int mi, mj, mk;
    long double v = 1e+300L;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        for (int k = 0; k < n; k++) {
          int b[257] = {0}, c[257] = {0};
          b[0] = i;
          for (int l = 1; l <= n; l++)
            b[l] = (j * b[l - 1] + k) % 256;
          for (int l = 0; l < n; l++)
            c[(a[l] + b[l + 1]) % 256]++;
          long double res = 0;
          for (int l = 0; l < 256; l++) {
            if (c[l])
              res -= c[l] * log(1.0L * c[l] / n);
          }
          if (v - 1e-13L > res)
            v = res, mi = i, mj = j, mk = k;
        }
      }
    }
    cout << mi << ' ' << mj << ' ' << mk << endl;
  }
}

Fixed submission:
#include <cmath>
#include <iostream>
using namespace std;
int n, a[256];
int main() {
  while (cin >> n, n) {
    for (int i = 0; i < n; i++)
      cin >> a[i];
    int mi, mj, mk;
    long double v = 1e+300L;
    for (int i = 0; i < 16; i++) {
      for (int j = 0; j < 16; j++) {
        for (int k = 0; k < 16; k++) {
          int b[257] = {0}, c[257] = {0};
          b[0] = i;
          for (int l = 1; l <= n; l++)
            b[l] = (j * b[l - 1] + k) % 256;
          for (int l = 0; l < n; l++)
            c[(a[l] + b[l + 1]) % 256]++;
          long double res = 0;
          for (int l = 0; l < 256; l++) {
            if (c[l])
              res -= c[l] * log(1.0L * c[l] / n);
          }
          if (v - 1e-13L > res)
            v = res, mi = i, mj = j, mk = k;
        }
      }
    }
    cout << mi << ' ' << mj << ' ' << mk << endl;
  }
}
Problem p01284 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#include <unordered_map>
#pragma warning(disable : 4996)
using namespace std;

const int My_Inf = 2147483647;
const long long int My_LInf = 9223372036854775807;

int main() {
  while (1) {
    int T;
    cin >> T;
    if (!T)
      break;
    vector<int> sleeps;
    for (int i = 0; i < T; ++i) {
      int t;
      cin >> t;
      sleeps.push_back(t);
    }
    int N;
    cin >> N;
    vector<int> needtimes(100, 25);
    for (int i = 0; i < N; ++i) {
      int d, m;
      cin >> d >> m;
      d--;
      needtimes[d] = min(needtimes[d], m);
    }

    vector<vector<int>> dp(100 + 1, vector<int>(T, 999));
    dp[0][0] = 0;
    for (int i = 0; i < 100; ++i) {
      for (int j = 0; j < T; ++j) {
        if (needtimes[i] >= sleeps[j]) {
          dp[i + 1][(j + 1) % T] = min(dp[i][j], dp[i + 1][(j + 1) % T]);
        }
        dp[i + 1][1 % T] = min(dp[i + 1][1 % T], dp[i][j] + 1);
      }
    }
    int ans = 99999999;
    for (int i = 0; i < T; ++i) {
      ans = min(dp[101][i], ans);
    }
    cout << ans << endl;
  }

  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
#include <unordered_map>
#pragma warning(disable : 4996)
using namespace std;

const int My_Inf = 2147483647;
const long long int My_LInf = 9223372036854775807;

int main() {
  while (1) {
    int T;
    cin >> T;
    if (!T)
      break;
    vector<int> sleeps;
    for (int i = 0; i < T; ++i) {
      int t;
      cin >> t;
      sleeps.push_back(t);
    }
    int N;
    cin >> N;
    vector<int> needtimes(100, 25);
    for (int i = 0; i < N; ++i) {
      int d, m;
      cin >> d >> m;
      d--;
      needtimes[d] = min(needtimes[d], m);
    }

    vector<vector<int>> dp(100 + 1, vector<int>(T, 999));
    dp[0][0] = 0;
    for (int i = 0; i < 100; ++i) {
      for (int j = 0; j < T; ++j) {
        if (needtimes[i] >= sleeps[j]) {
          dp[i + 1][(j + 1) % T] = min(dp[i][j], dp[i + 1][(j + 1) % T]);
        }
        dp[i + 1][1 % T] = min(dp[i + 1][1 % T], dp[i][j] + 1);
      }
    }
    int ans = 99999999;
    for (int i = 0; i < T; ++i) {
      ans = min(dp[100][i], ans);
    }
    cout << ans << endl;
  }

  return 0;
}
Problem p01288 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
// #pragma comment(linker,"/STACK:1024000000,1024000000")
using namespace std;
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
const double PI = acos(-1.0);
typedef long long ll;
const int maxn = 100010;
int tree[maxn];
int Find(int x) {
  if (tree[x] == x)
    return x;
  return Find(tree[x]);
}
int main() {
  int n, m, id, i;
  char com[10];
  ll ans;

  while (scanf("%d%d", &n, &m), n || m) {
    tree[1] = 1;
    ans = 0;
    for (i = 1; i < n; i++)
      scanf("%d", &tree[i]);
    for (i = 0; i < m; i++) {
      scanf("%s%d", com, &id);
      if (com[0] == 'M')
        tree[id] = id;
      else
        ans += Find(id);
    }
    printf("%lld\n", ans);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
// #pragma comment(linker,"/STACK:1024000000,1024000000")
using namespace std;
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
const double PI = acos(-1.0);
typedef long long ll;
const int maxn = 100010;
int tree[maxn];
int Find(int x) {
  if (tree[x] == x)
    return x;
  return Find(tree[x]);
}
int main() {
  int n, m, id, i;
  char com[10];
  ll ans;

  while (scanf("%d%d", &n, &m), n || m) {
    tree[1] = 1;
    ans = 0;
    for (i = 2; i <= n; i++)
      scanf("%d", &tree[i]);
    for (i = 0; i < m; i++) {
      scanf("%s%d", com, &id);
      if (com[0] == 'M')
        tree[id] = id;
      else
        ans += Find(id);
    }
    printf("%lld\n", ans);
  }
  return 0;
}
Problem p01288 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef pair<char, int> P;
const int maxn = 100005;
int tree[maxn];
int ar[maxn];
P query[maxn];
int get(int i) {
  int par = tree[i];
  while (!ar[par]) {
    par = tree[par];
  }
  return par;
}
int main() {
  int n, q;
  while (scanf("%d%d", &n, &q) && (q + n) != 0) {
    memset(ar, 0, sizeof(ar));
    ar[1] = true;
    for (int i = 2; i <= n; i++)
      scanf("%d", &tree[i]);
    for (int i = 0; i < q; i++)
      scanf(" %c%d", &query[i].first, &query[i].second);
    long long sum = 0;
    for (int i = 0; i < q; i++)
      if (query[i].first == 'M')
        ar[query[i].second] += 1;
    for (int i = q - 1; i >= 0; i--)
      if (query[i].first == 'M')
        ar[query[i].second] -= 1;
      else
        sum += get(query[i].second);
    printf("%lld\n", sum);
  }
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef pair<char, int> P;
const int maxn = 100005;
int tree[maxn];
int ar[maxn];
P query[maxn];
int get(int i) {
  if (ar[i])
    return i;
  return tree[i] = get(tree[i]);
}
int main() {
  int n, q;
  while (scanf("%d%d", &n, &q) && (q + n) != 0) {
    memset(ar, 0, sizeof(ar));
    ar[1] = true;
    for (int i = 2; i <= n; i++)
      scanf("%d", &tree[i]);
    for (int i = 0; i < q; i++)
      scanf(" %c%d", &query[i].first, &query[i].second);
    long long sum = 0;
    for (int i = 0; i < q; i++)
      if (query[i].first == 'M')
        ar[query[i].second] += 1;
    for (int i = q - 1; i >= 0; i--)
      if (query[i].first == 'M')
        ar[query[i].second] -= 1;
      else
        sum += get(query[i].second);
    printf("%lld\n", sum);
  }
  return 0;
}
Problem p01288 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
using namespace std;

int n, q, root[105000];
long long res;
int marked[105000];
pair<char, int> query[105000];

int r(int x) {
  if (marked[x])
    return x;
  return root[x] = r(root[x]);
}

int main() {
  while (true) {
    scanf("%d%d", &n, &q);
    //	cin >> n >> q;
    if (n == 0 && q == 0)
      return 0;
    res = 0;
    fill(marked, marked + n + 2, 0);
    marked[1] = true;
    for (int i = 1; i < n; i++) {
      cin >> root[i];
      root[i]--;
    }
    for (int i = 0; i < q; i++) {
      cin >> query[i].first >> query[i].second;
      query[i].second--;
      marked[query[i].second] += (query[i].first == 'M');
    }
    for (int i = q; i-- > 0;) {
      if (query[i].first == 'M') {
        marked[query[i].second]--;
      } else {
        res += r(query[i].second) + 1;
      }
    }
    printf("%lld\n", res);
    //	cout << res << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
using namespace std;

int n, q, root[105000];
long long res;
int marked[105000];
pair<char, int> query[105000];

int r(int x) {
  if (marked[x])
    return x;
  return root[x] = r(root[x]);
}

int main() {
  while (true) {
    scanf("%d%d", &n, &q);
    //	cin >> n >> q;
    if (n == 0 && q == 0)
      return 0;
    res = 0;
    fill(marked, marked + n + 2, 0);
    marked[0] = true;
    for (int i = 1; i < n; i++) {
      cin >> root[i];
      root[i]--;
    }
    for (int i = 0; i < q; i++) {
      cin >> query[i].first >> query[i].second;
      query[i].second--;
      marked[query[i].second] += (query[i].first == 'M');
    }
    for (int i = q; i-- > 0;) {
      if (query[i].first == 'M') {
        marked[query[i].second]--;
      } else {
        res += r(query[i].second) + 1;
      }
    }
    printf("%lld\n", res);
    //	cout << res << endl;
  }
}
Problem p01294 (C++)

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

bool solve(int n, int d) {
  vector<vector<int>> c(n);

  for (int i = 0; i < n; i++) {
    int m;
    cin >> m;
    for (int j = 0; j < m; j++) {
      int buf;
      cin >> buf;
      c[i].push_back(buf);
    }
  }
  if (n == 1)
    return true;

  while (true) {
    vector<int> sum(n);
    for (int i = 0; i < n; i++)
      sum[i] = accumulate(c[i].begin(), c[i].end(), 0);
    int midx = max_element(sum.begin(), sum.end()) - sum.begin();
    int smidx = midx ? 0 : 1;
    for (int i = 0; i < n; i++) {
      if (i != midx && sum[smidx] < sum[i])
        smidx = i;
    }
    bool isok = false;
    for (int i = 0; i < n && !isok; i++) {
      if (i == midx)
        continue;
      if (c[i].size() > 0 && sum[midx] - (sum[i] - c[i].back()) <= d) {
        c[i].pop_back();
        isok = true;
      }
    }
    if (isok)
      continue;
    if (c[midx].size() == 0 || sum[smidx] - (sum[midx] - c[midx].back()) > d) {
      break;
    }
    c[midx].pop_back();
  }
  for (int i = 0; i < n; i++) {
    cerr << c[i].size() << endl;
  }
  return all_of(c.begin(), c.end(),
                [](const vector<int> &vec) { return vec.empty(); });
}
int main() {
  int n, d;
  while (cin >> n >> d, n) {
    cout << (solve(n, d) ? "Yes" : "No") << endl;
  }
  return 0;
}


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

bool solve(int n, int d) {
  vector<vector<int>> c(n);

  for (int i = 0; i < n; i++) {
    int m;
    cin >> m;
    for (int j = 0; j < m; j++) {
      int buf;
      cin >> buf;
      c[i].push_back(buf);
    }
  }
  if (n == 1)
    return true;

  while (true) {
    vector<int> sum(n);
    for (int i = 0; i < n; i++)
      sum[i] = accumulate(c[i].begin(), c[i].end(), 0);
    int midx = max_element(sum.begin(), sum.end()) - sum.begin();
    int smidx = midx ? 0 : 1;
    for (int i = 0; i < n; i++) {
      if (i != midx && sum[smidx] < sum[i])
        smidx = i;
    }
    bool isok = false;
    for (int i = 0; i < n && !isok; i++) {
      if (i == midx)
        continue;
      if (c[i].size() > 0 && sum[midx] - (sum[i] - c[i].back()) <= d) {
        c[i].pop_back();
        isok = true;
      }
    }
    if (isok)
      continue;
    if (c[midx].size() == 0 || sum[smidx] - (sum[midx] - c[midx].back()) > d) {
      break;
    }
    c[midx].pop_back();
  }
  return all_of(c.begin(), c.end(),
                [](const vector<int> &vec) { return vec.empty(); });
}
int main() {
  int n, d;
  while (cin >> n >> d, n) {
    cout << (solve(n, d) ? "Yes" : "No") << endl;
  }
  return 0;
}
Problem p01294 (C++)

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

using namespace std;

int main() {
  for (int n, d; cin >> n >> d, n;) {
    vector<int> c[100];
    int s[100] = {};
    int nm = 0;
    for (int i = 0; i < n; i++) {
      int m;
      cin >> m;
      c[i].resize(m);
      for (int j = 0; j < m; j++) {
        cin >> c[i][j];
        nm++;
        s[i] += c[i][j];
      }
    }
    if (n == 1) {
      nm = 0;
    }
    for (; nm; nm--) {
      vector<int> v(begin(s), begin(s) + n);
      sort(v.rbegin(), v.rend());
      for (int i = 0; i < n; i++) {
        if (!v.empty() &&
            ((s[i] == v[0]) ? v[1] : v[0]) - (s[i] - c[i].back()) <= d) {
          s[i] -= c[i].back();
          c[i].pop_back();
          goto next;
        }
      }
      break;
    next:;
    }
    cout << (nm ? "No" : "Yes") << endl;
  }
}

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

using namespace std;

int main() {
  for (int n, d; cin >> n >> d, n;) {
    vector<int> c[100];
    int s[100] = {};
    int nm = 0;
    for (int i = 0; i < n; i++) {
      int m;
      cin >> m;
      c[i].resize(m);
      for (int j = 0; j < m; j++) {
        cin >> c[i][j];
        nm++;
        s[i] += c[i][j];
      }
    }
    if (n == 1) {
      nm = 0;
    }
    for (; nm; nm--) {
      vector<int> v(begin(s), begin(s) + n);
      sort(v.rbegin(), v.rend());
      for (int i = 0; i < n; i++) {
        if (!c[i].empty() &&
            ((s[i] == v[0]) ? v[1] : v[0]) - (s[i] - c[i].back()) <= d) {
          s[i] -= c[i].back();
          c[i].pop_back();
          goto next;
        }
      }
      break;
    next:;
    }
    cout << (nm ? "No" : "Yes") << endl;
  }
}
Problem p01294 (C++)

Buggy submission (status: 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 (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;
}

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 (!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;
}
Problem p01294 (C++)

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

const int INF = 1 << 28;

int main() {
  for (int n, d; cin >> n >> d && (n | d);) {
    vector<vector<int>> c(n);
    for (int i = 0; i < n; ++i) {
      int m;
      cin >> m;
      c[i].resize(m);
      for (int j = 0; j < m; ++j)
        cin >> c[i][j];
    }
    vector<int> sum(n);
    for (int i = 0; i < n; ++i) {
      sum[i] = accumulate(c[i].begin(), c[i].end(), 0);
    }
    while (1) {
      bool update = false;
      for (int i = 0; i < n; ++i) {
        sum[i] -= c[i].back();
        if (*max_element(sum.begin(), sum.end()) -
                *min_element(sum.begin(), sum.end()) <=
            d) {
          c[i].pop_back();
          update = true;
        } else {
          sum[i] += c[i].back();
        }
      }
      if (!update)
        break;
    }
    if (accumulate(sum.begin(), sum.end(), 0)) {
      cout << "No" << endl;
    } else {
      cout << "Yes" << endl;
    }
  }
  return 0;
}

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

const int INF = 1 << 28;

int main() {
  for (int n, d; cin >> n >> d && (n | d);) {
    vector<vector<int>> c(n);
    for (int i = 0; i < n; ++i) {
      int m;
      cin >> m;
      c[i].resize(m);
      for (int j = 0; j < m; ++j)
        cin >> c[i][j];
    }
    vector<int> sum(n);
    for (int i = 0; i < n; ++i) {
      sum[i] = accumulate(c[i].begin(), c[i].end(), 0);
    }
    while (1) {
      bool update = false;
      for (int i = 0; i < n; ++i) {
        if (c[i].empty())
          continue;
        sum[i] -= c[i].back();
        if (*max_element(sum.begin(), sum.end()) -
                *min_element(sum.begin(), sum.end()) <=
            d) {
          c[i].pop_back();
          update = true;
        } else {
          sum[i] += c[i].back();
        }
      }
      if (!update)
        break;
    }
    if (accumulate(sum.begin(), sum.end(), 0)) {
      cout << "No" << endl;
    } else {
      cout << "Yes" << endl;
    }
  }
  return 0;
}