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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
priority_queue<int> Q[3];
int a[100010];

int F(int bef, int aft) {
  int cnt = 0;
  while (true) {
    int pnt = (3 + aft - bef) % 3;
    if (Q[pnt].empty())
      break;
    cnt++;
    Q[pnt].pop();
    if (!Q[0].empty()) {
      while (!Q[0].empty()) {
        cnt++;
        Q[0].pop();
      }
    }
    bef = aft;
    if (aft == 1)
      aft = 2;
    else if (aft == 2)
      aft = 1;
  }
  if (!Q[0].empty() || !Q[1].empty() || !Q[2].empty())
    cnt++;
  return cnt;
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++) {
    Q[a[i] % 3].push(a[i]);
  }
  int ans = F(0, 1);
  for (int i = 0; i < 3; i++) {
    while (!Q[i].empty())
      Q[i].pop();
  }
  for (int i = 0; i < n; i++) {
    Q[a[i] % 3].push(a[i]);
  }
  ans = max(ans, F(0, 2));
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
priority_queue<int> Q[3];
int a[500010];

int F(int bef, int aft) {
  int cnt = 0;
  while (true) {
    int pnt = (3 + aft - bef) % 3;
    if (Q[pnt].empty())
      break;
    cnt++;
    Q[pnt].pop();
    if (!Q[0].empty()) {
      while (!Q[0].empty()) {
        cnt++;
        Q[0].pop();
      }
    }
    bef = aft;
    if (aft == 1)
      aft = 2;
    else if (aft == 2)
      aft = 1;
  }
  if (!Q[0].empty() || !Q[1].empty() || !Q[2].empty())
    cnt++;
  return cnt;
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++) {
    Q[a[i] % 3].push(a[i]);
  }
  int ans = F(0, 1);
  for (int i = 0; i < 3; i++) {
    while (!Q[i].empty())
      Q[i].pop();
  }
  for (int i = 0; i < n; i++) {
    Q[a[i] % 3].push(a[i]);
  }
  ans = max(ans, F(0, 2));
  cout << ans << endl;
  return 0;
}
Problem p01900 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, a) for (int i = 0; i < a; i++)
#define s second
#define f first
using namespace std;

int main(void) {
  int n, ary[3] = {0}, in, ret = 0;
  cin >> n;
  rep(i, n) {
    cin >> in;
    ary[in % 3]++;
  }
  if (ary[1] || ary[2])
    ret = ary[0];
  int h = 1, now = 0;
  ;
  while (true) {
    if (h == 2) { // h = 2
      int use = min(2, ary[2]);
      ret += use;
      ary[2] -= use;
      if (ary[1] != 0)
        h = 1;
      now = (use * 2 + now) % 3;
      if (now == 0)
        break;
    } else { // h = 1
      int use = min(2, ary[1]);
      ret += use;
      ary[1] -= use;
      if (ary[2] != 0)
        h = 2;
      now = (now + use) % 3;
      if (now == 0)
        break;
    }
  }
  cout << (ret == 0 && ary[0] ? 1 : ret) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a) for (int i = 0; i < a; i++)
#define s second
#define f first
using namespace std;

int main(void) {
  int n, ary[3] = {0}, in, ret = 0;
  cin >> n;
  rep(i, n) {
    cin >> in;
    ary[in % 3]++;
  }
  if (ary[1] || ary[2])
    ret = ary[0];
  int mi = min(ary[1], ary[2]);
  int diff = max(ary[1], ary[2]) - mi;
  // cout<<diff<<endl;
  ret = ret + mi * 2 + min(3, diff);
  // cout<< min(3,diff)<<endl;

  cout << (ret == 0 && ary[0] ? 1 : ret) << endl;
}
Problem p01901 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << #x << " = " << x << endl;

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 10000

int main() {
  ll t, s[SIZE], e[SIZE], r[SIZE] = {};
  int n;

  scanf("%lld%d", &t, &n);

  for (int i = 0; i < n; i++) {
    scanf("%lld%lld", s + i, e + i);
  }

  for (int i = 1; i < n; i++) {
    r[i] = s[i] - e[i - 1] + r[i - 1];
  }

  ll ans = 0;

  for (int i = 0; i < n; i++) {

    ll *it = upper_bound(s, s + n, s[i] + t);

    if (it == s) {
      ans = max(ans, min(t, e[0] - s[0]));
    } else {
      it--;

      if (s[i] + t >= e[it - s])
        ans = max(ans, e[it - s] - s[i] - (r[it - s] - r[i]));
      else
        ans = max(ans, t - (r[it - s] - r[i]));
    }
  }

  printf("%lld\n", ans);

  return 0;
}

Fixed submission:
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << #x << " = " << x << endl;

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 100010

int main() {
  ll t, s[SIZE], e[SIZE], r[SIZE] = {};
  int n;

  scanf("%lld%d", &t, &n);

  for (int i = 0; i < n; i++) {
    scanf("%lld%lld", s + i, e + i);
  }

  for (int i = 1; i < n; i++) {
    r[i] = s[i] - e[i - 1] + r[i - 1];
  }

  ll ans = 0;

  for (int i = 0; i < n; i++) {

    ll *it = upper_bound(s, s + n, s[i] + t);

    if (it == s) {
      ans = max(ans, min(t, e[0] - s[0]));
    } else {
      it--;

      if (s[i] + t >= e[it - s])
        ans = max(ans, e[it - s] - s[i] - (r[it - s] - r[i]));
      else
        ans = max(ans, t - (r[it - s] - r[i]));
    }
  }

  printf("%lld\n", ans);

  return 0;
}
Problem p01905 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define mp(a, b) make_pair((a), (b))
#define pb(a) push_back((a))
#define all(x) (x).begin(), (x).end()
#define uniq(x) sort(all(x)), (x).erase(unique(all(x)), end(x))
#define fi first
#define se second
#define dbg(x) cout << #x " = " << ((x)) << endl
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

