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

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

bool dfs(int v, int const s, vector<vector<int>> const &es,
         vector<bool> &visited) {
  visited[v] = true;
  bool res = false;
  for (auto &u : es[v]) {
    if (!visited[u]) {
      res |= dfs(u, s, es, visited);
    } else if (visited[v] && u == s) {
      res = true;
      break;
    }
  }
  return res;
}

int main() {
  int n;
  cin >> n;
  vector<vector<int>> es(200);
  for (int i = 0; i < n; ++i) {
    int u, d;
    string s;
    cin >> u >> s >> d;
    d += 100;
    if (s == "lock") {
      es[d].push_back(u);
    } else {
      es[u].push_back(d);
    }
  }
  bool res = false;
  for (int i = 0; i < 101; ++i) {
    vector<bool> visited(210);
    res |= dfs(i, i, es, visited);
  }
  cout << res << endl;
}

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

bool dfs(int v, int const s, vector<vector<int>> const &es,
         vector<bool> &visited) {
  visited[v] = true;
  bool res = false;
  for (auto &u : es[v]) {
    if (!visited[u]) {
      res |= dfs(u, s, es, visited);
    } else if (visited[v] && u == s) {
      res = true;
      break;
    }
  }
  return res;
}

int main() {
  int n;
  cin >> n;
  vector<vector<int>> es(210);
  for (int i = 0; i < n; ++i) {
    int u, d;
    string s;
    cin >> u >> s >> d;
    d += 100;
    if (s == "lock") {
      es[d].push_back(u);
    } else {
      es[u].push_back(d);
    }
  }
  bool res = false;
  for (int i = 0; i < 101; ++i) {
    vector<bool> visited(210);
    res |= dfs(i, i, es, visited);
  }
  cout << res << endl;
}
Problem p00303 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
vector<int> G[240];
int dist[240];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int a, b;
    string S;
    cin >> a >> S >> b;
    a--;
    b--;
    if (S == "wait") {
      G[a].push_back(b + 120);
    }
    if (S == "lock") {
      G[b + 120].push_back(a);
    }
  }
  int C = 0;
  for (int i = 0; i < 2 * n; i++) {
    queue<int> Q;
    for (int j = 0; j < 240; j++)
      dist[j] = 0;
    Q.push(i);
    int cnt = 0;
    while (!Q.empty()) {
      cnt++;
      int a1 = Q.front();
      Q.pop();
      if (cnt >= 100000) {
        C = 1;
        break;
      }
      for (int j = 0; j < G[a1].size(); j++) {
        if (dist[G[a1][j]] > dist[a1] - 1) {
          dist[G[a1][j]] = dist[a1] - 1;
          Q.push(G[a1][j]);
        }
      }
    }
  }
  cout << C << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
vector<int> G[240];
int dist[240];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int a, b;
    string S;
    cin >> a >> S >> b;
    a--;
    b--;
    if (S == "wait") {
      G[a].push_back(b + 120);
    }
    if (S == "lock") {
      G[b + 120].push_back(a);
    }
  }
  int C = 0;
  for (int i = 0; i < 240; i++) {
    queue<int> Q;
    for (int j = 0; j < 240; j++)
      dist[j] = 0;
    Q.push(i);
    int cnt = 0;
    while (!Q.empty()) {
      cnt++;
      int a1 = Q.front();
      Q.pop();
      if (cnt >= 100000) {
        C = 1;
        break;
      }
      for (int j = 0; j < G[a1].size(); j++) {
        if (dist[G[a1][j]] > dist[a1] - 1) {
          dist[G[a1][j]] = dist[a1] - 1;
          Q.push(G[a1][j]);
        }
      }
    }
  }
  cout << C << endl;
  return 0;
}
Problem p00303 (C++)

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

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

#define PB push_back
#define MP make_pair
typedef pair<int, int> pii;

int n;
vector<int> rods[200];
bool visited[200];
int nw;

bool chk(int pl) {
  visited[pl] = true;
  REP(i, rods[pl].size()) {
    int k = rods[pl][i];
    if (k == nw)
      return true;
    if (visited[k])
      continue;
    if (chk(k))
      return true;
  }
  return false;
}

int main() {
  scanf("%d", &n);
  REP(i, n) {
    int u, d;
    char s[5];
    scanf("%d%s%d", &u, s, &d);
    u--;
    d--;
    if (s[0] == 'l')
      rods[d + 100].PB(u);
    else
      rods[u].PB(d + 100);
  }
  REP(i, n) {
    nw = i;
    fill(visited, visited + 200, false);
    if (chk(nw)) {
      printf("1\n");
      return 0;
    }
  }
  printf("0\n");
}

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

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

#define PB push_back
#define MP make_pair
typedef pair<int, int> pii;

int n;
vector<int> rods[200];
bool visited[200];
int nw;

bool chk(int pl) {
  visited[pl] = true;
  REP(i, rods[pl].size()) {
    int k = rods[pl][i];
    if (k == nw)
      return true;
    if (visited[k])
      continue;
    if (chk(k))
      return true;
  }
  return false;
}

int main() {
  scanf("%d", &n);
  REP(i, n) {
    int u, d;
    char s[5];
    scanf("%d%s%d", &u, s, &d);
    u--;
    d--;
    if (s[0] == 'l')
      rods[d + 100].PB(u);
    else
      rods[u].PB(d + 100);
  }
  REP(i, 100) {
    nw = i;
    fill(visited, visited + 200, false);
    if (chk(nw)) {
      printf("1\n");
      return 0;
    }
  }
  printf("0\n");
}
Problem p00303 (C++)

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

using namespace std;

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

vector<int> g[200];

