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

Buggy submission (status: Time Limit Exceeded):
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

#include <queue>
typedef long long ll;

#include <cstdio>
inline int getInt() {
  int s;
  scanf("%d", &s);
  return s;
}

#include <set>

using namespace std;

const ll mod = 1000000007;
class BIT {
  vector<ll> bit;

public:
  BIT(int n) : bit(n + 1) {}
  void add(int idx, ll val) {
    idx++;
    while (idx < (int)bit.size()) {
      (bit[idx] += val) %= mod;
      idx += idx & -idx;
    }
  }
  ll sum(int i) {
    ll s = 0;
    i++;
    while (i > 0) {
      (s += bit[i]) %= mod;
      i -= i & -i;
    }
    return s;
  }

  void swap(BIT &other) { bit.swap(other.bit); }
};

int main() {
  const int n = getInt();
  const int l = getInt();

  vector<int> x(n);
  vector<int> a(n);
  REP(i, n) x[i] = getInt();
  REP(i, n) a[i] = getInt();

  BIT dp(l + 1);
  dp.add(0, 1);

  REP(i, n) {
    BIT next(l + 1);
    const int xx = x[i];
    const int aa = a[i];

    for (int p = xx; p < l; p += aa) {
      next.add(p + 1, dp.sum(p));
    }

    dp.swap(next);
  }

  printf("%d\n", (int)dp.sum(l));

  return 0;
}

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

#include <queue>
typedef long long ll;

#include <cstdio>
inline int getInt() {
  int s;
  scanf("%d", &s);
  return s;
}

#include <set>

using namespace std;

const ll mod = 1000000007;
class BIT {
  vector<ll> bit;

public:
  BIT(int n) : bit(n + 1) {}
  void add(int idx, ll val) {
    idx++;
    while (idx < (int)bit.size()) {
      (bit[idx] += val) %= mod;
      idx += idx & -idx;
    }
  }
  ll sum(int i) {
    ll s = 0;
    i++;
    while (i > 0) {
      (s += bit[i]) %= mod;
      i -= i & -i;
    }
    return s;
  }

  void swap(BIT &other) { bit.swap(other.bit); }
};

int main() {
  const int n = getInt();
  const int l = getInt();

  vector<int> x(n);
  vector<int> a(n);
  REP(i, n) x[i] = getInt();
  REP(i, n) a[i] = getInt();

  BIT dp(l + 1);
  dp.add(0, 1);

  REP(i, n) {
    BIT next(l + 1);
    const int xx = x[i];
    const int aa = a[i];

    if (aa != 0) {
      for (int p = xx; p < l; p += aa) {
        next.add(p + 1, dp.sum(p));
      }
    } else {
      next.add(xx + 1, dp.sum(xx));
    }

    dp.swap(next);
  }

  printf("%d\n", (int)dp.sum(l));

  return 0;
}
Problem p01769 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>
#define REP(i, N) for (int i = 0; i < N; ++i)
#define REPA(i, N) for (int i = 1; i < N; ++i)
#define MOD ((int)1e9 + 7)
using namespace std;
int main() {
  int N, L;
  cin >> N >> L;
  vector<int> sp(N), ran(N);
  REP(i, N) { cin >> sp[i]; }
  REP(i, N) { cin >> ran[i]; }
  vector<vector<int>> dp(N, vector<int>(L, 0));
  for (int i = sp[0]; i < L; i += ran[0]) {
    dp[0][i] = 1;
  }
  REPA(i, N) {
    for (int j = sp[i]; j < L; j += ran[i]) {
      int sum = 0;
      if (ran[i] == 0) {
        REP(k, sp[i]) { sum = (sum + dp[i - 1][k]) % MOD; }
        dp[i][j] = sum;
        break;
      } else if (j == sp[i]) {
        REP(k, sp[i]) { sum = (sum + dp[i - 1][k]) % MOD; }
        dp[i][j] = sum;
      } else {
        for (int k = j - ran[i]; k < j; ++k) {
          sum = (sum + dp[i - 1][k]) % MOD;
        }
        dp[i][j] = (dp[i][j - ran[i]] + sum) % MOD;
      }
    }
  }
  int ans = 0;
  REP(i, L) { ans = (ans + dp[N - 1][i]) % MOD; }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
#define REP(i, N) for (int i = 0; i < N; ++i)
#define REPA(i, N) for (int i = 1; i < N; ++i)
#define MOD ((int)1e9 + 7)
using namespace std;
int main() {
  int N, L;
  cin >> N >> L;
  vector<int> sp(N), ran(N);
  REP(i, N) { cin >> sp[i]; }
  REP(i, N) { cin >> ran[i]; }
  vector<vector<int>> dp(N, vector<int>(L, 0));
  for (int i = sp[0]; i < L; i += ran[0]) {
    dp[0][i] = 1;
    if (ran[0] == 0)
      break;
  }
  REPA(i, N) {
    for (int j = sp[i]; j < L; j += ran[i]) {
      int sum = 0;
      if (ran[i] == 0) {
        REP(k, sp[i]) { sum = (sum + dp[i - 1][k]) % MOD; }
        dp[i][j] = sum;
        break;
      } else if (j == sp[i]) {
        REP(k, sp[i]) { sum = (sum + dp[i - 1][k]) % MOD; }
        dp[i][j] = sum;
      } else {
        for (int k = j - ran[i]; k < j; ++k) {
          sum = (sum + dp[i - 1][k]) % MOD;
        }
        dp[i][j] = (dp[i][j - ran[i]] + sum) % MOD;
      }
    }
  }
  int ans = 0;
  REP(i, L) { ans = (ans + dp[N - 1][i]) % MOD; }
  cout << ans << endl;
  return 0;
}
Problem p01772 (C++)

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

int main() {
  string row;
  cin >> row;
  int f = 0, cnt = 0;
  // cout<<row.size();
  for (int i = 0; row.size(); i++) {
    if (row[i] == 'A')
      f = 1;
    if (f == 1 && row[i] == 'Z') {
      cnt++;
      f = 0;
    }
  }
  if (cnt != 0) {
    for (int i = 0; i < cnt; i++) {
      cout << "AZ";
    }
    cout << endl;
  } else
    cout << -1 << endl;

  return 0;
}


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

int main() {
  string row;
  cin >> row;
  int f = 0, cnt = 0;
  // cout<<row.size();
  for (int i = 0; i < row.size(); i++) {
    if (row[i] == 'A')
      f = 1;
    if (f == 1 && row[i] == 'Z') {
      cnt++;
      f = 0;
    }
  }
  if (cnt != 0) {
    for (int i = 0; i < cnt; i++) {
      cout << "AZ";
    }
    cout << endl;
  } else
    cout << -1 << endl;

  return 0;
}
Problem p01773 (C++)

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

int main() {
  int a, n, m;
  scanf("%d", &a);
  vector<Pi> datas;
  for (int i = 0; i < a; i++) {
    int H1, H2, M1, M2;
    scanf("%d:%d-%d:%d", &H1, &M1, &H2, &M2);
    datas.push_back(make_pair(H1 * 60 + M1, H2 * 60 + M2));
  }
  scanf("%d", &n);
  vector<vector<Pi>> teacher(n);
  for (int i = 0; i < n; i++) {
    int k;
    scanf("%d", &k);
    while (k--) {
      int H1, H2, M1, M2;
      scanf("%d:%d-%d:%d", &H1, &M1, &H2, &M2);
      teacher[i].push_back(make_pair(H1 * 60 + M1, H2 * 60 + M2));
    }
  }
  scanf("%d", &m);
  vector<vector<Pi>> student(m);
  for (int i = 0; i < m; i++) {
    int k;
    scanf("%d", &k);
    while (k--) {
      int H1, H2, M1, M2;
      scanf("%d:%d-%d:%d", &H1, &M1, &H2, &M2);
      student[i].push_back(make_pair(H1 * 60 + M1, H2 * 60 + M2));
    }
  }

  int ret = 0;
  for (int i = 0; i < a; i++) {
    int t = 0, s = 0;
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < teacher[j].size(); k++) {
        if (teacher[j][k].first <= datas[i].first &&
            datas[i].second <= teacher[j][k].second) {
          ++t;
          break;
        }
      }
    }
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < student[j].size(); k++) {
        if (student[j][k].first <= datas[i].first &&
            datas[i].second <= student[j][k].second) {
          ++s;
          break;
        }
      }
    }
    ret += min(t, s);
  }
  printf("%d\n", ret);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> Pi;