#define INF 2147483600

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> vec(m);
  rep(i, m) cin >> vec[i];

  vector<bool> b(n, true);
  rep(i, m) b[vec[i] - 1] = false;

  int ans = 0;
  for (int d = 1; d < n; d *= 2) {
    for (int i = 0; i < n; i += d * 2) {
      if (b[i] && b[i + d])
        ans++;
      else if (b[i] || b[i + d])
        b[i] = true;
    }
    //    dbg(b);
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define mp(a, b) make_pair((a), (b))
#define pb(a) push_back((a))
#define all(x) (x).begin(), (x).end()
#define uniq(x) sort(all(x)), (x).erase(unique(all(x)), end(x))
#define fi first
#define se second
#define dbg(x) cout << #x " = " << ((x)) << endl
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

#define INF 2147483600

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> vec(m);
  rep(i, m) cin >> vec[i];

  vector<bool> b(n, true);
  rep(i, m) b[vec[i]] = false;

  int ans = 0;
  for (int d = 1; d < n; d *= 2) {
    for (int i = 0; i < n; i += d * 2) {
      if (b[i] && b[i + d])
        ans++;
      else if (b[i] || b[i + d])
        b[i] = true;
    }
    //    dbg(b);
  }

  cout << ans << endl;

  return 0;
}
Problem p01906 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>

const int MAX_N = 1000;
int N, M;
int A[MAX_N];

int gcf(int a, int b);

int ans = 0;
int main() {
  scanf("%d %d", &N, &M);
  for (int i = 0; i < N; i++) {
    scanf("%d", &A[i]);
  }

  if (N % M == 0) {
    for (int i = 0; i < N; i += M) {
      int max = 0, min = 1000;
      for (int j = 0; j < M; j++) {
        if (A[i + j] < min)
          min = A[i + j];
        if (A[i + j] > max)
          max = A[i + j];
      }
      ans += max - min;
    }
  } else {
    int gnum = 0;
    int gcfn = gcf(N, M);
    gnum = N / gcfn;
    for (int i = 0; i < gnum; i++) {
      int max = 0, min = 1000;
      for (int j = 0; j < M; j++) {
        int dum = 0;
        if (i * M - i * N + j < 0)
          dum = M + 1;
        if (A[i * M - i * N + j + dum] < min)
          min = A[i * M - i * N + j + dum];
        if (A[i * M - i * N + j + dum] > max)
          max = A[i * M - i * N + j + dum];
      }
      ans += max - min;
    }
  }

  printf("%d\n", ans);

  return 0;
}

int gcf(int a, int b) {
  if (a < b) {
    int dum = a;
    a = b;
    b = dum;
  }
  if (a % b == 1)
    return 1;
  else if (a % b == 0)
    return b;
  else
    gcf(b, a % b);
}

Fixed submission:
#include <cstdio>

const int MAX_N = 1000;
int N, M;
int A[MAX_N];

int gcf(int a, int b);

int ans = 0;
int main() {
  scanf("%d %d", &N, &M);
  for (int i = 0; i < N; i++) {
    scanf("%d", &A[i]);
  }

  if (N % M == 0) {
    for (int i = 0; i < N; i += M) {
      int max = 0, min = 1000;
      for (int j = 0; j < M; j++) {
        if (A[i + j] < min)
          min = A[i + j];
        if (A[i + j] > max)
          max = A[i + j];
      }
      ans += max - min;
    }
  } else {
    int gnum = 0;
    int gcfn = gcf(N, M);
    gnum = N / gcfn;
    for (int i = 0; i < gnum; i++) {
      int max = 0, min = 1000;
      for (int j = 0; j < M; j++) {
        int k = (i * M + j) % (N);
        if (A[k] < min)
          min = A[k];
        if (A[k] > max)
          max = A[k];
      }
      ans += max - min;
    }
  }

  printf("%d\n", ans);

  return 0;
}