int main() {
  int n;
  cin >> n;
  REP(i, n) {
    int a, b;
    string str;
    cin >> a >> str >> b;
    a--;
    b--;
    b += 100;
    if (str == "lock")
      g[b].push_back(a);
    else
      g[a].push_back(b);
  }

  bool done[200];
  bool flag = false;
  REP(i, n) {
    fill(done, done + 200, false);
    int st = i + 100;

    queue<int> que;
    REP(i, g[st].size()) que.push(g[st][i]);

    while (!que.empty()) {
      int now = que.front();
      que.pop();
      if (done[now])
        continue;
      done[now] = true;

      if (now == st) {
        flag = true;
        break;
      }

      REP(i, g[now].size()) que.push(g[now][i]);
    }
    if (flag)
      break;
  }

  puts(flag ? "1" : "0");

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;

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

vector<int> g[200];

int main() {
  int n;
  cin >> n;
  REP(i, n) {
    int a, b;
    string str;
    cin >> a >> str >> b;
    a--;
    b--;
    b += 100;
    if (str == "lock")
      g[b].push_back(a);
    else
      g[a].push_back(b);
  }

  bool done[200];
  bool flag = false;
  REP(i, 100) {
    fill(done, done + 200, false);
    int st = i + 100;

    queue<int> que;
    REP(i, g[st].size()) que.push(g[st][i]);

    while (!que.empty()) {
      int now = que.front();
      que.pop();
      if (done[now])
        continue;
      done[now] = true;

      if (now == st) {
        flag = true;
        break;
      }

      REP(i, g[now].size()) que.push(g[now][i]);
    }
    if (flag)
      break;
  }

  puts(flag ? "1" : "0");

  return 0;
}
Problem p00305 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
int k[1000][1000], x[1000][1000], n, cnt, maxn = -10000000;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      scanf("%d", &k[i][j]);
    }
  }
  for (int i = 1; i <= n; i++) {
    cnt = 0;
    for (int j = 1; j <= n; j++) {
      cnt += k[i][j];
      x[i][j] = cnt + x[i - 1][j];
    }
  }
  if (n <= 200) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        for (int k = i + 1; k <= n; k++) {
          for (int l = j + 1; l <= n; l++) {
            if (k - i >= 3 && l - j >= 3) {
              maxn = max(maxn, (x[i][j] + x[k][l] - x[i][l] - x[k][j]) -
                                   (x[i + 1][j + 1] + x[k - 1][l - 1] -
                                    x[i + 1][l - 1] - x[k - 1][j + 1]));
            } else {
              maxn = max(maxn, x[i][j] + x[k][l] - x[i][l] - x[k][j]);
            }
          }
        }
      }
    }
  } else {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        for (int k = i + 1; k <= n; k++) {
          for (int l = j + 1; l <= n; l++) {
            if (k - i >= 220 && l - j >= 220) {
              maxn = max(maxn, (x[i][j] + x[k][l] - x[i][l] - x[k][j]) -
                                   (x[i + 1][j + 1] + x[k - 1][l - 1] -
                                    x[i + 1][l - 1] - x[k - 1][j + 1]));
            }
          }
        }
      }
    }
  }
  cout << maxn << endl;
}

Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
int k[1000][1000], x[1000][1000], n, cnt, maxn = -10000000;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      scanf("%d", &k[i][j]);
    }
  }
  for (int i = 1; i <= n; i++) {
    cnt = 0;
    for (int j = 1; j <= n; j++) {
      cnt += k[i][j];
      x[i][j] = cnt + x[i - 1][j];
    }
  }
  if (n <= 200) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        for (int k = i + 1; k <= n; k++) {
          for (int l = j + 1; l <= n; l++) {
            if (k - i >= 3 && l - j >= 3) {
              maxn = max(maxn, (x[i][j] + x[k][l] - x[i][l] - x[k][j]) -
                                   (x[i + 1][j + 1] + x[k - 1][l - 1] -
                                    x[i + 1][l - 1] - x[k - 1][j + 1]));
            } else {
              maxn = max(maxn, x[i][j] + x[k][l] - x[i][l] - x[k][j]);
            }
          }
        }
      }
    }
  } else {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        for (int k = i + 1; k <= n; k++) {
          if (k - i >= 180) {
            for (int l = j + 1; l <= n; l++) {
              if (l - j >= 180) {
                maxn = max(maxn, (x[i][j] + x[k][l] - x[i][l] - x[k][j]) -
                                     (x[i + 1][j + 1] + x[k - 1][l - 1] -
                                      x[i + 1][l - 1] - x[k - 1][j + 1]));
              }
            }
          }
        }
      }
    }
  }
  cout << maxn << endl;
}
Problem p00305 (C++)

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

int p[333][333];
int yoko[333][333];
int tate[333][333];
int dp[301][301][301];

signed main() {
  int n;
  cin >> n;

  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      cin >> p[i][j];

  for (int i = 0; i < n; i++) {
    int sum = 0;
    for (int j = 0; j < n; j++) {
      sum += p[i][j];
      yoko[i][j] = sum;
    }
    sum = 0;
    for (int j = 0; j < n; j++) {
      sum += p[j][i];
      tate[j][i] = sum;
    }
  }
  int INF = 100000000;
  fill_n(**dp, 333 * 333 * 333, INF);
  for (int i = 0; i < n; i++) {
    for (int j = i; j < n; j++) {
      for (int k = n - 1; k >= 0; k--) {
        dp[i][j][k] =
            yoko[i][k] + yoko[j][k] + tate[j][k] - tate[i][k] - p[j][k];
        if (i == j) {
          dp[i][j][k] -= yoko[i][k];
          dp[i][j][k] += p[j][k];
        }
        if (k < n - 1)
          dp[i][j][k] = max(dp[i][j][k], dp[i][j][k + 1]);
        //	cout<<i<<" "<<j<<" "<<k<<" "<<dp[i][j][k]<<endl;
      }
    }
  }
  int ans = -INF;
  for (int i = 0; i < n; i++) {
    for (int j = i; j < n; j++) {
      for (int k = 0; k < n; k++) {
        int sum = tate[j][k] - tate[i][k] + p[i][k];
        if (k < n - 1) {
          int tmp = sum;
          tmp += dp[i][j][k + 1];
          tmp -= (yoko[i][k] + yoko[j][k]);
          if (i == j)
            tmp += yoko[i][k];
          sum = max(sum, tmp);
        }
        ans = max(ans, sum);
        //	cout<<i<<" "<<j<<" "<<k<<" "<<ans<<endl;
      }
    }
  }
  cout << ans << endl;
  return 0;
}


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

int p[333][333];
int yoko[333][333];
int tate[333][333];
int dp[301][301][301];