int main() {
  int a, n, m;
  scanf("%d", &a);
  vector<Pi> datas;
  for (int i = 0; i < a; i++) {
    int H1, H2, M1, M2;
    scanf("%d:%d-%d:%d", &H1, &M1, &H2, &M2);
    datas.push_back(make_pair(H1 * 60 + M1, H2 * 60 + M2));
  }
  scanf("%d", &n);
  vector<vector<Pi>> teacher(n);
  for (int i = 0; i < n; i++) {
    int k;
    scanf("%d", &k);
    while (k--) {
      int H1, H2, M1, M2;
      scanf("%d:%d-%d:%d", &H1, &M1, &H2, &M2);
      teacher[i].push_back(make_pair(H1 * 60 + M1, H2 * 60 + M2));
    }
  }
  scanf("%d", &m);
  vector<vector<Pi>> student(m);
  for (int i = 0; i < m; i++) {
    int k;
    scanf("%d", &k);
    while (k--) {
      int H1, H2, M1, M2;
      scanf("%d:%d-%d:%d", &H1, &M1, &H2, &M2);
      student[i].push_back(make_pair(H1 * 60 + M1, H2 * 60 + M2));
    }
  }

  int ret = 0;
  for (int i = 0; i < a; i++) {
    int t = 0, s = 0;
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < teacher[j].size(); k++) {
        if (teacher[j][k].first <= datas[i].first &&
            datas[i].second <= teacher[j][k].second) {
          ++t;
          break;
        }
      }
    }
    for (int j = 0; j < m; j++) {
      for (int k = 0; k < student[j].size(); k++) {
        if (student[j][k].first <= datas[i].first &&
            datas[i].second <= student[j][k].second) {
          ++s;
          break;
        }
      }
    }
    ret += min(t, s);
  }
  printf("%d\n", ret);
}
Problem p01773 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
P s2p(string s) { return P(stoi(s.substr(0, 2)), stoi(s.substr(3, 2))); }
bool contain(string c, string p) {
  P stc = s2p(c.substr(0, 5));
  P enc = s2p(c.substr(6, 5));
  P stp = s2p(p.substr(0, 5));
  P enp = s2p(p.substr(6, 5));
  // cout<<stc.first<<" "<<stc.second<<endl;
  // cout<<enc.first<<" "<<enc.second<<endl;
  // cout<<stp.first<<" "<<stp.second<<endl;
  // cout<<enp.first<<" "<<enp.second<<endl;
  return stp <= stc && enc <= enp;
}
int main() {
  int k, n, m;
  cin >> k;
  string c[k];
  for (int i = 0; i < k; i++)
    cin >> c[i];
  cin >> n;
  vector<string> t[n];
  for (int i = 0, j; i < n; i++) {
    string tmp;
    cin >> j;
    for (int a = 0; a < j; a++) {
      cin >> tmp;
      t[i].push_back(tmp);
    }
  }
  cin >> m;
  vector<string> s[m];
  for (int i = 0, j; i < m; i++) {
    string tmp;
    cin >> j;
    for (int a = 0; a < j; a++) {
      cin >> tmp;
      s[i].push_back(tmp);
    }
  }
  int ans = 0;
  for (int i = 0; i < k; i++) {
    int a = 0, b = 0;
    for (int l = 0; l < n; l++) {
      bool f = 0;
      for (int j = 0; j < (int)t[l].size(); j++) {
        f |= contain(c[i], t[l][j]);
      }
      if (f)
        a++;
    }
    for (int l = 0; l < n; l++) {
      bool f = 0;
      for (int j = 0; j < (int)s[l].size(); j++) {
        f |= contain(c[i], s[l][j]);
      }
      if (f)
        b++;
    }
    // cout<<a<<" "<<b<<endl;
    ans += min(a, b);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
P s2p(string s) { return P(stoi(s.substr(0, 2)), stoi(s.substr(3, 2))); }
bool contain(string c, string p) {
  P stc = s2p(c.substr(0, 5));
  P enc = s2p(c.substr(6, 5));
  P stp = s2p(p.substr(0, 5));
  P enp = s2p(p.substr(6, 5));
  // cout<<stc.first<<" "<<stc.second<<endl;
  // cout<<enc.first<<" "<<enc.second<<endl;
  // cout<<stp.first<<" "<<stp.second<<endl;
  // cout<<enp.first<<" "<<enp.second<<endl;
  return stp <= stc && enc <= enp;
}
int main() {
  int k, n, m;
  cin >> k;
  string c[k];
  for (int i = 0; i < k; i++)
    cin >> c[i];
  cin >> n;
  vector<string> t[n];
  for (int i = 0, j; i < n; i++) {
    string tmp;
    cin >> j;
    for (int a = 0; a < j; a++) {
      cin >> tmp;
      t[i].push_back(tmp);
    }
  }
  cin >> m;
  vector<string> s[m];
  for (int i = 0, j; i < m; i++) {
    string tmp;
    cin >> j;
    for (int a = 0; a < j; a++) {
      cin >> tmp;
      s[i].push_back(tmp);
    }
  }
  int ans = 0;
  for (int i = 0; i < k; i++) {
    int a = 0, b = 0;
    for (int l = 0; l < n; l++) {
      bool f = 0;
      for (int j = 0; j < (int)t[l].size(); j++) {
        f |= contain(c[i], t[l][j]);
      }
      if (f)
        a++;
    }
    for (int l = 0; l < m; l++) {
      bool f = 0;
      for (int j = 0; j < (int)s[l].size(); j++) {
        f |= contain(c[i], s[l][j]);
      }
      if (f)
        b++;
    }
    // cout<<a<<" "<<b<<endl;
    ans += min(a, b);
  }
  cout << ans << endl;
  return 0;
}
Problem p01781 (C++)

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

using namespace std;

using ll = long long;
using ld = long double;
using P = pair<int, int>;
constexpr ld EPS = 1e-12;
constexpr int INF = numeric_limits<int>::max() / 2;
constexpr int MOD = 1e9 + 7;

ll X, Y, Z, A, B, C, N;
ll cnt_x[1000], cnt_y[1000], cnt_z[1000], res[1000];

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

  cin >> X >> Y >> Z >> A >> B >> C >> N;
  for (int i = 0; i < X; i++) {
    int diff = abs(i - A) % N;
    cnt_x[diff]++;
  }
  for (int i = 0; i < Y; i++) {
    int diff = abs(i - B) % N;
    cnt_y[diff]++;
  }
  for (int i = 0; i < Z; i++) {
    int diff = abs(i - C) % N;
    cnt_z[diff]++;
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      for (int k = 0; k < N; k++) {
        res[(i + j + k) % N] += cnt_x[i] * cnt_y[j] * cnt_z[k];
      }
    }
  }
  for (int i = 0; i < N; i++) {
    cout << res[i] << " \n"[i == N - 1];
  }
}