int gcf(int a, int b) {
  if (a < b) {
    int dum = a;
    a = b;
    b = dum;
  }
  if (a % b == 1)
    return 1;
  else if (a % b == 0)
    return b;
  else
    gcf(b, a % b);
}
Problem p01906 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#define lol(i, n) for (int i = 0; i < n; i++)
#define mod 1000000007
typedef long long ll;
using namespace std;
int a[110];
pair<int, int> F(int left, int right) {
  int p = -1, q = mod;
  for (int i = left; i < right; i++) {
    p = max(p, a[i]);
    q = min(q, a[i]);
  }
  return make_pair(p, q);
}
int main() {
  int n, m;
  cin >> n >> m;
  lol(i, n) cin >> a[i];
  if (n == m) {
    pair<int, int> tmp = F(0, n);
    cout << tmp.first - tmp.second << endl;
    return 0;
  }
  int left = 0, right = m;
  int ans = 0;
  while (true) {
    int p, q;
    if (left < right) {
      pair<int, int> tmp = F(left, right);
      p = tmp.first, q = tmp.second;
    } else {
      pair<int, int> tmp = F(0, right);
      p = tmp.first, q = tmp.second;
      tmp = F(left, n);
      p = max(p, tmp.first);
      q = min(q, tmp.second);
    }
    ans += p - q;
    left = right % n;
    right = (right + m) % n;
    if (right <= 0)
      right += n;
    if (left == 0)
      break;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#define lol(i, n) for (int i = 0; i < n; i++)
#define mod 1000000007
typedef long long ll;
using namespace std;
int a[1100];
pair<int, int> F(int left, int right) {
  int p = -1, q = mod;
  for (int i = left; i < right; i++) {
    p = max(p, a[i]);
    q = min(q, a[i]);
  }
  return make_pair(p, q);
}
int main() {
  int n, m;
  cin >> n >> m;
  lol(i, n) cin >> a[i];
  if (n == m) {
    pair<int, int> tmp = F(0, n);
    cout << tmp.first - tmp.second << endl;
    return 0;
  }
  int left = 0, right = m;
  int ans = 0;
  while (true) {
    int p, q;
    if (left < right) {
      pair<int, int> tmp = F(left, right);
      p = tmp.first, q = tmp.second;
    } else {
      pair<int, int> tmp = F(0, right);
      p = tmp.first, q = tmp.second;
      tmp = F(left, n);
      p = max(p, tmp.first);
      q = min(q, tmp.second);
    }
    ans += p - q;
    left = right % n;
    right = (right + m) % n;
    if (right <= 0)
      right += n;
    if (left == 0)
      break;
  }
  cout << ans << endl;
  return 0;
}
Problem p01907 (C++)

Buggy submission (status: Runtime Error):
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

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

#include <queue>
#include <set>

using namespace std;

vector<pair<int, int>> g[10000];
double pp[100000];
double p;

double dfs(int pos, double prob = 1, int prev = -1) {
  ::pp[pos] = prob;

  double ans = 0;
  REP(i, g[pos].size()) {
    const int next = g[pos][i].first;
    const int c = g[pos][i].second;

    if (next != prev) {
      ans += p * (c + dfs(next, prob * p, pos));
    }
  }

  return ans;
}

int main() {
  ::p = getDouble();
  const int n = getInt();

  REP(i, n - 1) {
    const int a = getInt() - 1;
    const int b = getInt() - 1;
    const int c = getInt();

    g[a].push_back(make_pair(b, c));
    g[b].push_back(make_pair(a, c));
  }

  const double t = dfs(0);

  double ans = t;
  REP(i, n) { ans += pp[i] * t; }

  printf("%.10f\n", ans);
}

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

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

#include <queue>
#include <set>

using namespace std;

vector<pair<int, int>> g[100000];
double pp[100000];
double p;

double dfs(int pos, double prob = 1, int prev = -1) {
  ::pp[pos] = prob;

  double ans = 0;
  REP(i, g[pos].size()) {
    const int next = g[pos][i].first;
    const int c = g[pos][i].second;

    if (next != prev) {
      ans += p * (c + dfs(next, prob * p, pos));
    }
  }

  return ans;
}

int main() {
  ::p = getDouble();
  const int n = getInt();

  REP(i, n - 1) {
    const int a = getInt() - 1;
    const int b = getInt() - 1;
    const int c = getInt();

    g[a].push_back(make_pair(b, c));
    g[b].push_back(make_pair(a, c));
  }

  const double t = dfs(0);

  double ans = t;
  REP(i, n) { ans += pp[i] * t; }

  printf("%.10f\n", ans);
}
Problem p01907 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <math.h>
#include <vector>

using namespace std;

const int MAX_N = 10000;

struct edge {
  int to, cost;
};
vector<edge> tree[MAX_N];
bool flag[MAX_N] = {false};
int depth[MAX_N];
int s, t, c;
double sum = 0.0;
double ans = 0.0;
double p;
int N;

void dfs(int v, int d) {
  flag[v] = true;
  depth[v] = d;
  for (int i = 0; i < tree[v].size(); i++) {
    if (!flag[tree[v][i].to]) {
      sum += tree[v][i].cost * pow(p, (d + 1.0));
      dfs(tree[v][i].to, d + 1);
    }
  }
}

int main() {
  scanf("%lf %d", &p, &N);

  for (int i = 0; i < N - 1; i++) {
    scanf("%d %d %d", &s, &t, &c);
    s--;
    t--;
    edge e;
    e.to = t;
    e.cost = c;
    tree[s].push_back(e);
    e.to = s;
    tree[t].push_back(e);
  }

  dfs(0, 0);

  ans = 2 * sum;
  for (int i = 1; i < N; i++) {
    ans += sum * pow(p, depth[i]);
  }
  printf("%.10lf\n", ans);

  return 0;
}

Fixed submission:
#include <cstdio>
#include <math.h>
#include <vector>

using namespace std;

const int MAX_N = 100001;

struct edge {
  int to, cost;
};
vector<edge> tree[MAX_N];
bool flag[MAX_N] = {false};
int depth[MAX_N];
int s, t, c;
double sum = 0.0;
double ans = 0.0;
double p;
int N;

void dfs(int v, int d) {
  flag[v] = true;
  depth[v] = d;
  for (int i = 0; i < tree[v].size(); i++) {
    if (!flag[tree[v][i].to]) {
      sum += tree[v][i].cost * pow(p, (d + 1.0));
      dfs(tree[v][i].to, d + 1);
    }
  }
}

int main() {
  scanf("%lf %d", &p, &N);

  for (int i = 0; i < N - 1; i++) {
    scanf("%d %d %d", &s, &t, &c);
    s--;
    t--;
    edge e;
    e.to = t;
    e.cost = c;
    tree[s].push_back(e);
    e.to = s;
    tree[t].push_back(e);
  }

  dfs(0, 0);

  ans = 2 * sum;
  for (int i = 1; i < N; i++) {
    ans += sum * pow(p, depth[i]);
  }
  printf("%.10lf\n", ans);

  return 0;
}
Problem p01908 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

//// < "d:\d_download\visual studio
///2015\projects\programing_contest_c++\debug\a.txt" > "d:\d_download\visual
///studio 2015\projects\programing_contest_c++\debug\b.txt"

int main() {
  int N;
  cin >> N;
  set<string> dangers;
  for (int i = 0; i < N; ++i) {
    string st;
    cin >> st;
    for (int asize = 1; asize <= 4; ++asize) {
      for (int l = 0; l <= st.size() - asize; ++l) {
        dangers.emplace(st.substr(l, asize));
      }
    }
  }
  int num = 1;
  bool flag = false;
  string ans = "dd";
  for (int asize = 1; asize <= 4; ++asize) {
    num *= 26;
    for (int j = 0; j < num; ++j) {
      string pass(asize, ' ');
      int n(j);
      for (int i = 0; i < asize; ++i) {
        pass[asize - i - 1] = n % 26 + 'a';
        n /= 26;
      }
      if (dangers.find(pass) == dangers.end()) {
        ans = pass;
        flag = true;
        break;
      }
    }
    if (flag)
      break;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

//// < "d:\d_download\visual studio
///2015\projects\programing_contest_c++\debug\a.txt" > "d:\d_download\visual
///studio 2015\projects\programing_contest_c++\debug\b.txt"

int main() {
  int N;
  cin >> N;
  set<string> dangers;
  for (int i = 0; i < N; ++i) {
    string st;
    cin >> st;
    for (int asize = 1; asize <= 4; ++asize) {
      for (int l = 0; l <= int(st.size()) - asize; ++l) {
        dangers.emplace(st.substr(l, asize));
      }
    }
  }
  int num = 1;
  bool flag = false;
  string ans = "dd";
  for (int asize = 1; asize <= 4; ++asize) {
    num *= 26;
    for (int j = 0; j < num; ++j) {
      string pass(asize, ' ');
      int n(j);
      for (int i = 0; i < asize; ++i) {
        pass[asize - i - 1] = n % 26 + 'a';
        n /= 26;
      }
      if (dangers.find(pass) == dangers.end()) {
        ans = pass;
        flag = true;
        break;
      }
    }
    if (flag)
      break;
  }
  cout << ans << endl;
  return 0;
}
Problem p01908 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

const int MAX = 1000000;
bool a[MAX];

int main() {
  int n;
  cin >> n;
  vector<string> s(n);
  REP(i, n) cin >> s[i];
  FOR(l, 1, 5) {
    REP(i, n) {
      REP(j, s[i].length() - l + 1) {
        ll p = 0;
        REP(k, l) { p = 27 * p + s[i][j + k] - 'a' + 1; }
        if (p >= MAX)
          break;
        a[p] = true;
      }
    }
  }

  FOR(i, 1, MAX - 1) {
    if (a[i])
      continue;
    int x = i;
    string ans;
    bool f = true;
    while (x) {
      if (x % 27 == 0)
        f = false;
      char c = (x % 27) - 1 + 'a';
      ans = c + ans;
      x /= 27;
    }
    if (f) {
      cout << ans << endl;
      return 0;
    }
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

const int MAX = 1000000;
bool a[MAX];

int main() {
  int n;
  cin >> n;
  vector<string> s(n);
  REP(i, n) cin >> s[i];
  FOR(l, 1, 5) {
    REP(i, n) {
      if (s[i].length() < l)
        continue;
      REP(j, s[i].length() - l + 1) {
        ll p = 0;
        REP(k, l) { p = 27 * p + s[i][j + k] - 'a' + 1; }
        if (p >= MAX)
          break;
        a[p] = true;
      }
    }
  }

  FOR(i, 1, MAX - 1) {
    if (a[i])
      continue;
    int x = i;
    string ans;
    bool f = true;
    while (x) {
      if (x % 27 == 0)
        f = false;
      char c = (x % 27) - 1 + 'a';
      ans = c + ans;
      x /= 27;
    }
    if (f) {
      cout << ans << endl;
      return 0;
    }
  }

  return 0;
}
Problem p01908 (C++)

Buggy submission (status: Runtime Error):
#define _USE_MATH_DEFINES

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <unordered_map>
#include <vector>

using namespace std;

typedef pair<long long int, long long int> P;

long long int INF = 1e18;
long long int MOD = 1e9 + 7;

unordered_map<string, bool> m;
string str = "";

bool func(int pos, int num) {
  if (pos == num) {
    if (m.find(str) == m.end()) {
      cout << str << endl;
      return true;
    }
    return false;
  }
  for (int i = 0; i < 26; i++) {
    str[pos] = 'a' + i;
    if (func(pos + 1, num)) {
      return true;
    }
  }
  return false;
}

int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    string S;
    cin >> S;
    for (int i = 1; i <= 4; i++) {
      for (int j = 0; j < S.size() - i + 1; j++) {
        m[S.substr(j, i)] = true;
      }
    }
  }
  for (int i = 1; i <= 4; i++) {
    str += " ";
    if (func(0, i)) {
      return 0;
    }
  }
  return 0;
}


Fixed submission:
#define _USE_MATH_DEFINES

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <unordered_map>
#include <vector>

using namespace std;

typedef pair<long long int, long long int> P;

long long int INF = 1e18;
long long int MOD = 1e9 + 7;

unordered_map<string, bool> m;
string str = "";

bool func(int pos, int num) {
  if (pos == num) {
    if (m.find(str) == m.end()) {
      cout << str << endl;
      return true;
    }
    return false;
  }
  for (int i = 0; i < 26; i++) {
    str[pos] = 'a' + i;
    if (func(pos + 1, num)) {
      return true;
    }
  }
  return false;
}

int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    string S;
    cin >> S;
    for (int i = 1; i <= 4; i++) {
      for (int j = 0; j < (int)S.size() - i + 1; j++) {
        m[S.substr(j, i)] = true;
      }
    }
  }
  for (int i = 1; i <= 4; i++) {
    str += " ";
    if (func(0, i)) {
      return 0;
    }
  }
  return 0;
}
Problem p01908 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define MAX_L 8
#define MAX_N 100005
typedef long long ll;
ll mod = 1e9 + 7;
ll B = 5575777;

int N;
string S[MAX_N];
set<ll> t[MAX_L + 1];
char tmp[MAX_L + 5];

bool dfs(ll hash, int depth, int anslen) {
  if (depth == anslen) {
    if (t[anslen].count(hash) == 1)
      return false;
    for (int i = 0; i < depth; i++)
      cout << tmp[i];
    cout << endl;
    return true;
  }

  for (char ch = 'a'; ch <= 'z'; ch++) {
    tmp[depth] = ch;
    ll nhash = (hash * B + ch) % mod;
    if (dfs(nhash, depth + 1, anslen))
      return true;
  }
  return false;
}

void solve() {
  for (int i = 0; i < N; i++) {
    int size = S[i].size();

    for (int j = 1; j <= MAX_L; j++) {

      ll key = 0, nb = 1;
      for (int k = 0; k < size; k++) {
        key = (key * B + S[i][k]) % mod;
        if (k - j >= 0) {
          key += (mod - (S[i][k - j] * nb) % mod);
          key %= mod;
        } else
          nb = (nb * B) % mod;
        if (k + 1 >= j)
          t[j].insert(key);
      }
    }
  }

  ll cnt = 1;
  int anslen = 1;
  for (int i = 1; i <= MAX_L; i++) {
    cnt *= 26;
    if ((ll)t[i].size() < cnt) {
      anslen = i;
      break;
    }
  }

  dfs(0, 0, anslen);
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> S[i];
  solve();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define MAX_L 8
#define MAX_N 100005
typedef long long ll;
ll mod = 1e9 + 7;
ll B = 5575777;

int N;
string S[MAX_N];
unordered_set<ll> t[MAX_L + 1];
char tmp[MAX_L + 5];

bool dfs(ll hash, int depth, int anslen) {
  if (depth == anslen) {
    if (t[anslen].count(hash) == 1)
      return false;
    for (int i = 0; i < depth; i++)
      cout << tmp[i];
    cout << endl;
    return true;
  }

  for (char ch = 'a'; ch <= 'z'; ch++) {
    tmp[depth] = ch;
    ll nhash = (hash * B + ch) % mod;
    if (dfs(nhash, depth + 1, anslen))
      return true;
  }
  return false;
}

void solve() {
  for (int i = 0; i < N; i++) {
    int size = S[i].size();

    for (int j = 1; j <= MAX_L; j++) {

      ll key = 0, nb = 1;
      for (int k = 0; k < size; k++) {
        key = (key * B + S[i][k]) % mod;
        if (k - j >= 0) {
          key += (mod - (S[i][k - j] * nb) % mod);
          key %= mod;
        } else
          nb = (nb * B) % mod;
        if (k + 1 >= j)
          t[j].insert(key);
      }
    }
  }

  ll cnt = 1;
  int anslen = 1;
  for (int i = 1; i <= MAX_L; i++) {
    cnt *= 26;
    if ((ll)t[i].size() < cnt) {
      anslen = i;
      break;
    }
  }

  dfs(0, 0, anslen);
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> S[i];
  solve();
  return 0;
}
Problem p01908 (C++)

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

int main() {
  int N;
  cin >> N;
  vector<string> S;
  for (int i = 0; i < N; i++) {
    string s;
    cin >> s;
    S.push_back(s);
  }
  set<string> st;
  for (int i = 0; i < N; i++) {
    string s = S[i];
    for (int j = 1; j <= s.size(); j++) {
      for (int k = 0; k + j <= s.size(); k++) {
        st.insert(s.substr(k, j));
      }
    }
  }
  vector<int> vec(5, -1);
  while (1) {
    bool carry = true;
    for (int i = 0; carry; i++) {
      carry = false;
      vec[i]++;
      if (vec[i] >= 26) {
        vec[i] = 0;
        carry = true;
      }
    }
    if (~vec[4])
      break;
    string ans = "";
    for (int i = 0; vec[i] != -1; i++)
      ans += 'a' + vec[i];
    reverse(ans.begin(), ans.end());
    if (st.count(ans))
      continue;
    else {
      cout << ans << endl;
      return 0;
    }
  }

  return 0;
}

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

int main() {
  int N;
  cin >> N;
  vector<string> S;
  for (int i = 0; i < N; i++) {
    string s;
    cin >> s;
    S.push_back(s);
  }
  set<string> st;
  for (int i = 0; i < N; i++) {
    string s = S[i];
    for (int j = 1; j <= 4; j++) {
      for (int k = 0; k + j <= s.size(); k++) {
        st.insert(s.substr(k, j));
      }
    }
  }
  vector<int> vec(5, -1);
  while (1) {
    bool carry = true;
    for (int i = 0; carry; i++) {
      carry = false;
      vec[i]++;
      if (vec[i] >= 26) {
        vec[i] = 0;
        carry = true;
      }
    }
    if (~vec[4])
      break;
    string ans = "";
    for (int i = 0; vec[i] != -1; i++)
      ans += 'a' + vec[i];
    reverse(ans.begin(), ans.end());
    if (st.count(ans))
      continue;
    else {
      cout << ans << endl;
      return 0;
    }
  }

  return 0;
}
Problem p01908 (C++)

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

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

using namespace std;

const int width = 20;

bool dfs(const int len, unordered_set<string> &S, string pas) {
  if ((int)pas.size() >= len) {
    if (S.count(pas) == 0) {
      cout << pas << endl;
      return true;
    }
    return false;
  }
  rep(i, 26) {
    string npas = pas + string(1, (char)('a' + i));
    if (dfs(len, S, npas))
      return true;
  }
  return false;
}

int main() {
  int n;
  cin >> n;
  unordered_set<string> S;
  rep(i, n) {
    string s;
    cin >> s;
    rep(j, (int)s.size()) {
      rep(k, width) {
        if (j + k >= (int)s.size())
          break;
        S.insert(s.substr(j, k + 1));
      }
    }
  }
  REP(len, 1, width) if (dfs(len, S, "")) break;
  return 0;
}

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

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

using namespace std;

const int width = 7;

bool dfs(const int len, unordered_set<string> &S, string pas) {
  if ((int)pas.size() >= len) {
    if (S.count(pas) == 0) {
      cout << pas << endl;
      return true;
    }
    return false;
  }
  rep(i, 26) {
    string npas = pas + string(1, (char)('a' + i));
    if (dfs(len, S, npas))
      return true;
  }
  return false;
}

int main() {
  int n;
  cin >> n;
  unordered_set<string> S;
  rep(i, n) {
    string s;
    cin >> s;
    rep(j, (int)s.size()) {
      rep(k, width) {
        if (j + k >= (int)s.size())
          break;
        S.insert(s.substr(j, k + 1));
      }
    }
  }
  REP(len, 1, width) if (dfs(len, S, "")) break;
  return 0;
}
Problem p01910 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define int ll
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb emplace_back
#define INF (1LL << 60)

// verified AOJ GRL_1
#define MAX_V 100020
template <typename T> struct edge {
  int to;
  T cost;
};

template <typename T>
void dijkstra(int s, vector<T> &d, vector<edge<T>> G[MAX_V]) {
  priority_queue<pii, vector<pii>, greater<pii>> que;
  rep(i, d.size()) d[i] = INF;
  d[s] = 0;
  que.push(pii(0, s));

  while (que.size()) {
    pii p = que.top();
    que.pop();

    int v = p.second;
    if (d[v] < p.first)
      continue;

    rep(i, G[v].size()) {
      edge<T> e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(pii(d[e.to], e.to));
      }
    }
  }
}

signed main() {
  int n, m, r, q;
  cin >> n >> m >> r >> q;
  vector<edge<int>> G[MAX_V];

  rep(i, m) {
    int s, t, c;
    cin >> s >> t >> c;
    G[t].pb(edge<int>{s, c});
  }
  for (int i = 1; i <= n; i++) {
    G[n + 1 + i % r].pb(edge<int>{i, 0});
  }

  static vector<vector<int>> d(r, vector<int>(n + r));
  for (int i = 0; i < r; i++) {
    dijkstra<int>(n + i + 1, d[i], G);
  }

  int ans = 0;
  rep(i, q) {
    int x, y, z;
    cin >> x >> y >> z;
    int mini = INF;
    rep(j, r) { mini = min(mini, d[j][x] + d[j][y]); }
    if (z - mini > 0)
      ans += z - mini;
  }
  cout << ans << endl;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define int ll
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb emplace_back
#define INF (1LL << 60)

// verified AOJ GRL_1
#define MAX_V 100020
template <typename T> struct edge {
  int to;
  T cost;
};

template <typename T>
void dijkstra(int s, vector<T> &d, vector<edge<T>> G[MAX_V]) {
  priority_queue<pii, vector<pii>, greater<pii>> que;
  rep(i, d.size()) d[i] = INF;
  d[s] = 0;
  que.push(pii(0, s));

  while (que.size()) {
    pii p = que.top();
    que.pop();

    int v = p.second;
    if (d[v] < p.first)
      continue;

    rep(i, G[v].size()) {
      edge<T> e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(pii(d[e.to], e.to));
      }
    }
  }
}

signed main() {
  int n, m, r, q;
  cin >> n >> m >> r >> q;
  vector<edge<int>> G[MAX_V];

  rep(i, m) {
    int s, t, c;
    cin >> s >> t >> c;
    G[t].pb(edge<int>{s, c});
  }
  for (int i = 1; i <= n; i++) {
    G[n + 1 + i % r].pb(edge<int>{i, 0});
  }

  static vector<vector<int>> d(r, vector<int>(n + r + 1));
  for (int i = 0; i < r; i++) {
    dijkstra<int>(n + i + 1, d[i], G);
  }

  int ans = 0;
  rep(i, q) {
    int x, y, z;
    cin >> x >> y >> z;
    int mini = INF;
    rep(j, r) { mini = min(mini, d[j][x] + d[j][y]); }
    if (z - mini > 0)
      ans += z - mini;
  }
  cout << ans << endl;
}
Problem p01911 (C++)

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

#define CIN_ONLY if (1)
struct cww {
  cww() {
    CIN_ONLY {
      ios::sync_with_stdio(false);
      cin.tie(0);
    }
  }
} star;
#define fin "\n"
#define FOR(i, bg, ed) for (int i = (bg); i < (ed); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define fi first
#define se second
#define pb push_back
#define DEBUG if (0)
#define REC(ret, ...) std::function<ret(__VA_ARGS__)>
template <typename T> inline void chmin(T &l, T r) { l = min(l, r); }
template <typename T> inline void chmax(T &l, T r) { l = max(l, r); }
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &it : v)
    is >> it;
  return is;
}
typedef tuple<int, int, int, LL> T;
const LL INF = 1e15;
LL a[112345];
LL b[112345];
LL c[112345];
LL t[112345];