signed main() {
  int n;
  cin >> n;

  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      cin >> p[i][j];

  for (int i = 0; i < n; i++) {
    int sum = 0;
    for (int j = 0; j < n; j++) {
      sum += p[i][j];
      yoko[i][j] = sum;
    }
    sum = 0;
    for (int j = 0; j < n; j++) {
      sum += p[j][i];
      tate[j][i] = sum;
    }
  }
  int INF = 100000000;
  fill_n(**dp, 301 * 301 * 301, INF);
  for (int i = 0; i < n; i++) {
    for (int j = i; j < n; j++) {
      for (int k = n - 1; k >= 0; k--) {
        dp[i][j][k] =
            yoko[i][k] + yoko[j][k] + tate[j][k] - tate[i][k] - p[j][k];
        if (i == j) {
          dp[i][j][k] -= yoko[i][k];
          dp[i][j][k] += p[j][k];
        }
        if (k < n - 1)
          dp[i][j][k] = max(dp[i][j][k], dp[i][j][k + 1]);
        //	cout<<i<<" "<<j<<" "<<k<<" "<<dp[i][j][k]<<endl;
      }
    }
  }
  int ans = -INF;
  for (int i = 0; i < n; i++) {
    for (int j = i; j < n; j++) {
      for (int k = 0; k < n; k++) {
        int sum = tate[j][k] - tate[i][k] + p[i][k];
        if (k < n - 1) {
          int tmp = sum;
          tmp += dp[i][j][k + 1];
          tmp -= (yoko[i][k] + yoko[j][k]);
          if (i == j)
            tmp += yoko[i][k];
          sum = max(sum, tmp);
        }
        ans = max(ans, sum);
        //	cout<<i<<" "<<j<<" "<<k<<" "<<ans<<endl;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p00314 (C++)

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

using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> p(N);
  for (int &t : p)
    cin >> t;
  for (int A = 100; A >= 0; A--) {
    int c = 0;
    for (int t : p)
      if (t >= A)
        c++;
    if (c >= A) {
      cout << A << endl;
      break;
    }
  }
}

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

using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> p(N);
  for (int &t : p)
    cin >> t;
  for (int A = 100; A >= 0; A--) {
    int c = 0;
    for (int t : p)
      if (t >= A)
        c++;
    if (c >= A) {
      cout << A << endl;
      break;
    }
  }
  return 0;
}
Problem p00314 (C++)

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

using namespace std;
using LL = long long;
int main() {
  int n;
  cin >> n;
  vector<int> vec;
  int ma = 0;
  for (int a = 0; a < n; a++) {
    int b;
    cin >> b;
    vec.push_back(b);
    ma = max(ma, b);
  }
  for (int a = ma; a >= 0; a--) {
    int ins = 0;
    for (int b = 0; b < n; b++) {
      if (vec.at(b) >= a) {
        ins++;
      }
    }
    if (ins >= a) {
      cout << a << endl;
      return a;
    }
  }
  cout << 0 << endl;
  return 0;
}


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

using namespace std;
using LL = long long;
int main() {
  int n;
  cin >> n;
  vector<int> vec;
  int ma = 0;
  for (int a = 0; a < n; a++) {
    int b;
    cin >> b;
    vec.push_back(b);
    ma = max(ma, b);
  }
  for (int a = ma; a >= 0; a--) {
    int ins = 0;
    for (int b = 0; b < n; b++) {
      if (vec.at(b) >= a) {
        ins++;
      }
    }
    if (ins >= a) {
      cout << a << endl;
      return 0;
    }
  }
  cout << 0 << endl;
  return 0;
}
Problem p00316 (C++)

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

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

#define pb push_back
#define rep(i, a, n) for (int i = (a); i < (n); i++)
#define dep(i, a, n) for (int i = (a); i >= (n); i--)
#define mod 1e9 + 7

__attribute__((constructor)) void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

int s[100001], v[100001], r[100001];

void init() {
  rep(i, 0, 100001) {
    s[i] = i;
    r[i] = 0;
  }
}

int find(int x) {
  if (s[x] == x) {
    return x;
  } else {
    return s[x] = find(s[x]);
  }
}

void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;

  if (r[x] < r[y]) {
    s[x] = y;
    if (v[x])
      v[y] = v[x];
  } else {
    s[y] = x;
    if (r[x] == r[y])
      r[x]++;
    if (v[y])
      v[x] = v[y];
  }
}

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  int a[100001], b[100001], c[100001];
  init();
  rep(i, 0, k) cin >> a[i] >> b[i] >> c[i];
  rep(i, 0, k) {
    if (a[i] == 1) {
      int bb = find(b[i]);
      int cc = find(c[i]);
      if (v[bb] && v[cc] && v[bb] != v[cc]) {
        cout << i + 1 << endl;
        return 0;
      }
      unite(bb, cc);
    } else {
      int bb = find(b[i]);
      if (v[bb] && v[bb] != c[i]) {
        cout << i + 1 << endl;
        return 0;
      }
      v[bb] = c[i];
    }
  }

  cout << 0 << endl;
}

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

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

#define pb push_back
#define rep(i, a, n) for (int i = (a); i < (n); i++)
#define dep(i, a, n) for (int i = (a); i >= (n); i--)
#define mod 1e9 + 7

__attribute__((constructor)) void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

int s[100001], v[100001], r[100001];

void init() {
  rep(i, 0, 100001) {
    s[i] = i;
    r[i] = 0;
  }
}

int find(int x) {
  if (s[x] == x) {
    return x;
  } else {
    return s[x] = find(s[x]);
  }
}

void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;

  if (r[x] < r[y]) {
    s[x] = y;
    if (v[x])
      v[y] = v[x];
  } else {
    s[y] = x;
    if (r[x] == r[y])
      r[x]++;
    if (v[y])
      v[x] = v[y];
  }
}

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  int a[200001], b[200001], c[200001];
  init();
  rep(i, 0, k) cin >> a[i] >> b[i] >> c[i];
  rep(i, 0, k) {
    if (a[i] == 1) {
      int bb = find(b[i]);
      int cc = find(c[i]);
      if (v[bb] && v[cc] && v[bb] != v[cc]) {
        cout << i + 1 << endl;
        return 0;
      }
      unite(bb, cc);
    } else {
      int bb = find(b[i]);
      if (v[bb] && v[bb] != c[i]) {
        cout << i + 1 << endl;
        return 0;
      }
      v[bb] = c[i];
    }
  }

  cout << 0 << endl;
}
Problem p00318 (C++)

Buggy submission (status: Time Limit Exceeded):
#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"