Fixed submission:
#include "bits/stdc++.h"

using namespace std;

using ll = long long;
using ld = long double;
using P = pair<int, int>;
constexpr ld EPS = 1e-12;
constexpr int INF = numeric_limits<int>::max() / 2;
constexpr int MOD = 1e9 + 7;

ll X, Y, Z, A, B, C, N;
ll cnt_x[1000], cnt_y[1000], cnt_z[1000], res[1000];

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

  cin >> X >> Y >> Z >> A >> B >> C >> N;
  for (int i = 0; i < X; i++) {
    int diff = abs(i - A) % N;
    cnt_x[diff]++;
  }
  for (int i = 0; i < Y; i++) {
    int diff = abs(i - B) % N;
    cnt_y[diff]++;
  }
  for (int i = 0; i < Z; i++) {
    int diff = abs(i - C) % N;
    cnt_z[diff]++;
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      for (int k = 0; k < N; k++) {
        int idx = i + j + k;
        if (idx >= 2 * N)
          idx -= 2 * N;
        else if (idx >= N)
          idx -= N;
        res[idx] += cnt_x[i] * cnt_y[j] * cnt_z[k];
      }
    }
  }
  for (int i = 0; i < N; i++) {
    cout << res[i] << " \n"[i == N - 1];
  }
}
Problem p01809 (C++)

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

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int main() {
  int p, q;
  cin >> p >> q;
  p = gcd(p, q);
  q /= p;
  set<int> s;
  for (int i = 2; i <= q;) {
    if (q % i == 0) {
      q /= i;
      s.insert(i);
    } else {
      i++;
    }
  }
  int r = 1;
  for (auto it = begin(s); it != end(s); it++) {
    r *= *it;
  }
  cout << r << endl;
  return 0;
}

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

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int main() {
  int p, q;
  cin >> p >> q;
  p = gcd(p, q);
  q /= p;
  set<int> s;
  for (int i = 2; i <= q;) {
    if (i * i > q) {
      i = q;
    }
    if (q % i == 0) {
      q /= i;
      s.insert(i);
    } else {
      i++;
    }
  }
  int r = 1;
  for (auto it = begin(s); it != end(s); it++) {
    r *= *it;
  }
  cout << r << endl;
  return 0;
}
Problem p01809 (C++)

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

using namespace std;

#define INF 1001000100010001000
#define MOD 1000000007
#define EPS 1e-10
#define int long long
#define rep(i, N) for (int i = 0; i < N; i++)
#define Rep(i, N) for (int i = 1; i < N; i++)
#define For(i, a, b) for (int i = (a); i < (b); i++)
#define pb push_back
#define mp make_pair
#define i_i pair<int, int>
#define vi vector<int>
#define vvi vector<vi>
#define vb vector<bool>
#define vvb vector<vb>
#define vp vector<i_i>
#define all(a) (a).begin(), (a).end()
#define Int(x)                                                                 \
  int x;                                                                       \
  scanf("%lld", &x);

// int dxy[5] = {0, 1, 0, -1, 0};
//  assign

int gcd(int a, int b);

signed main() {
  Int(p);
  Int(q);

  while (p && q) {
    int base = gcd(p, q);
    q /= base;
    for (int i = 2; i * i <= q; i++) {
      if (!(q % i)) {
        int ret = q / i;
        while (!(ret % i)) {
          q /= i;
          ret /= i;
        }
      }
    }
    cout << q << endl;
    cin >> p >> q;
  }

  return 0;
}

int gcd(int a, int b) {
  if (!b) {
    return a;
  } else {
    gcd(b, a % b);
  }
}

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

using namespace std;