int main() {
  int N, M, S, G;
  cin >> N >> M >> S >> G;
  priority_queue<T> sc;
  vector<LL> ame(N + 1, INF);
  REP(i, M) {
    cin >> a[i] >> b[i] >> t[i] >> c[i];
    sc.push(T(-t[i], 0, i, 0));
  }
  ame[S] = 0;
  LL res = INF;
  while (sc.size()) {
    int tt, type, e;
    LL cww;
    tie(tt, type, e, cww) = sc.top();
    sc.pop();
    LL bg = t[e];
    LL ed = t[e] + c[e];
    if (type == 0) {
      LL cost = ame[a[e]] + t[e];
      sc.push(T(-ed, 1, e, cost));
    } else {
      if (b[e] == G)
        chmin(res, cww);
      chmin(ame[b[e]], cww - t[e] - c[e]);
    }
  }
  cout << res << endl;
  return 0;
}

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

#define CIN_ONLY if (1)
struct cww {
  cww() {
    CIN_ONLY {
      ios::sync_with_stdio(false);
      cin.tie(0);
    }
  }
} star;
#define fin "\n"
#define FOR(i, bg, ed) for (int i = (bg); i < (ed); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define fi first
#define se second
#define pb push_back
#define DEBUG if (0)
#define REC(ret, ...) std::function<ret(__VA_ARGS__)>
template <typename T> inline void chmin(T &l, T r) { l = min(l, r); }
template <typename T> inline void chmax(T &l, T r) { l = max(l, r); }
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &it : v)
    is >> it;
  return is;
}
typedef tuple<int, int, int, LL> T;
const LL INF = 1e15;
LL a[212345];
LL b[212345];
LL c[212345];
LL t[212345];