bool check(vector<pair<ld, ld>> &ps, ld dis) {
  map<ld, int> mp;
  for (int i = 0; i < ps.size(); ++i) {
    auto p(ps[i]);
    ld d = sqrt(p.second * p.second - dis * dis);
    ld l(p.first - d);
    ld r(p.first + d);
    mp[l]++;
    mp[r]--;
  }
  int num = 0;
  bool flag = false;
  for (auto it = mp.begin(); it != mp.end(); ++it) {
    auto m(*it);
    num += m.second;
    if (num == ps.size())
      flag = true;
  }
  return flag;
}

int main() {
  int N;
  cin >> N;
  vector<pair<ld, ld>> ps;
  ld amin = 0;
  ld amax = 1e18;
  for (int i = 0; i < N; ++i) {
    int a, b;
    cin >> a >> b;
    ps.emplace_back(a, b);
    amax = min(amax, ld(b));
  }

  amax -= 1e-9;
  int kk = 40;
  while (kk--) {
    ld amid((amin + amax) / 2);
    if (check(ps, amid)) {
      amin = amid;
    } else {
      amax = amid;
    }
  }
  cout << setprecision(10) << fixed << amin << 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"

bool check(vector<pair<ld, ld>> &ps, ld dis) {
  map<ld, int> mp;
  for (int i = 0; i < ps.size(); ++i) {
    auto p(ps[i]);
    ld d = sqrt(p.second * p.second - dis * dis);
    ld l(p.first - d);
    ld r(p.first + d);
    mp[l]++;
    mp[r]--;
  }
  int num = 0;
  bool flag = false;
  for (auto it = mp.begin(); it != mp.end(); ++it) {
    auto m(*it);
    num += m.second;
    if (num == ps.size())
      flag = true;
  }
  return flag;
}

int main() {
  int N;
  cin >> N;
  vector<pair<ld, ld>> ps;
  ld amin = 0;
  ld amax = 1e18;
  for (int i = 0; i < N; ++i) {
    int a, b;
    cin >> a >> b;
    ps.emplace_back(a, b);
    amax = min(amax, ld(b));
  }

  amax -= 1e-9;
  int kk = 30;
  while (kk--) {
    ld amid((amin + amax) / 2);
    if (check(ps, amid)) {
      amin = amid;
    } else {
      amax = amid;
    }
  }
  cout << setprecision(10) << fixed << amin << endl;

  return 0;
}
Problem p00318 (C++)

Buggy submission (status: Time Limit Exceeded):
// 2分探索, 座標圧縮的な何か, imos法
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>
#define rep(i, n) for (i = 0; i < n; i++)
using namespace std;

int n;
double x[100000], r[100000];

bool check(double y) {
  int i;
  vector<double> vec;
  static int imos[200001];

  rep(i, n) {
    if (y >= r[i])
      return false;
    double width = sqrt(r[i] * r[i] - y * y);
    vec.push_back(x[i] - width);
    vec.push_back(x[i] + width);
  }
  sort(vec.begin(), vec.end());

  rep(i, 2 * n) imos[i] = 0;

  rep(i, n) {
    double width = sqrt(r[i] * r[i] - y * y);
    int l = lower_bound(vec.begin(), vec.end(), x[i] - width) - vec.begin();
    int r = lower_bound(vec.begin(), vec.end(), x[i] + width) - vec.begin();
    // 区間l,l+1,…,r-1に1を足す
    imos[l]++;
    imos[r]--;
  }

  rep(i, 2 * n) if (i > 0) imos[i] += imos[i - 1];
  rep(i, 2 * n) if (imos[i] == n) return true;
  return false;
}

int main() {
  int i;

  cin >> n;
  rep(i, n) cin >> x[i] >> r[i];

  double st = 0, ed = 1000000, mid; // ooooxxx, [st, ed)
  for (i = 0; i < 120; i++) {
    mid = (st + ed) / 2;
    if (check(mid))
      st = mid;
    else
      ed = mid;
  }
  printf("%.14f\n", st);
  return 0;
}


Fixed submission:
// 2分探索, 座標圧縮的な何か, imos法
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>
#define rep(i, n) for (i = 0; i < n; i++)
using namespace std;

int n;
double x[100000], r[100000];

bool check(double y) {
  int i;
  vector<double> vec;
  static int imos[200001];

  rep(i, n) {
    if (y >= r[i])
      return false;
    double width = sqrt(r[i] * r[i] - y * y);
    vec.push_back(x[i] - width);
    vec.push_back(x[i] + width);
  }
  sort(vec.begin(), vec.end());

  rep(i, 2 * n) imos[i] = 0;

  rep(i, n) {
    double width = sqrt(r[i] * r[i] - y * y);
    int l = lower_bound(vec.begin(), vec.end(), x[i] - width) - vec.begin();
    int r = lower_bound(vec.begin(), vec.end(), x[i] + width) - vec.begin();
    // 区間l,l+1,…,r-1に1を足す
    imos[l]++;
    imos[r]--;
  }

  rep(i, 2 * n) if (i > 0) imos[i] += imos[i - 1];
  rep(i, 2 * n) if (imos[i] == n) return true;
  return false;
}

int main() {
  int i;

  cin >> n;
  rep(i, n) cin >> x[i] >> r[i];

  double st = 0, ed = 1000000, mid; // ooooxxx, [st, ed)
  for (i = 0; i < 50; i++) {
    mid = (st + ed) / 2;
    if (check(mid))
      st = mid;
    else
      ed = mid;
  }
  printf("%.14f\n", st);
  return 0;
}
Problem p00321 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
int main() {
  int n, f;
  int num = 0;
  map<string, int> mp;
  int con[1001][1001] = {};
  cin >> n >> f;
  string str[1001];
  for (int i = 0; i < n; i++) {
    int m;
    cin >> m;
    vector<string> it(m);

    for (int j = 0; j < m; j++) {
      cin >> it[j];
      if (!mp[it[j]]) {
        num++;
        mp[it[j]] = num;
        str[num] = it[j];
      }
    }
    for (int j = 0; j < m; j++) {
      for (int k = m - 1; k >= 0; k--) {
        if (j == k)
          continue;
        if (it[j] > it[k])
          continue;
        con[mp[it[j]]][mp[it[k]]]++;
      }
    }
  }
  vector<pair<string, string>> ans;
  pair<string, string> abuf;
  int anum = 0;
  for (int i = 1; i <= num; i++) {
    for (int j = 0; j <= 1001; j++) {
      if (con[i][j] >= f) {
        abuf.first = str[i];
        abuf.second = str[j];
        ans.push_back(abuf);
        anum++;
      }
    }
  }
  sort(ans.begin(), ans.end());
  cout << anum << endl;
  for (int i = 0; i < anum; i++) {
    cout << ans[i].first << " " << ans[i].second << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
int main() {
  int n, f;
  int num = 0;
  map<string, int> mp;
  int con[1001][1001] = {};
  cin >> n >> f;
  string str[1001];
  for (int i = 0; i < n; i++) {
    int m;
    cin >> m;
    vector<string> it(m);

    for (int j = 0; j < m; j++) {
      cin >> it[j];
      if (!mp[it[j]]) {
        num++;
        mp[it[j]] = num;
        str[num] = it[j];
      }
    }
    for (int j = 0; j < m; j++) {
      for (int k = m - 1; k >= 0; k--) {
        if (j == k)
          continue;
        if (it[j] > it[k])
          continue;
        con[mp[it[j]]][mp[it[k]]]++;
      }
    }
  }
  vector<pair<string, string>> ans;
  pair<string, string> abuf;
  int anum = 0;
  for (int i = 1; i <= num; i++) {
    for (int j = 0; j <= num; j++) {
      if (con[i][j] >= f) {
        abuf.first = str[i];
        abuf.second = str[j];
        ans.push_back(abuf);
        anum++;
      }
    }
  }
  sort(ans.begin(), ans.end());
  cout << anum << endl;
  for (int i = 0; i < anum; i++) {
    cout << ans[i].first << " " << ans[i].second << endl;
  }
  return 0;
}
Problem p00323 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#define INF 210000
using namespace std;
int c[INF];
int main() {
  int n, a, b;
  for (int i = 0; i < INF; i++)
    c[i] = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a >> b;
    c[a + b]++;
  }
  for (int i = 0; i < INF; i++) {
    c[i + 1] += c[i] / 2;
    if (c[i] % 2)
      cout << i << " 0" << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#define INF 210000
using namespace std;
int c[INF];
int main() {
  int n, a, b;
  for (int i = 0; i < INF; i++)
    c[i] = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a >> b;
    c[a + b]++;
  }
  for (int i = 0; i < INF - 1; i++) {
    c[i + 1] += c[i] / 2;
    if (c[i] % 2)
      cout << i << " 0" << endl;
  }
  return 0;
}
Problem p00329 (C++)

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

int N, a, cc = 1;
vector<int> G[MAX_V];
int match[MAX_V];
bool used[MAX_V];

bool dfs(int v) {
  used[v] = cc;
  for (int i = 0; i < (int)G[v].size(); i++) {
    int u = G[v][i], w = match[u];
    if (w < 0 || (used[w] != cc && dfs(w))) {
      match[u] = v;
      match[v] = u;
      return true;
    }
  }
  return false;
}

int bipartite_matching() {
  int res = 0;
  memset(match, -1, sizeof(match));
  for (int v = 0; v < N; v++) {
    if (match[v] < 0) {
      cc++;
      if (dfs(v))
        res++;
    }
  }
  return res;
}

int main() {
  scanf("%d", &N);
  N--;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      scanf("%d", &a);
      if (a == 1)
        G[j].push_back(N + i);
    }
  }
  for (int i = 0; i < N; i++)
    sort(G[i].begin(), G[i].end());
  int f = bipartite_matching();
  printf(f == N ? "yes\n" : "no\n");
  for (int i = 0; i < N && f == N; i++) {
    match[match[i]] = -1;
    match[i] = -1;
    cc++;
    dfs(i);
    G[i].clear();
    G[i].push_back(match[i]);
    printf("%d\n", match[i] - N + 1);
  }
  return 0;
}

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

int N, a, cc = 1;
vector<int> G[MAX_V];
int match[MAX_V];
int used[MAX_V];

bool dfs(int v) {
  used[v] = cc;
  for (int i = 0; i < (int)G[v].size(); i++) {
    int u = G[v][i], w = match[u];
    if (w < 0 || (used[w] != cc && dfs(w))) {
      match[u] = v;
      match[v] = u;
      return true;
    }
  }
  return false;
}

int bipartite_matching() {
  int res = 0;
  memset(match, -1, sizeof(match));
  for (int v = 0; v < N; v++) {
    if (match[v] < 0) {
      cc++;
      if (dfs(v))
        res++;
    }
  }
  return res;
}

int main() {
  scanf("%d", &N);
  N--;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      scanf("%d", &a);
      if (a == 1)
        G[j].push_back(N + i);
    }
  }
  for (int i = 0; i < N; i++)
    sort(G[i].begin(), G[i].end());
  int f = bipartite_matching();
  printf(f == N ? "yes\n" : "no\n");
  for (int i = 0; i < N && f == N; i++) {
    match[match[i]] = -1;
    match[i] = -1;
    cc++;
    dfs(i);
    G[i].clear();
    G[i].push_back(match[i]);
    printf("%d\n", match[i] - N + 1);
  }
  return 0;
}
Problem p00333 (C++)

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

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return (b, a % b);
}