#define INF 1001000100010001000
#define MOD 1000000007
#define EPS 1e-10
#define int long long
#define rep(i, N) for (int i = 0; i < N; i++)
#define Rep(i, N) for (int i = 1; i < N; i++)
#define For(i, a, b) for (int i = (a); i < (b); i++)
#define pb push_back
#define mp make_pair
#define i_i pair<int, int>
#define vi vector<int>
#define vvi vector<vi>
#define vb vector<bool>
#define vvb vector<vb>
#define vp vector<i_i>
#define all(a) (a).begin(), (a).end()
#define Int(x)                                                                 \
  int x;                                                                       \
  scanf("%lld", &x);

// int dxy[5] = {0, 1, 0, -1, 0};
//  assign

int gcd(int a, int b);

signed main() {
  Int(p);
  Int(q);

  while (p && q) {
    int base = gcd(p, q);
    q /= base;
    for (int i = 2; i * i <= q; i++) {
      if (!(q % i)) {
        int ret = q / i;
        while (!(ret % i)) {
          q /= i;
          ret /= i;
        }
      }
    }
    cout << q << endl;
    p = 0;
    q = 0;
  }

  return 0;
}

int gcd(int a, int b) {
  if (!b) {
    return a;
  } else {
    gcd(b, a % b);
  }
}
Problem p01810 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define CH(N, A, B) (A <= N && N < B)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define RREP(i, a, b) for (int i = (b - 1); a <= i; i--)
#define F first
#define S second
#define ll long long;

const int INF = 1e9;
const long long LLINF = 1e15;

using namespace std;

long long N, k, a[5] = {0, 0, 1, 2, 1};