int main() {
  int N, M, S, G;
  cin >> N >> M >> S >> G;
  priority_queue<T> sc;
  vector<LL> ame(N + 1, INF);
  REP(i, M) {
    cin >> a[i] >> b[i] >> t[i] >> c[i];
    sc.push(T(-t[i], 0, i, 0));
  }
  ame[S] = 0;
  LL res = INF;
  while (sc.size()) {
    int tt, type, e;
    LL cww;
    tie(tt, type, e, cww) = sc.top();
    sc.pop();
    LL bg = t[e];
    LL ed = t[e] + c[e];
    if (type == 0) {
      LL cost = ame[a[e]] + t[e];
      sc.push(T(-ed, 1, e, cost));
    } else {
      if (b[e] == G)
        chmin(res, cww);
      chmin(ame[b[e]], cww - t[e] - c[e]);
    }
  }
  cout << res << endl;
  return 0;
}
Problem p01912 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int h, w, n;
int G[25001][22];
bool used[22][25001];

bool C(int v) {
  for (int i = 0; i < 20; i++) {
    memset(used[i + 1], false, sizeof(used[i + 1]));
    if ((v >> i) & 1) {
      for (int j = 0; j < w * h; j++) {
        if (used[i][j]) {
          if (used[i + 1][G[j][i]])
            return true;
          used[i + 1][G[j][i]] = true;
        }
      }
    } else {
      for (int j = 0; j < w * h; j++) {
        used[i + 1][j] = used[i][j];
      }
    }
  }
  return false;
}