int main() {
  int W, H, C;
  cin >> W >> H >> C;
  int GCD = gcd(W, H);
  cout << ((W / GCD) * (H / GCD) * C) << endl;

  return 0;
}


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

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

int main() {
  int W, H, C;
  cin >> W >> H >> C;
  int GCD = gcd(W, H);
  cout << ((W / GCD) * (H / GCD) * C) << endl;

  return 0;
}
Problem p00333 (C++)

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

int gcd(int m, int n) {
  if (n == 0)
    return 0;
  return gcd(n, m % n);
}

int main() {
  int W, H, C;
  cin >> W >> H >> C;

  int g = gcd(W, H);
  cout << W / g * H / g * C << endl;

  return 0;
}


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

int gcd(int m, int n) {
  if (n == 0)
    return m;
  return gcd(n, m % n);
}

int main() {
  int W, H, C;
  cin >> W >> H >> C;

  int g = gcd(W, H);
  cout << W / g * H / g * C << endl;

  return 0;
}
Problem p00334 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int main() {
  int te[100][3];
  bool ka[100] = {0};
  int n;
  int cnt;
  int i, j;
  int tmp;

  cin >> n;

  for (i = 0; i < n; i++) {
    cin >> te[i][0] >> te[i][1] >> te[i][2];
    if (te[i][0] > te[i][1]) {
      tmp = te[i][0];
      te[i][0] = te[i][1];
      te[i][1] = tmp;
    }
    if (te[i][1] > te[i][2]) {
      tmp = te[i][1];
      te[i][1] = te[i][2];
      te[i][2] = tmp;
      if (te[i][0] > te[i][1]) {
        tmp = te[i][0];
        te[i][0] = te[i][1];
        te[i][1] = tmp;
      }
    }
  }

  cnt = 0;
  for (i = 0; i < n; i++) {
    if (!ka[i]) {
      for (j = i + 1; j < n; j++) {
        if (te[i][0] == te[j][0] && te[i][1] == te[j][1] &&
            te[i][2] == te[j][2]) {
          cnt++;
          ka[j] = true;
        }
      }
    }
  }

  cout << cnt << endl;
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int main() {
  int te[1000 + 1][3];
  bool ka[1000 + 1] = {0};
  int n;
  int cnt;
  int i, j;
  int tmp;

  cin >> n;

  for (i = 0; i < n; i++) {
    cin >> te[i][0] >> te[i][1] >> te[i][2];
    if (te[i][0] > te[i][1]) {
      tmp = te[i][0];
      te[i][0] = te[i][1];
      te[i][1] = tmp;
    }
    if (te[i][1] > te[i][2]) {
      tmp = te[i][1];
      te[i][1] = te[i][2];
      te[i][2] = tmp;
      if (te[i][0] > te[i][1]) {
        tmp = te[i][0];
        te[i][0] = te[i][1];
        te[i][1] = tmp;
      }
    }
  }

  cnt = 0;
  for (i = 0; i < n; i++) {
    if (!ka[i]) {
      for (j = i + 1; j < n; j++) {
        if (te[i][0] == te[j][0] && te[i][1] == te[j][1] &&
            te[i][2] == te[j][2]) {
          cnt++;
          ka[j] = true;
        }
      }
    }
  }

  cout << cnt << endl;
  return 0;
}
Problem p00334 (C++)

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

int kumi[100][100][100] = {0};

int main() {
  int N;
  int count = 0;

  int p[3003];

  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> p[(3 * i)] >> p[(3 * i) + 1] >> p[(3 * i) + 2];
    sort(p + (3 * i), p + (3 * i) + 3);
    if (kumi[p[3 * i]][p[(3 * i) + 1]][p[(3 * i) + 2]] == 0) {
      kumi[p[3 * i]][p[(3 * i) + 1]][p[(3 * i) + 2]] = 1;
    } else {
      count++;
    }
  }
  cout << count << endl;
  return 0;
}

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