int main() {
  cin >> N >> k;
  long long num = 0;
  REP(i, 0, N - 1) {
    while (a[0] * k - a[1] > num || num > a[2] * k - a[3]) {
      REP(j, 0, 5) a[j]++;
    }
    num += a[4];
  }
  cout << num << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define CH(N, A, B) (A <= N && N < B)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define RREP(i, a, b) for (int i = (b - 1); a <= i; i--)
#define F first
#define S second
#define ll long long;

const int INF = 1e9;
const long long LLINF = 1e15;

using namespace std;

long long N, k, a[5] = {0, 0, 1, 2, 1};

int main() {
  cin >> N >> k;
  long long num = 0;
  REP(i, 0, N - 1) {
    if (a[0] * k - a[1] > num || num > a[2] * k - a[3]) {
      long long inc = (k * a[0] - a[1] - num) / (1 - k);
      REP(j, 0, 5) a[j] += inc;
    }
    num += a[4];
  }
  cout << num << endl;
  return 0;
}
Problem p01811 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define REP(i, n, N) for (int i = n; i < N; i++)
#define p(S) cout << (S) << endl
#define ck(a, b) (0 <= (a) && (a) < b)
using namespace std;

string abcgene(string anss) {
  queue<string> q;
  q.push(anss);
  string c[3] = {"A", "B", "C"};
  map<string, bool> m;
  m[anss] = true;
  while (!q.empty()) {
    string s = q.front();
    q.pop();
    if (s == "ABC")
      return "Yes";
    REP(j, 0, 3) {
      string tmps = s;
      int cnt = 0;
      REP(i, 0, (int)tmps.size() - 2) {
        if (tmps.substr(i, 3) == "ABC") {
          tmps.replace(i, 3, c[j]);
          cnt++;
        }
      }
      if (tmps[tmps.size() - 1] != 'C' || tmps[tmps.size() - 2] == 'A')
        continue;
      if (!m[tmps]) {
        m[tmps] = true;
        q.push(tmps);
      }
    }
  }
  return "No";
}

int main() {
  string ans;
  cin >> ans;
  p(abcgene(ans));
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n, N) for (int i = n; i < N; i++)
#define p(S) cout << (S) << endl
#define ck(a, b) (0 <= (a) && (a) < b)
using namespace std;

string abcgene(string anss) {
  queue<string> q;
  q.push(anss);
  string c[3] = {"A", "B", "C"};
  map<string, bool> m;
  m[anss] = true;
  while (!q.empty()) {
    string s = q.front();
    q.pop();
    if (s == "ABC")
      return "Yes";
    REP(j, 0, 3) {
      string tmps = s;
      int cnt = 0;
      REP(i, 0, (int)tmps.size() - 2) {
        if (tmps.substr(i, 3) == "ABC") {
          tmps.replace(i, 3, c[j]);
          cnt++;
        }
      }
      if (cnt != count(tmps.begin(), tmps.end(), 'A' + j))
        continue;
      if (!m[tmps]) {
        m[tmps] = true;
        q.push(tmps);
      }
    }
  }
  return "No";
}

int main() {
  string ans;
  cin >> ans;
  p(abcgene(ans));
  return 0;
}
Problem p01811 (C++)

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

using namespace std;
string s;
int cnt[3];
bool ok(string s, int a, int b, int c) {
  if (s == "ABC")
    return true;
  string nxt;
  char to;
  int abc;
  if (a < b && a < c)
    to = 'A', abc = a, b -= a, c -= a;
  else if (b < a && b < c)
    to = 'B', abc = b, a -= b, c -= b;
  else if (c < a && c < b)
    to = 'C', abc = c, a -= c, b -= c;
  else
    return false;
  int p = 0;
  while (p < s.size()) {
    if (p + 2 < s.size() && s[p] == 'A' && s[p + 1] == 'B' && s[p + 2] == 'C') {
      nxt += to;
      p += 3;
      abc--;
    } else {
      nxt += s[p];
      p++;
    }
  }
  if (abc != 0)
    return false;
  return ok(nxt, a, b, c);
}

int main() {
  cin >> s;
  for (int i = 0; i < s.size(); i++) {
    cnt[s[i] - 'A']++;
  }
  if (ok(s, cnt[0], cnt[1], cnt[2]))
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}


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

using namespace std;
string s;
int cnt[3];
bool ok(string s, int a, int b, int c) {
  if (s == "ABC")
    return true;
  string nxt;
  char to;
  int abc;
  if (a < b && a < c)
    to = 'A', abc = a, b -= a, c -= a;
  else if (b < a && b < c)
    to = 'B', abc = b, a -= b, c -= b;
  else if (c < a && c < b)
    to = 'C', abc = c, a -= c, b -= c;
  else
    return false;
  if (abc == 0)
    return false;
  int p = 0;
  while (p < s.size()) {
    if (p + 2 < s.size() && s[p] == 'A' && s[p + 1] == 'B' && s[p + 2] == 'C') {
      nxt += to;
      p += 3;
      abc--;
    } else {
      nxt += s[p];
      p++;
    }
  }
  if (abc != 0)
    return false;
  return ok(nxt, a, b, c);
}

int main() {
  cin >> s;
  for (int i = 0; i < s.size(); i++) {
    cnt[s[i] - 'A']++;
  }
  if (ok(s, cnt[0], cnt[1], cnt[2]))
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}
Problem p01811 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

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

bool f(const string &s) {
  // cout<< s<< endl;
  if (s == "ABC")
    return true;
  if (s.size() <= 3)
    return false;
  int n = s.size();
  char c;
  if (s.substr(0, 3) == "ABC")
    c = 'A';
  else if (s.substr(n - 3, 3) == "ABC")
    c = 'C';
  else
    c = 'B';
  string t = "";
  rep(i, n) {
    if (s.substr(i, 3) == "ABC") {
      t += c;
      i += 2;
    } else {
      t += s[i];
    }
  }
  string u = "";
  int m = t.size();
  rep(i, m) {
    if (t[i] == c) {
      u += "ABC";
    } else {
      u += t[i];
    }
  }
  if (s == u)
    return f(t);
  else
    return false;
}

int main() {

  string s;
  cin >> s;

  bool ok = f(s);
  if (ok)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;

  // cout<< ret<< endl;

  return 0;
}


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

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

bool f(const string &s) {
  // cout<< s<< endl;
  if (s == "ABC")
    return true;
  if (s.size() <= 3)
    return false;
  int n = s.size();
  char c;
  if (s.substr(0, 3) == "ABC")
    c = 'A';
  else if (s.substr(n - 3, 3) == "ABC")
    c = 'C';
  else
    c = 'B';
  string t = "";
  rep(i, n) {
    if (s.substr(i, 3) == "ABC") {
      t += c;
      i += 2;
    } else {
      t += s[i];
    }
  }
  if (s.size() == t.size())
    return false;
  string u = "";
  int m = t.size();
  rep(i, m) {
    if (t[i] == c) {
      u += "ABC";
    } else {
      u += t[i];
    }
  }
  if (s == u)
    return f(t);
  else
    return false;
}

int main() {

  string s;
  cin >> s;

  bool ok = f(s);
  if (ok)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;

  // cout<< ret<< endl;

  return 0;
}
Problem p01811 (C++)

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

int main() {
  string s;
  cin >> s;
  while (s != "ABC") {
    string t = "";
    for (int i = 0; i < s.length() - 2; i++) {
      if (s.substr(i, 3) == "ABC") {
        for (int j = 0; j < 3; j++)
          s[i + j] = ' ';
      }
    }
    bool a = false, b = false, c = false;
    for (int i = 0; i < s.length(); i++) {
      if (s[i] == 'A')
        a = true;
      else if (s[i] == 'B')
        b = true;
      else if (s[i] == 'C')
        c = true;
    }
    char cha = ' ';
    if (a && b)
      cha = 'C';
    if (b && c)
      cha = 'A';
    if (c && a)
      cha = 'B';
    if (cha == ' ' || (a && b && c)) {
      cout << "No" << endl;
      return 0;
    }
    for (int i = 0; i < s.length(); i++) {
      if (s[i] == ' ') {
        t = t + cha;
        i += 2;
      } else
        t = t + s[i];
    }
    if (t == s) {
      cout << "No" << endl;
      return 0;
    }
    s = t;
  }
  cout << "Yes" << endl;
  return 0;
}

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

int main() {
  string s;
  cin >> s;
  if (s.length() < 3) {
    cout << "No" << endl;
    return 0;
  }
  while (s != "ABC") {
    string t = "";
    for (int i = 0; i < s.length() - 2; i++) {
      if (s.substr(i, 3) == "ABC") {
        for (int j = 0; j < 3; j++)
          s[i + j] = ' ';
      }
    }
    bool a = false, b = false, c = false;
    for (int i = 0; i < s.length(); i++) {
      if (s[i] == 'A')
        a = true;
      else if (s[i] == 'B')
        b = true;
      else if (s[i] == 'C')
        c = true;
    }
    char cha = ' ';
    if (a && b)
      cha = 'C';
    if (b && c)
      cha = 'A';
    if (c && a)
      cha = 'B';
    if (cha == ' ' || (a && b && c)) {
      cout << "No" << endl;
      return 0;
    }
    for (int i = 0; i < s.length(); i++) {
      if (s[i] == ' ') {
        t = t + cha;
        i += 2;
      } else
        t = t + s[i];
    }
    if (t == s) {
      cout << "No" << endl;
      return 0;
    }
    s = t;
  }
  cout << "Yes" << endl;
  return 0;
}
Problem p01811 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <map>
#include <string>
using namespace std;

int main() {
  string S;
  cin >> S;
  map<char, int> M;
  while (S.size() > 3) {
    for (char c = 'A'; c < 'D'; ++c)
      M[c] = 0;
    int abc = 0;
    int l = S.size();
    for (int i = 0; i < l; ++i) {
      M[S[i]] += 1;
      if (S[i] == 'A' && i < l - 2 && S[i + 1] == 'B' && S[i + 2] == 'C')
        abc += 1;
    }

    char s = 'A';
    bool f = true;
    for (; s < 'D'; ++s) {
      if (!(M[s] - abc)) {
        f = false;
        break;
      }
    }
    if (f) {
      cout << "No" << endl;
      return 0;
    }
    string T = S;
    S = "";
    for (int i = 0; i < l; ++i) {
      if (i < l - 2 && T[i] == 'A' && T[i + 1] == 'B' && T[i + 2] == 'C') {
        S += s;
        i += 2;
      } else
        S += T[i];
    }
  }
  if (S == "ABC")
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  ;
  return 0;
}

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

int main() {
  string S;
  cin >> S;
  map<char, int> M;
  while (S.size() > 3) {
    if (!(S.size() % 2)) {
      cout << "No" << endl;
      return 0;
    }
    for (char c = 'A'; c < 'D'; ++c)
      M[c] = 0;
    int abc = 0;
    int l = S.size();
    for (int i = 0; i < l; ++i) {
      M[S[i]] += 1;
      if (S[i] == 'A' && i < l - 2 && S[i + 1] == 'B' && S[i + 2] == 'C')
        abc += 1;
    }

    char s = 'A';
    bool f = true;
    for (; s < 'D'; ++s) {
      if (!(M[s] - abc)) {
        f = false;
        break;
      }
    }
    if (f) {
      cout << "No" << endl;
      return 0;
    }
    string T = S;
    S = "";
    for (int i = 0; i < l; ++i) {
      if (i < l - 2 && T[i] == 'A' && T[i + 1] == 'B' && T[i + 2] == 'C') {
        S += s;
        i += 2;
      } else
        S += T[i];
    }
  }
  if (S == "ABC")
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  ;
  return 0;
}
Problem p01812 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int x[101][101];
vector<int> D;
int n, m, p, dp[100000];
queue<int> Q;
int main() {
  cin >> n >> m >> p;
  for (int i = 0; i < m; i++) {
    int r;
    cin >> r;
    D.push_back(r);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < p; j++)
      cin >> x[i][j];
  }
  for (int i = 1; i < 100000; i++)
    dp[i] = 998244353;
  Q.push(0);
  while (!Q.empty()) {
    int i = Q.front();
    int bit[16];
    for (int j = 0; j < m; j++)
      bit[j] = (i / (1 << j)) % 2;

    for (int o = 0; o < p; o++) {
      int bit2[16];
      for (int k = 0; k < m; k++)
        bit2[k] = 1;
      for (int j = 0; j < m; j++) {
        if (bit[j] == 0) {
          int to = x[D[j]][o];
          for (int l = 0; l < m; l++) {
            if (D[l] == to)
              bit2[l] = 0;
          }
        }
      }
      int cnt = 0;
      for (int j = 0; j < m; j++)
        cnt += (1 << j) * bit2[j];
      if (dp[cnt] == 998244353) {
        dp[cnt] = min(dp[cnt], dp[i] + 1);
        Q.push(cnt);
      }
    }
  }
  cout << dp[(1 << m) - 1] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int x[101][101];
vector<int> D;
int n, m, p, dp[100000];
queue<int> Q;
int main() {
  cin >> n >> m >> p;
  for (int i = 0; i < m; i++) {
    int r;
    cin >> r;
    D.push_back(r);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < p; j++)
      cin >> x[i][j];
  }
  for (int i = 1; i < 100000; i++)
    dp[i] = 998244353;
  Q.push(0);
  while (!Q.empty()) {
    int i = Q.front();
    Q.pop();
    int bit[16];
    for (int j = 0; j < m; j++)
      bit[j] = (i / (1 << j)) % 2;

    for (int o = 0; o < p; o++) {
      int bit2[16];
      for (int k = 0; k < m; k++)
        bit2[k] = 1;
      for (int j = 0; j < m; j++) {
        if (bit[j] == 0) {
          int to = x[D[j]][o];
          for (int l = 0; l < m; l++) {
            if (D[l] == to)
              bit2[l] = 0;
          }
        }
      }
      int cnt = 0;
      for (int j = 0; j < m; j++)
        cnt += (1 << j) * bit2[j];
      if (dp[cnt] == 998244353) {
        dp[cnt] = min(dp[cnt], dp[i] + 1);
        Q.push(cnt);
      }
    }
  }
  cout << dp[(1 << m) - 1] << endl;
  return 0;
}
Problem p01812 (C++)

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

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> d(m);
  map<int, int> mp;
  int v[101][101] = {};
  for (int i = 0; i < m; i++) {
    cin >> d[i];
    d[i]--;
    mp[d[i]] = i;
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < k; j++) {
      cin >> v[i][j];
      v[i][j]--;
    }
  }
  int bits = (1 << m) - 1;
  vector<bool> flag((1 << m) + 1);
  vector<int> dst((1 << m) + 1);
  queue<int> q;
  q.push(bits);
  flag[bits] = 1;
  dst[bits] = 0;
  while (!q.empty()) {
    auto x = q.front();
    q.pop();
    for (int i = 0; i < k; i++) {
      int sa = 0;
      for (int j = 0; j < m; j++) {
        if (x & (1 << j)) {
          if (mp.find(v[d[j]][i]) != mp.end()) {
            int y = mp[v[d[j]][i]];
            sa += 1 << y;
          }
        }
      }
      if (!flag[sa]) {
        flag[sa] = 1;
        dst[sa] = dst[x] + 1;
        q.push(sa);
      }
    }
  }
  cout << dst[0] << endl;
  return 0;
}


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

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> d(m);
  map<int, int> mp;
  int v[101][101] = {};
  for (int i = 0; i < m; i++) {
    cin >> d[i];
    d[i]--;
    mp[d[i]] = i;
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < k; j++) {
      cin >> v[i][j];
      v[i][j]--;
    }
  }
  int bits = (1 << m) - 1;
  vector<bool> flag((1 << m) + 1);
  vector<int> dst((1 << m) + 1);
  queue<int> q;
  q.push(bits);
  flag[bits] = 1;
  dst[bits] = 0;
  while (!q.empty()) {
    auto x = q.front();
    q.pop();
    for (int i = 0; i < k; i++) {
      int sa = 0;
      for (int j = 0; j < m; j++) {
        if (x & (1 << j)) {
          if (mp.find(v[d[j]][i]) != mp.end()) {
            int y = mp[v[d[j]][i]];
            if (!(sa & 1 << y))
              sa += 1 << y;
          }
        }
      }
      if (!flag[sa]) {
        flag[sa] = 1;
        dst[sa] = dst[x] + 1;
        q.push(sa);
      }
    }
  }
  cout << dst[0] << endl;
  return 0;
}
Problem p01812 (C++)

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

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> d(m);
  map<int, int> dark_map;
  for (int i = 0; i < m; i++) {
    cin >> d[i];
    d[i]--;
    dark_map[d[i]] = i;
  }
  vector<vector<int>> v(n, vector<int>(k));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < k; j++) {
      cin >> v[i][j];
      v[i][j]--;
    }
  vector<vector<int>> v2(m);
  for (int i = 0; i < m; i++) {
    v2[i] = v[d[i]];
  }
  const int INF = 1e9;
  vector<int> dp(1 << m, INF);
  int first_p = (1 << m) - 1;
  dp[first_p] = 0;
  pair<int, int> state = make_pair(0, first_p);
  priority_queue<pair<int, int>> que;
  que.push(state);

  while (!que.empty()) {
    state = que.top();
    que.pop();
    int p = state.second;
    int sc = state.first;
    if (dp[p] < sc)
      continue;

    for (int i = 0; i < k; i++) {
      int top = 0;
      for (int j = 0; j < m; j++) {
        if (p & (1 << j)) {
          if (dark_map.count(v2[j][i])) {
            top |= (1 << (dark_map[v2[j][i]]));
          }
        }
      }
      if (dp[top] > sc + 1) {
        dp[top] = sc + 1;
        que.push(make_pair(sc + 1, top));
      }
    }
  }
  cout << dp[0] << endl;
  return 0;
}

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

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  vector<int> d(m);
  map<int, int> dark_map;
  for (int i = 0; i < m; i++) {
    cin >> d[i];
    d[i]--;
    dark_map[d[i]] = i;
  }
  vector<vector<int>> v(n, vector<int>(k));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < k; j++) {
      cin >> v[i][j];
      v[i][j]--;
    }
  vector<vector<int>> v2(m);
  for (int i = 0; i < m; i++) {
    v2[i] = v[d[i]];
  }
  const int INF = 1e9;
  vector<int> dp(1 << m, INF);
  int first_p = (1 << m) - 1;
  dp[first_p] = 0;
  pair<int, int> state = make_pair(0, first_p);
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      que;
  que.push(state);

  while (!que.empty()) {
    state = que.top();
    que.pop();
    int p = state.second;
    int sc = state.first;
    if (dp[p] < sc)
      continue;

    for (int i = 0; i < k; i++) {
      int top = 0;
      for (int j = 0; j < m; j++) {
        if (p & (1 << j)) {
          if (dark_map.count(v2[j][i])) {
            top |= (1 << (dark_map[v2[j][i]]));
          }
        }
      }
      if (dp[top] > sc + 1) {
        dp[top] = sc + 1;
        que.push(make_pair(sc + 1, top));
      }
    }
  }
  cout << dp[0] << endl;
  return 0;
}
Problem p01829 (C++)

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