int main(void) {
  scanf("%d%d%d", &h, &w, &n);
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      int r, c;
      scanf("%d%d", &r, &c);
      G[i * w + j][0] = r * w + c;
    }
  }
  for (int i = 0; i < n; i++) {
    int r, c;
    scanf("%d%d", &r, &c);
    used[0][r * w + c] = true;
  }
  for (int i = 1; i < 20; i++) {
    for (int j = 0; j < h * w; j++) {
      G[j][i] = G[G[j][i - 1]][i - 1];
    }
  }
  int l = 0, r = h * w + 1;
  while (r - l > 1) {
    int mid = (l + r) / 2;
    if (C(mid))
      r = mid;
    else
      l = mid;
  }
  printf("%d\n", r == h * w + 1 ? -1 : r);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int h, w, n;
int G[250001][22];
bool used[22][250001];

bool C(int v) {
  for (int i = 0; i < 20; i++) {
    memset(used[i + 1], false, sizeof(used[i + 1]));
    if ((v >> i) & 1) {
      for (int j = 0; j < w * h; j++) {
        if (used[i][j]) {
          if (used[i + 1][G[j][i]])
            return true;
          used[i + 1][G[j][i]] = true;
        }
      }
    } else {
      for (int j = 0; j < w * h; j++) {
        used[i + 1][j] = used[i][j];
      }
    }
  }
  return false;
}