int kumi[1000][1000][1000] = {0};

int main() {
  int N;
  int count = 0;

  int p[3003];

  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> p[(3 * i)] >> p[(3 * i) + 1] >> p[(3 * i) + 2];
    sort(p + (3 * i), p + (3 * i) + 3);
    if (kumi[p[3 * i]][p[(3 * i) + 1]][p[(3 * i) + 2]] == 0) {
      kumi[p[3 * i]][p[(3 * i) + 1]][p[(3 * i) + 2]] = 1;
    } else {
      count++;
    }
  }
  cout << count << endl;
  return 0;
}
Problem p00334 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
class triangle {
public:
  int position[3];
};
bool compare(triangle &a, triangle &b) {
  for (int i = 0; i < 3; ++i) {
    if (a.position[i] < b.position[i]) {
      return true;
    } else if (a.position[i] > b.position[i]) {
      return false;
    }
  }
  return true;
}
void input(vector<triangle> &line) {
  int num;
  cin >> num;
  line.resize(num);
  for (int i = 0; i < num; ++i) {
    for (int j = 0; j < 3; ++j) {
      cin >> line[i].position[j];
    }
    sort(line[i].position, line[i].position + 3);
  }
  sort(line.begin(), line.end(), compare);
}
int func(vector<triangle> &line) {
  int res = 0;
  for (int i = 1; i < line.size(); ++i) {
    bool flag = true;
    for (int j = 0; j < 3; ++j) {
      if (line[i - 1].position[j] != line[i].position[j]) {
        flag = false;
        break;
      }
    }
    if (flag) {
      ++res;
    }
  }
  return res;
}
int main() {
  vector<triangle> line;
  input(line);
  cout << func(line) << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
class triangle {
public:
  int position[3];
};
bool compare(triangle &a, triangle &b) {
  for (int i = 0; i < 3; ++i) {
    if (a.position[i] < b.position[i]) {
      return true;
    } else if (a.position[i] > b.position[i]) {
      return false;
    }
  }
  return false;
}
void input(vector<triangle> &line) {
  int num;
  cin >> num;
  line.resize(num);
  for (int i = 0; i < num; ++i) {
    for (int j = 0; j < 3; ++j) {
      cin >> line[i].position[j];
    }
    sort(line[i].position, line[i].position + 3);
  }
  sort(line.begin(), line.end(), compare);
}
int func(vector<triangle> &line) {
  int res = 0;
  for (int i = 1; i < line.size(); ++i) {
    bool flag = true;
    for (int j = 0; j < 3; ++j) {
      if (line[i - 1].position[j] != line[i].position[j]) {
        flag = false;
        break;
      }
    }
    if (flag) {
      ++res;
    }
  }
  return res;
}
int main() {
  vector<triangle> line;
  input(line);
  cout << func(line) << endl;
  return 0;
}
Problem p00335 (C++)

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

#define int long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
#define all(v) (v).begin(), (v).end()
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
#define fi first
#define se second
template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

int n;
int p[5000];
int z[5000];
signed main() {
  while (cin >> n, n) {
    rep(i, n) cin >> p[i];

    int ans = 1001001001ll;
    for (int t = 0; t <= p[0]; t++) {
      int cost = t;
      memset(z, 0, sizeof(z));
      z[0] = t;
      for (int i = 1; i < n; i++) {
        cost += (p[i - 1] - z[i - 1]) * 2;
        z[i] += p[i - 1] - z[i - 1];
        chmin(z[i], p[i]);
      }
      cost += p[n - 1] - z[n - 1];
      chmin(ans, cost);
    }
    cout << ans << endl;
  }
  return 0;
}

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

#define int long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
#define all(v) (v).begin(), (v).end()
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
#define fi first
#define se second
template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

int n;
int p[5000];
int z[5000];
signed main() {
  while (cin >> n) {
    rep(i, n) cin >> p[i];

    int ans = 1001001001ll;
    for (int t = 0; t <= p[0]; t++) {
      int cost = t;
      memset(z, 0, sizeof(z));
      z[0] = t;
      for (int i = 1; i < n; i++) {
        cost += (p[i - 1] - z[i - 1]) * 2;
        z[i] += p[i - 1] - z[i - 1];
        chmin(z[i], p[i]);
      }
      cost += p[n - 1] - z[n - 1];
      chmin(ans, cost);
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00336 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int dp[1000][1000];
int main() {
  string t, b;
  cin >> t >> b;
  dp[0][0] = 0;
  int MOD = 1000000007;
  for (int i = 1; i <= b.size(); i++) {
    for (int j = 1 + i - 1; j <= t.size(); j++) {

      if (b[i - 1] == t[j - 1]) {
        if (i == 1)
          dp[i][j] = (dp[i][j - 1] + 1) % MOD;
        else
          dp[i][j] = (dp[i - 1][j - 1] + dp[i][j - 1]) % MOD;
      } else
        dp[i][j] = dp[i][j - 1] % MOD;
    }
  }
  cout << dp[b.size()][t.size()] % MOD << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int dp[1001][1001];
int main() {
  string t, b;
  cin >> t >> b;
  dp[0][0] = 0;
  int MOD = 1000000007;
  for (int i = 1; i <= b.size(); i++) {
    for (int j = 1 + i - 1; j <= t.size(); j++) {

      if (b[i - 1] == t[j - 1]) {
        if (i == 1)
          dp[i][j] = (dp[i][j - 1] + 1) % MOD;
        else
          dp[i][j] = (dp[i - 1][j - 1] + dp[i][j - 1]) % MOD;
      } else
        dp[i][j] = dp[i][j - 1] % MOD;
    }
  }
  cout << dp[b.size()][t.size()] % MOD << endl;
  return 0;
}
Problem p00336 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>

#define bigone t.size() + 1
#define bangone b.size() + 1
#define big t.size()
#define bang b.size()
#define USA 1000000007

using namespace std;

int P[1000][1000];

int main() {
  string t, b;
  cin >> t >> b;
  // P=プロフェッショナル

  for (int i = 0; i < bang + 1; i++) {
    P[0][i] = 0;
  }

  for (int i = 0; i < big + 1; i++) {
    P[i][0] = 1;
  }

  for (int i = 1; i < big + 1; i++) {
    char ti = t.at(i - 1);
    for (int j = 1; j < bang + 1; j++) {
      char bj = b.at(j - 1);
      if (j <= i) {
        if (ti == bj) {
          P[i][j] = (P[i - 1][j - 1] + P[i - 1][j]) % USA;
        } else {
          P[i][j] = P[i - 1][j];
        }
      } else {
        P[i][j] = 0;
      }
    }
  }
  cout << P[big][bang] << endl;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>

#define bigone t.size() + 1
#define bangone b.size() + 1
#define big t.size()
#define bang b.size()
#define USA 1000000007

using namespace std;

long long P[10000][10000];

int main() {
  string t, b;
  cin >> t >> b;
  // P=プロフェッショナル

  for (int i = 0; i < bang + 1; i++) {
    P[0][i] = 0;
  }

  for (int i = 0; i < big + 1; i++) {
    P[i][0] = 1;
  }

  for (int i = 1; i < big + 1; i++) {
    char ti = t.at(i - 1);
    for (int j = 1; j < bang + 1; j++) {
      char bj = b.at(j - 1);
      if (j <= i) {
        if (ti == bj) {
          P[i][j] = (P[i - 1][j - 1] + P[i - 1][j]) % USA;
        } else {
          P[i][j] = P[i - 1][j];
        }
      } else {
        P[i][j] = 0;
      }
    }
  }
  cout << P[big][bang] << endl;
}
Problem p00336 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> mp;
#define inf 1e9
ll mod = 1e9 + 7;

int main() {
  string t, b;
  cin >> t >> b;
  ll n1 = t.size(), n2 = b.size();
  vector<vector<ll>> dp(n1, vector<ll>(n2, 0));
  for (int i = 0; i < n1; i++) {
    if (t[i] == b[0])
      dp[i][0] = 1;
  }

  for (int i = 1; i < n2; i++) {
    ll sum = 0;
    for (int j = 0; j < i; j++) {
      sum += dp[j][i - 1];
      sum %= mod;
    }
    for (int j = i; j < n1; j++) {
      if (t[j] == b[i]) {
        dp[j][i] = sum;
      }
      sum += dp[j][i - 1];
      sum %= mod;
    }
  }
  ll res = 0;
  /*for(int i=0;i<n2;i++){
          for(int j=0;j<n1;j++){
                  cout<<dp[i][j]<<' ';
          }
          cout<<endl;
  }*/
  for (int i = 0; i < n1; i++) {
    res += dp[i][n2 - 1];
    res %= mod;
  }
  cout << res << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> mp;
#define inf 1e9
ll mod = 1e9 + 7;

int main() {
  string t, b;
  cin >> t >> b;
  ll n1 = t.size(), n2 = b.size();
  vector<vector<ll>> dp(n1, vector<ll>(n2, 0));
  for (int i = 0; i < n1; i++) {
    if (t[i] == b[0])
      dp[i][0] = 1;
  }
  if (n1 < n2) {
    cout << 0 << endl;
    return 0;
  }
  for (int i = 1; i < n2; i++) {
    ll sum = 0;
    for (int j = 0; j < i; j++) {
      sum += dp[j][i - 1];
      sum %= mod;
    }
    for (int j = i; j < n1; j++) {
      if (t[j] == b[i]) {
        dp[j][i] = sum;
      }
      sum += dp[j][i - 1];
      sum %= mod;
    }
  }
  ll res = 0;
  /*for(int i=0;i<n2;i++){
          for(int j=0;j<n1;j++){
                  cout<<dp[i][j]<<' ';
          }
          cout<<endl;
  }*/
  for (int i = 0; i < n1; i++) {
    res += dp[i][n2 - 1];
    res %= mod;
  }
  cout << res << endl;
  return 0;
}
Problem p00336 (C++)

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

long long int dp[1001][1001];

int main() {

  string t, b;

  cin >> t >> b;

  int tsize = t.size(), bsize = b.size();

  dp[0][0] = 1;

  for (int i = 0; i <= t.size(); i++) {
    for (int j = 0; j <= b.size(); j++) {
      if (t[i] == b[j]) {
        dp[i + 1][j + 1] += dp[i][j];
        dp[i + 1][j + 1] %= 1000000007;
      }
      dp[i + 1][j] += dp[i][j];
      dp[i + 1][j] %= 1000000007;
    }
  }

  cout << dp[tsize][bsize] << endl;

  return 0;
}

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

long long int dp[1002][1002];

int main() {

  string t, b;

  cin >> t >> b;

  int tsize = t.size(), bsize = b.size();

  dp[0][0] = 1;

  for (int i = 0; i <= t.size(); i++) {
    for (int j = 0; j <= b.size(); j++) {
      if (t[i] == b[j]) {
        dp[i + 1][j + 1] += dp[i][j];
        dp[i + 1][j + 1] %= 1000000007;
      }
      dp[i + 1][j] += dp[i][j];
      dp[i + 1][j] %= 1000000007;
    }
  }

  cout << dp[tsize][bsize] << endl;

  return 0;
}
Problem p00336 (C++)

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

using namespace std;

using ll = long long;

int main() {
  int A, B;
  string a, b;
  cin >> a >> b;
  A = a.size();
  B = b.size();

  vector<vector<ll>> dp(B, vector<ll>(A));
  constexpr ll MOD = ((1e9) + 7);

  for (int i = 0; i < B; i++) {
    for (int j = 0; j < A; j++) {
      if (i > 0)
        dp[i][j + 1] += dp[i][j];
      if (b[i] == a[j]) {
        dp[i + 1][j + 1] += (dp[i][j] + (i == 0));
        dp[i + 1][j + 1] %= MOD;
      }
    }
  }
  ll res = 0;
  for (int i = 0; i <= A; i++) {
    res += dp[B][i];
    res %= MOD;
  }
  cout << res << endl;
  return 0;
}

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

using namespace std;

using ll = long long;

int main() {
  int A, B;
  string a, b;
  cin >> a >> b;
  A = a.size();
  B = b.size();

  vector<vector<ll>> dp(B + 1, vector<ll>(A + 1));
  constexpr ll MOD = ((1e9) + 7);

  for (int i = 0; i < B; i++) {
    for (int j = 0; j < A; j++) {
      if (i > 0)
        dp[i][j + 1] += dp[i][j];
      if (b[i] == a[j]) {
        dp[i + 1][j + 1] += (dp[i][j] + (i == 0));
        dp[i + 1][j + 1] %= MOD;
      }
    }
  }
  ll res = 0;
  for (int i = 0; i <= A; i++) {
    res += dp[B][i];
    res %= MOD;
  }
  cout << res << endl;
  return 0;
}
Problem p00342 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int a[1001], i, ma = 100000, p, n;
double m1, m2;
int main() {
  for (cin >> n; i < n; i++)
    cin >> a[i];
  sort(a, a + n);
  m1 = (double)(a[n - 1] + a[n - 2]) / (a[1] - a[0]);
  for (i = n - 1; i > 0; i--)
    if (a[i] - a[i - 1] <= ma)
      ma = a[i] - a[i++ - 1];
  for (i = n - 1; i > 0; i -= 2)
    if (i != p) {
      m2 = (double)(a[i - 1] + a[i]) / (a[p] - a[p - 1]);
      break;
    }
  m1 = max(m1, m2);
  printf("%.9f\n", m1);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int a[1001], i, ma = 100000, p, n;
double m1, m2;
int main() {
  for (cin >> n; i < n; i++)
    cin >> a[i];
  sort(a, a + n);
  m1 = (double)(a[n - 1] + a[n - 2]) / (a[1] - a[0]);
  for (i = n - 1; i > 0; i--)
    if (a[i] - a[i - 1] <= ma) {
      ma = a[i] - a[i - 1];
      p = i;
    }
  for (i = n - 1; i > 0; i -= 2)
    if (i != p) {
      m2 = (double)(a[i - 1] + a[i]) / (a[p] - a[p - 1]);
      break;
    }
  m1 = max(m1, m2);
  printf("%.9f\n", m1);
}
Problem p00344 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define yesno(flg)                                                             \
  if (flg) {                                                                   \
    cout << "yes" << endl;                                                     \
  } else {                                                                     \
    cout << "no" << endl;                                                      \
  }
#define vi vector<int>
#define MAX_N 200005
#define i197 1000000007

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P1;
typedef pair<int, P1> P2;
const int inf = 1000000000;
P1 xy[MAX_N] = {};
bool dp[100001] = {};

int main() {
  int n;
  cin >> n;
  int a[100001];
  rep(i, n) cin >> a[i];
  int t = 0;

  rep(i, n) {
    if (dp[i])
      continue;
    bool dp2[100001] = {};
    dp[i] = true;
    dp2[i] = true;
    int k = (i + a[i]) % n;
    while (1) {
      if (dp2[k]) {
        if (k == i) {
          // success
          rep(j, n) {
            if (dp2[j]) {
              t++;
              dp[j] = true;
            }
          }
        }
        break;
      }
      if (dp[k]) {
        //				rep(j,n){
        //					if(dp2[j])dp[j]=true;
        //					//false
        //				}
        //				break;
      }
      dp2[k] = true;
      k = (k + a[k]) % n;
    }
  }
  cout << t << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define yesno(flg)                                                             \
  if (flg) {                                                                   \
    cout << "yes" << endl;                                                     \
  } else {                                                                     \
    cout << "no" << endl;                                                      \
  }
#define vi vector<int>
#define MAX_N 200005
#define i197 1000000007

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P1;
typedef pair<int, P1> P2;
const int inf = 1000000000;
P1 xy[MAX_N] = {};
bool dp[100001] = {};

int main() {
  int n;
  cin >> n;
  int a[100001];
  rep(i, n) cin >> a[i];
  int t = 0;

  rep(i, n) {
    if (dp[i])
      continue;
    bool dp2[100001] = {};
    dp[i] = true;
    dp2[i] = true;
    int k = (i + a[i]) % n;
    while (1) {
      if (dp2[k]) {
        if (k == i) {
          // success
          t++;
          k = (k + a[k]) % n;
          while (k != i) {
            t++;
            dp[k] = true;
            k = (k + a[k]) % n;
          }
        }
        break;
      }
      if (dp[k]) {
        //				rep(j,n){
        //					if(dp2[j])dp[j]=true;
        //					//false
        //				}
        //				break;
      }
      dp2[k] = true;
      k = (k + a[k]) % n;
    }
  }
  cout << t << endl;
  return 0;
}