int main() {
  string S, res;
  cin >> S;
  ll n = S.size(), md = -1, MAX = 1, s = stoi(S);
  vector<int> digit = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  for (int i = 0; i < n; i++) {
    MAX *= 10;
  }
  do {
    ll val = 0;
    string v;
    for (int i = 0; i < n; i++) {
      val = val * 10 + digit[i];
      v.push_back('0' + digit[i]);
    }
    ll d = min(MAX - abs(s - val), abs(s - val));
    if (d > md) {
      md = d;
      res = v;
    }
  } while (next_permutation(digit.begin(), digit.end()));
  cout << res << endl;
  return 0;
}

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

int main() {
  string S, res;
  cin >> S;
  ll n = S.size(), md = -1, MAX = 1, s = stoll(S);
  vector<int> digit = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  for (int i = 0; i < n; i++) {
    MAX *= 10;
  }
  do {
    ll val = 0;
    string v;
    for (int i = 0; i < n; i++) {
      val = val * 10 + digit[i];
      v.push_back('0' + digit[i]);
    }
    ll d = min(MAX - abs(s - val), abs(s - val));
    if (d > md) {
      md = d;
      res = v;
    }
  } while (next_permutation(digit.begin(), digit.end()));
  cout << res << endl;
  return 0;
}
Problem p01829 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>