int main(void) {
  scanf("%d%d%d", &h, &w, &n);
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      int r, c;
      scanf("%d%d", &r, &c);
      G[i * w + j][0] = r * w + c;
    }
  }
  for (int i = 0; i < n; i++) {
    int r, c;
    scanf("%d%d", &r, &c);
    used[0][r * w + c] = true;
  }
  for (int i = 1; i < 20; i++) {
    for (int j = 0; j < h * w; j++) {
      G[j][i] = G[G[j][i - 1]][i - 1];
    }
  }
  int l = 0, r = h * w + 1;
  while (r - l > 1) {
    int mid = (l + r) / 2;
    if (C(mid))
      r = mid;
    else
      l = mid;
  }
  printf("%d\n", r == h * w + 1 ? -1 : r);
  return 0;
}
Problem p01917 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
#define fi first
#define se second
using namespace std;
typedef pair<int, int> P;
int n, score[101], a[101][3];

bool ch() {

  int t[10] = {}, x[10];
  r(i, 3) {
    int cnt = 0, pre, c2 = 0;
    memset(x, 0, sizeof(x));

    vector<P> v;

    r(j, n) v.push_back(P(a[j][i], j));

    sort(v.begin(), v.end(), greater<P>());

    r(k, n) {
      if (!k) {
        t[v[k].se] += score[cnt];
        pre = v[k].fi;
      } else {
        if (pre == v[k].fi) {
          c2++;
          t[v[k].se] += score[cnt];
        } else {
          pre = v[k].fi;
          cnt += c2 + 1;
          c2 = 0;
          t[v[k].se] += score[cnt];
        }
      }
    }
  }

  vector<P> v;

  r(j, n) v.push_back(P(t[j], j));

  sort(v.begin(), v.end(), greater<P>());
  int cnt = 0, c2 = 0, pre;
  r(i, n) {
    if (!i) {

      if (!v[i].se && cnt <= 7)
        return 1;

      pre = v[i].fi;
    } else {
      if (pre == v[i].fi) {
        if (!v[i].se && cnt <= 7)
          return 1;
        pre = v[i].fi;
        c2++;
      } else {
        cnt += c2 + 1;
        c2 = 0;
        if (!v[i].se && cnt <= 7)
          return 1;
        pre = v[i].fi;
      }
    }
  }
  return 0;
}

int main() {
  cin >> n;
  r(i, n) cin >> score[i];
  r(i, n) r(j, 3) cin >> a[i][j];
  r(i, 1000) r(j, 3) {
    a[0][j] += i;
    if (ch()) {
      cout << i << endl;
      return 0;
    }
    a[0][j] -= i;
  }
  cout << "Saiko" << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
#define fi first
#define se second
using namespace std;
typedef pair<int, int> P;
int n, score[101], a[101][3];

bool ch() {

  int t[101] = {}, x[101];
  r(i, 3) {
    int cnt = 0, pre, c2 = 0;
    memset(x, 0, sizeof(x));

    vector<P> v;

    r(j, n) v.push_back(P(a[j][i], j));

    sort(v.begin(), v.end(), greater<P>());

    r(k, n) {
      if (!k) {
        t[v[k].se] += score[cnt];
        pre = v[k].fi;
      } else {
        if (pre == v[k].fi) {
          c2++;
          t[v[k].se] += score[cnt];
        } else {
          pre = v[k].fi;
          cnt += c2 + 1;
          c2 = 0;
          t[v[k].se] += score[cnt];
        }
      }
    }
  }

  vector<P> v;

  r(j, n) v.push_back(P(t[j], j));

  sort(v.begin(), v.end(), greater<P>());
  int cnt = 0, c2 = 0, pre;
  r(i, n) {
    if (!i) {

      if (!v[i].se && cnt <= 7)
        return 1;

      pre = v[i].fi;
    } else {
      if (pre == v[i].fi) {
        if (!v[i].se && cnt <= 7)
          return 1;
        pre = v[i].fi;
        c2++;
      } else {
        cnt += c2 + 1;
        c2 = 0;
        if (!v[i].se && cnt <= 7)
          return 1;
        pre = v[i].fi;
      }
    }
  }
  return 0;
}

int main() {
  cin >> n;
  r(i, n) cin >> score[i];
  r(i, n) r(j, 3) cin >> a[i][j];
  r(i, 1000) r(j, 3) {
    a[0][j] += i;
    if (ch()) {
      cout << i << endl;
      return 0;
    }
    a[0][j] -= i;
  }
  cout << "Saiko" << endl;
}
Problem p01925 (C++)

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

int testcase_ends() {
  size_t N, M;
  scanf("%zu %zu", &N, &M);
  if (N == 0 && M == 0)
    return 1;

  std::vector<int> min(N), max(N);
  for (size_t i = 0; i < M; ++i) {
    int S;
    size_t k;
    scanf("%d %zu", &S, &k);
    std::vector<size_t> c(k);
    for (size_t j = 0; j < k; ++j) {
      scanf("%zu", &c[j]);
      --c[j];
    }

    if (k == 1) {
      min[c[0]] += S;
      max[c[0]] += S;
      continue;
    }
    for (size_t j = 0; j < k; ++j) {
      max[c[j]] += S;
    }
  }

  std::vector<int> max_left(N), max_right(N + 1);

  for (size_t i = 0; i < N; ++i) {
    max_left[i + 1] = std::max(max_left[i], max[i]);
  }
  for (size_t i = N; i--;) {
    max_right[i] = std::max(max_right[i + 1], max[i]);
  }

  int res = 0;
  for (size_t i = 0; i < N; ++i) {
    int max0 = std::max(max_left[i], max_right[i + 1]);
    res = std::max(res, max0 - min[i] + 1);
  }
  printf("%d\n", res);
  return 0;
}

int main() {
  while (!testcase_ends()) {
  }
}


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

int testcase_ends() {
  size_t N, M;
  scanf("%zu %zu", &N, &M);
  if (N == 0 && M == 0)
    return 1;

  std::vector<int> min(N), max(N);
  for (size_t i = 0; i < M; ++i) {
    int S;
    size_t k;
    scanf("%d %zu", &S, &k);
    std::vector<size_t> c(k);
    for (size_t j = 0; j < k; ++j) {
      scanf("%zu", &c[j]);
      --c[j];
    }

    if (k == 1) {
      min[c[0]] += S;
      max[c[0]] += S;
      continue;
    }
    for (size_t j = 0; j < k; ++j) {
      max[c[j]] += S;
    }
  }

  std::vector<int> max_left(N + 1), max_right(N + 1);

  for (size_t i = 0; i < N; ++i) {
    max_left[i + 1] = std::max(max_left[i], max[i]);
  }
  for (size_t i = N; i--;) {
    max_right[i] = std::max(max_right[i + 1], max[i]);
  }

  int res = 0;
  for (size_t i = 0; i < N; ++i) {
    int max0 = std::max(max_left[i], max_right[i + 1]);
    res = std::max(res, max0 - min[i] + 1);
  }
  printf("%d\n", res);
  return 0;
}

int main() {
  while (!testcase_ends()) {
  }
}
Problem p01933 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, a, n) for (int i = ((int)a); i < ((int)n); i++)
#define INF (1 << 30)
using namespace std;

int N, K, A;
vector<int> R, E[100000];
bool ans[100000];

int dfs(int v) {
  int depth = INF;
  for (int u : E[v])
    depth = min(depth, dfs(u));
  if (depth == INF)
    depth = 0;
  if (depth < K)
    ans[v] = true;
  return depth + 1;
}

int main(void) {
  cin >> N >> K;
  REP(i, 0, N) {
    cin >> A;
    if (A >= 1)
      E[A - 1].push_back(i);
    else
      R.push_back(i);
  }

  REP(i, 0, N) ans[i] = false;
  REP(i, 0, R.size()) ans[R[i]] = true, dfs(R[i]);

  int cnt = 0;
  REP(i, 0, N) if (ans[i]) cnt++;
  cout << cnt << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, a, n) for (int i = ((int)a); i < ((int)n); i++)
#define INF (1 << 30)
using namespace std;

int N, K, A;
vector<int> R, E[100000];
bool ans[100000];

int dfs(int v) {
  int depth = INF;
  REP(i, 0, E[v].size()) depth = min(depth, dfs(E[v][i]));
  if (depth == INF)
    depth = 0;
  if (depth < K)
    ans[v] = true;
  return depth + 1;
}

int main(void) {
  cin >> N >> K;
  REP(i, 0, N) {
    cin >> A;
    if (A >= 1)
      E[A - 1].push_back(i);
    else
      R.push_back(i);
  }

  REP(i, 0, N) ans[i] = false;
  REP(i, 0, R.size()) ans[R[i]] = true, dfs(R[i]);

  int cnt = 0;
  REP(i, 0, N) if (ans[i]) cnt++;
  cout << cnt << endl;
}
Problem p01933 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>

using namespace std;
#define repeat(i, n) for (int i = 0; i < (n); i++)

vector<int> g[100005]; // 0-indexed
int root[100005];
const int inf = 1e8;
vector<int> d(100005, inf);
int N, K;

void f(int i, int p = -1) {
  int ret = inf;
  for (int j : g[i])
    if (j != p) {
      f(j, i);
      ret = min(ret, d[j]);
    }
  if (ret == inf)
    d[i] = 0;
  else
    d[i] = ret + 1;
}

int main() {

  cin >> N >> K;
  repeat(i, N) {
    int x;
    cin >> x;
    if (x == 0) {
      root[i] = 1;
    } else {
      g[x - 1].push_back(i);
      g[i].push_back(x - 1);
    }
  }

  repeat(i, N) if (root[i]) { f(i); }

  int c = 0;
  repeat(i, N) {
    if (root[i])
      c++;
    else
      c += (d[i] < K);
  }
  cout << c << endl;

  return 0;
}

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

using namespace std;
#define repeat(i, n) for (int i = 0; i < (n); i++)

vector<int> g[100005]; // 0-indexed
int root[100005];
const int inf = 1e8;
vector<int> d(100005, inf);
int N, K;

void f(int i, int p = -1) {
  int ret = inf;
  for (int j : g[i])
    if (j != p) {
      f(j, i);
      ret = min(ret, d[j]);
    }
  if (ret == inf)
    d[i] = 0;
  else
    d[i] = ret + 1;
}

int main() {

  cin >> N >> K;
  repeat(i, N) {
    int x;
    cin >> x;
    if (x == 0) {
      root[i] = 1;
    } else {
      g[x - 1].push_back(i);
      // g[i].push_back(x-1);
    }
  }

  repeat(i, N) if (root[i]) { f(i); }

  int c = 0;
  repeat(i, N) {
    if (root[i])
      c++;
    else
      c += (d[i] < K);
  }
  cout << c << endl;

  return 0;
}