using namespace std;
using lint = long long;

lint ten = 1;
lint M;
lint maxdiff = 0, ans = 0;

void dfs(int N, int used, lint num) {
  if (N == 0) {
    lint diff = min(abs(num - M), ten - abs(num - M));
    if (diff > maxdiff || (diff == maxdiff && num < ans)) {
      ans = num;
      maxdiff = diff;
    }
  } else {
    for (int d = 0; d < 10; ++d) {
      if ((used >> d) & 1)
        continue;
      dfs(N - 1, used | (1 << d), num * 10 + d);
    }
  }
}

int main() {
  string S;
  cin >> S;
  int N = S.size();
  for (int i = 0; i < N; ++i)
    ten *= 10;
  M = stoi(S);

  dfs(N, 0, 0);
  cout << right << setw(N) << setfill('0') << ans << endl;
  return 0;
}


Fixed submission:
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>

using namespace std;
using lint = long long;

lint ten = 1;
lint M;
lint maxdiff = 0, ans = 0;

void dfs(int N, int used, lint num) {
  if (N == 0) {
    lint diff = min(abs(num - M), ten - abs(num - M));
    if (diff > maxdiff || (diff == maxdiff && num < ans)) {
      ans = num;
      maxdiff = diff;
    }
  } else {
    for (int d = 0; d < 10; ++d) {
      if ((used >> d) & 1)
        continue;
      dfs(N - 1, used | (1 << d), num * 10 + d);
    }
  }
}

int main() {
  string S;
  cin >> S;
  int N = S.size();
  for (int i = 0; i < N; ++i)
    ten *= 10;
  M = stoll(S);

  dfs(N, 0, 0);
  cout << right << setw(N) << setfill('0') << ans << endl;
  return 0;
}
Problem p01840 (C++)

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

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define MP make_pair
#define PB push_back
#define MOD 1000000007
#define INF (1LL << 30)
#define LLINF (1LL << 60)
#define PI 3.14159265359
#define EPS 1e-12
#define int ll

signed main(void) {
  int n, m, t, a[105], b[10010];
  cin >> n >> m >> t;
  REP(i, n) {
    cin >> a[i];
    b[a[i] - m]++;
    b[a[i] + m]--;
  }

  FOR(i, 1, t) b[i] += b[i - 1];
  int ret = 0;
  REP(i, t) if (b[i] == 0) ret++;
  cout << ret << endl;

  return 0;
}

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

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define MP make_pair
#define PB push_back
#define MOD 1000000007
#define INF (1LL << 30)
#define LLINF (1LL << 60)
#define PI 3.14159265359
#define EPS 1e-12
#define int ll

signed main(void) {
  int n, m, t, a[105], b[10010];
  cin >> n >> m >> t;
  REP(i, n) {
    cin >> a[i];
    b[max((ll)0, a[i] - m)]++;
    b[min(t, a[i] + m)]--;
  }

  FOR(i, 1, t) b[i] += b[i - 1];
  int ret = 0;
  REP(i, t) if (b[i] == 0) ret++;
  cout << ret << endl;

  return 0;
}
Problem p01840 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define PB push_back
#define MP make_pair
#define ALL(a) (a).begin(), (a).end()
#define ll long long
using namespace std;
int main() {
  int n, m, t, a;
  int time[10100];
  memset(time, 0, sizeof(time));
  cin >> n >> m >> t;
  REP(i, n) {
    cin >> a;
    time[a - m]++;
    time[a + m]--;
  }
  REP(i, t) time[i + 1] += time[i];
  int ans = 0;
  REP(i, t) if (time[i] == 0) ans++;
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define PB push_back
#define MP make_pair
#define ALL(a) (a).begin(), (a).end()
#define ll long long
using namespace std;
int main() {
  int n, m, t, a;
  int time[10100];
  memset(time, 0, sizeof(time));
  cin >> n >> m >> t;
  REP(i, n) {
    cin >> a;
    time[a - m]++;
    time[min(a + m, 10010)]--;
  }
  REP(i, t) time[i + 1] += time[i];
  int ans = 0;
  REP(i, t) if (time[i] == 0) ans++;
  cout << ans << endl;
}
Problem p01840 (C++)

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

int main() {

  int N, M, T, t[100];
  int imos[10001] = {0};
  cin >> N >> M >> T;
  for (int i = 0; i < N; i++) {
    int t;
    cin >> t;
    imos[t - M]++, imos[t + M]--;
  }
  for (int i = 1; i <= T; i++) {
    imos[i] += imos[i - 1];
  }

  int cnt = 0;
  for (int i = 0; i < T; i++) {
    if (!imos[i])
      cnt++;
  }
  cout << cnt << endl;
}

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

int main() {

  int N, M, T, t[100];
  int imos[30000] = {0};
  cin >> N >> M >> T;
  for (int i = 0; i < N; i++) {
    int t;
    cin >> t;
    imos[t - M]++, imos[t + M]--;
  }
  for (int i = 1; i <= T; i++) {
    imos[i] += imos[i - 1];
  }

  int cnt = 0;
  for (int i = 0; i < T; i++) {
    if (!imos[i])
      cnt++;
  }
  cout << cnt << endl;
}
Problem p01840 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = (1LL << 31) - 1;
const ll MOD = 1e9 + 7;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, N) for (int i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1
#define MAX_N 100100 * 3

int T[10100];
int n, m, t;

int main() {
  cin >> n >> m >> t;
  rep(i, n) {
    int tt;
    cin >> tt;
    T[tt - m]++;
    T[tt + m]--;
  }
  rep(i, t) { T[i + 1] += T[i]; }
  int ans = 0;
  rep(i, t) {
    // cout << i << " " << T[i] << endl;
    ans += (T[i] == 0);
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = (1LL << 31) - 1;
const ll MOD = 1e9 + 7;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, N) for (int i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1
#define MAX_N 100100 * 3

int T[20100];
int n, m, t;

int main() {
  cin >> n >> m >> t;
  rep(i, n) {
    int tt;
    cin >> tt;
    T[tt - m]++;
    T[tt + m]--;
  }
  rep(i, t) { T[i + 1] += T[i]; }
  int ans = 0;
  rep(i, t) {
    // cout << i << " " << T[i] << endl;
    ans += (T[i] == 0);
  }
  cout << ans << endl;
}
Problem p01840 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF 999999999
#define mod 1000000007

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define rrep(i, n) for (int i = (n); i >= 0; i--)
#define REP(i, a, b) for (int i = (a); i < int(b); i++)
#define all(x) (x).begin(), x.end()
#define pb push_back
#define MOD(x) (x % (mod))
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pi;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
bool debug = false;
/*---------------------------------------------------*/

int main() {
  int n, m, t;
  int a;
  int time[10005];
  rep(i, 10005) time[i] = 0;
  cin >> n >> m >> t;
  rep(i, n) {
    cin >> a;
    time[a - m] += 1;
    time[a + m] -= 1;
  }
  for (int i = 1; i <= t; i++)
    time[i] += time[i - 1];
  int ans = 0;
  for (int i = 0; i < t; i++)
    if (time[i] == 0)
      ans++;
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF 999999999
#define mod 1000000007

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define rrep(i, n) for (int i = (n); i >= 0; i--)
#define REP(i, a, b) for (int i = (a); i < int(b); i++)
#define all(x) (x).begin(), x.end()
#define pb push_back
#define MOD(x) (x % (mod))
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pi;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
bool debug = false;
/*---------------------------------------------------*/

int main() {
  int n, m, t;
  int a;
  int time[20005];
  rep(i, 20005) time[i] = 0;
  cin >> n >> m >> t;
  rep(i, n) {
    cin >> a;
    time[a - m] += 1;
    time[a + m] -= 1;
  }
  for (int i = 1; i <= t; i++)
    time[i] += time[i - 1];
  int ans = 0;
  for (int i = 0; i < t; i++)
    if (time[i] == 0)
      ans++;
  cout << ans << endl;

  return 0;
}
Problem p01840 (C++)

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

signed main() {
  bool a[10001]{};
  int b, c, d;
  cin >> b >> c >> d;
  for (int e = 0; e < b; e++) {
    int f;
    cin >> f;
    for (int g = f - c; g < f + c; g++) {
      a[g] = true;
    }
  }
  int s = 0;
  for (int h = 0; h < d; h++) {
    if (!a[h])
      s++;
  }
  cout << s << endl;
}

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

signed main() {
  bool a[30000]{};
  int b, c, d;
  cin >> b >> c >> d;
  for (int e = 0; e < b; e++) {
    int f;
    cin >> f;
    for (int g = f - c; g < f + c; g++) {
      a[g] = true;
    }
  }
  int s = 0;
  for (int h = 0; h < d; h++) {
    if (!a[h])
      s++;
  }
  cout << s << endl;
}
Problem p01840 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int a[10000], n, m, t, p;
int main() {
  cin >> n >> m >> t;
  for (int i = 0; i < n; i++) {
    cin >> p;
    a[p - m]++;
    a[p + m]--;
  }
  for (int i = 1; i < t; i++)
    a[i] += a[i - 1];
  int cnt = 0;
  for (int i = 0; i < t; i++) {
    if (a[i] == 0)
      cnt++;
  }
  cout << cnt << endl;
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int a[100000], n, m, t, p;
int main() {
  cin >> n >> m >> t;
  for (int i = 0; i < n; i++) {
    cin >> p;
    a[p - m]++;
    a[p + m]--;
  }
  for (int i = 1; i < t; i++)
    a[i] += a[i - 1];
  int cnt = 0;
  for (int i = 0; i < t; i++) {
    if (a[i] == 0)
      cnt++;
  }
  cout << cnt << endl;
  return 0;
}