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

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

int wmax, n;
vector<int> v(1000), w(1000);

int main() {
  int cnt = 0;
  while (cin >> wmax >> n) {
    for (int i = 0; i < n; ++i) {
      string tmp;
      getline(cin, tmp, ',');
      v[i] = atoi(tmp.c_str());
      getline(cin, tmp);
      w[i] = atoi(tmp.c_str());
    }

    vector<vector<int>> res(wmax + 1, vector<int>(n));

    for (int c = 0; c < n; ++c) {
      for (int r = 0; r < wmax + 1; ++r) {
        if (c == 0) {
          res[r][c] = (r < w[0]) ? 0 : v[0];
        } else {
          if (r < w[c]) {
            res[r][c] = res[r][c - 1];
          } else {
            int v1 = res[r][c - 1];
            int v2 = res[r - w[c]][c - 1] + v[c];
            res[r][c] = (v1 < v2) ? v2 : v1;
          }
        }
      }
    }

    int res_v = 0;
    int res_w = wmax;
    for (int r = 0; r < wmax + 1; ++r) {
      for (int c = 0; c < n; ++c) {
        if (res_v < res[r][c]) {
          res_v = res[r][c];
          res_w = r;
        }
      }
    }

    cout << "Case " << ++cnt << ":" << endl;
    cout << res_v << endl;
    cout << res_w << endl;
  }
  return EXIT_SUCCESS;
}


Error: TLE

Fixed submission:

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

pair<int, int> hoge(vector<pair<int, int>> &c, int i, int ww, int w, int v,
                    pair<int, int> d[]) {
  if (i == int(c.size()) || ww < w)
    return make_pair(w, v);
  else if (0 < i && d[i - 1].first <= w && v < d[i - 1].second)
    return make_pair(w, v);

  pair<int, int> c0, c1, c2;
  c1 = hoge(c, i + 1, ww, w + c[i].first, v + c[i].second, d);
  c2 = hoge(c, i + 1, ww, w, v, d);

  if (ww < c1.first) {
    c0 = c2;
  } else if (c1.second == c2.second) {
    c0 = (c1.first < c2.first) ? c1 : c2;
  } else {
    c0 = (c1.second < c2.second) ? c2 : c1;
  }
  return d[i] = c0;
}

int main(int argc, char const *argv[]) {
  int ww, nn;
  for (int i = 0;; ++i) {
    vector<pair<int, int>> container;
    cin >> ww;
    if (ww == 0)
      break;
    cin >> nn;
    for (int i = 0; i < nn; ++i) {
      int v, w;
      char c;
      cin >> v >> c >> w;
      container.push_back(make_pair(w, v));
    }
    pair<int, int> d[1000] = {};
    pair<int, int> r = hoge(container, 0, ww, 0, 0, d);
    cout << "Case " << i + 1 << ":" << endl;
    cout << r.second << endl;
    cout << r.first << endl;
  }

  return 0;
}
Problem p00042 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < j; i++)
#define FOR(i, j, k) for (int i = j; i < k; i++)
#define P pair<int, int>
const int INF = INT_MAX / 2;

int main() {
  int w, c = 1;
  while (cin >> w && w) {
    int n;
    cin >> n;
    vector<vector<int>> dp(n + 1, vector<int>(w + 1));
    REP(i, dp.size()) {
      REP(j, dp[i].size()) { dp[i][j] = 0; }
    }
    dp[0][0] = 0;
    // input begin
    vector<P> j(n + 1);
    string str;
    getline(cin, str);
    FOR(i, 1, n + 1) {
      getline(cin, str);
      stringstream ss(str);
      int a, b;
      char c;
      ss >> a >> c >> b;
      j[i].first = a;
      j[i].second = b;
    }
    // input end

    FOR(i, 1, dp.size()) {
      int value = j[i].first, weight = j[i].second;
      REP(j, dp[i].size()) {
        if (j >= weight) {
          dp[i][j] = max(dp[i][j], dp[i - 1][j - weight] + value);
        }
        dp[i][j] = max(dp[i][j], dp[i - 1][j]);
      }
    }

    int ans_v = -1, ans_w = 0;
    REP(i, dp[0].size()) {
      if (dp[n][i] > ans_v) {
        ans_v = dp[n][i];
        ans_w = i;
      }
    }
    cout << "Case " << c << ":" << endl << ans_v << endl << ans_w << endl;
    c++;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < j; i++)
#define FOR(i, j, k) for (int i = j; i < k; i++)
#define P pair<int, int>
const int INF = INT_MAX / 2;
const int MAX_N = 1000;

int w, n, ans_w = INF, ans = -1;

void loop(int now, int cnt, int sum_w, int sum, vector<P> j) {
  if (now >= n) {
    if (ans < sum) {
      ans = sum;
      ans_w = sum_w;
    } else if (ans == sum) {
      if (ans_w > sum_w) {
        ans = sum;
        ans_w = sum_w;
      }
    }
  } else {
    loop(now + 1, cnt, sum_w, sum, j);
    if (sum_w + j[now].second <= w)
      loop(now + 1, cnt + 1, sum_w + j[now].second, sum + j[now].first, j);
  }
}

int main() {
  int c = 1;
  while (cin >> w && w) {
    ans = -1;
    // input begin
    cin >> n;
    vector<P> j(n);
    string str;
    getline(cin, str);
    REP(i, n) {
      getline(cin, str);
      stringstream ss(str);
      int a, b;
      char c;
      ss >> a >> c >> b;
      j[i].first = a;
      j[i].second = b;
    }
    // input end

    loop(0, 0, 0, 0, j);
    cout << "Case " << c << ":" << endl << ans << endl << ans_w << endl;
    c++;
  }
  return 0;
}
Problem p00042 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
int dp[1001][1001];
int main(void) {
  int i, j, k, l;
  int w, n;
  int max;
  int a[1001], b[1001];

  scanf("%d", &w);
  l = 1;
  while (w != 0) {
    max = 0;
    scanf("%d", &n);
    for (i = 0; i <= n; i++) {
      for (j = 0; j <= w; j++) {
        dp[i][j] = -1;
      }
    }
    for (i = 0; i < n; i++) {
      scanf("%d,%d", &a[i], &b[i]);
    }
    for (i = 0; i < n; i++) {
      dp[i][0] = 0;
      for (j = 0; j <= w; j++) {
        if (dp[i][j] != -1) {
          if (j + b[i] <= w && dp[i][j] + a[i] >= dp[i + 1][j + b[i]]) {
            dp[i + 1][j + b[i]] = dp[i][j] + a[i];
          }
          if (dp[i + 1][j] < dp[i][j]) {
            dp[i + 1][j] = dp[i][j];
          }
        }
      }
    }
    for (j = 0; j <= w; j++) {
      if (dp[n][j] > max) {
        max = dp[n][j];
        k = j;
      }
    }
    printf("Case %d:\\n", l);
    printf("%d\\n%d\\n", max, k);
    scanf("%d", &w);
    l++;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
int dp[1001][1001];
int main(void) {
  int i, j, k, l;
  int w, n;
  int max = 0;
  int a[1001], b[1001];

  scanf("%d", &w);
  scanf("%d", &n);
  l = 1;
  while (w != 0) {
    for (i = 0; i <= n; i++) {
      for (j = 0; j <= 1001; j++) {
        dp[i][j] = -1;
      }
    }
    for (i = 0; i < n; i++) {
      scanf("%d,%d", &a[i], &b[i]);
    }
    for (i = 0; i <= n; i++) {
      dp[i][0] = 0;
      for (j = 0; j <= w; j++) {
        if (dp[i][j] != -1) {
          if (dp[i][j] + a[i] >= dp[i + 1][j + b[i]]) {
            dp[i + 1][j + b[i]] = dp[i][j] + a[i];
          }
          if (dp[i + 1][j] < dp[i][j]) {
            dp[i + 1][j] = dp[i][j];
          }
        }
      }
    }
    for (j = 0; j <= w; j++) {
      if (dp[n][j] > max) {
        max = dp[n][j];
        k = j;
      }
    }
    printf("Case %d:\\n", l);
    printf("%d\\n%d\\n", max, k);
    scanf("%d", &w);
  }
  return 0;
}
Problem p00042 (C++) — buggy submission, verdict: Runtime Error

// バグ有

#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int W, N;
  char d;
  int counter = 1;
  int takara[1000][2];
  while (1) {
    cin >> W;
    if (W == 0) {
      break;
    }
    cin >> N;
    for (int i = 0; i < N; i++) {
      cin >> takara[i][0] >> d >> takara[i][1];
    }
    vector<vector<int>> dp(W + 1, vector<int>(N + 1));
    for (int i = 0; i <= N; i++) {
      for (int j = 0; j <= W; j++) {
        dp[j][i] = 0;
      }
    }
    for (int i = 0; i <= N; i++) {
      for (int j = 0; j <= W; j++) {
        if (i == 0) {
          continue;
        }
        if (j < takara[i - 1][1]) {
          dp[j][i] = dp[j][i - 1];
        } else {
          dp[j][i] = max(dp[j][i - 1],
                         dp[j - takara[i - 1][1]][i - 1] + takara[i - 1][0]);
        }
      }
    }
    int MAX[2];
    MAX[0] = 0;
    MAX[1] = 10000;
    for (int i = 0; i <= W; i++) {
      if (MAX[0] == dp[i][N]) {
        MAX[1] = min(MAX[1], i);
      } else if (MAX[0] < dp[i][N]) {
        MAX[0] = dp[i][N];
        MAX[1] = i;
      }
    }
    cout << "Case " << counter << ':' << endl
         << MAX[0] << endl
         << MAX[1] << endl;
    counter++;
  }
  return 0;
}

Error: -11

Fixed submission:

// バグ有

#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int W, N;
  char d;
  int counter = 1;
  int takara[1000][2];
  while (1) {
    cin >> W;
    if (W == 0) {
      break;
    }
    cin >> N;
    for (int i = 0; i < N; i++) {
      cin >> takara[i][0] >> d >> takara[i][1];
    }
    vector<vector<int>> dp(W, vector<int>(N + 1));
    for (int i = 0; i <= N; i++) {
      for (int j = 0; j <= W; j++) {
        dp[j][i] = 0;
      }
    }
    for (int i = 0; i <= N; i++) {
      for (int j = 0; j <= W; j++) {
        if (i == 0) {
          continue;
        }
        if (j < takara[i - 1][1]) {
          dp[j][i] = dp[j][i - 1];
        } else {
          dp[j][i] = max(dp[j][i - 1],
                         dp[j - takara[i - 1][1]][i - 1] + takara[i - 1][0]);
        }
      }
    }
    int MAX[2];
    MAX[0] = 0;
    MAX[1] = 10000;
    for (int i = 0; i <= W; i++) {
      if (MAX[0] == dp[i][N]) {
        MAX[1] = min(MAX[1], i);
      } else if (MAX[0] < dp[i][N]) {
        MAX[0] = dp[i][N];
        MAX[1] = i;
      }
    }
    cout << "Case " << counter << ':' << endl
         << MAX[0] << endl
         << MAX[1] << endl;
    counter++;
  }
  return 0;
}
Problem p00042 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
using namespace std;

typedef struct {
  int value;
  int weight;
} SET;

int main() {
  int cnt = 1;

  while (1) {

    int w, n;
    SET data[1111];
    int memo[1111][1111] = {{0}};

    scanf("%d", &w);
    if (w == 0)
      break;
    scanf("%d", &n);

    for (int i = 0; i < n; i++) {
      scanf("%d,%d", &data[i].value, &data[i].weight);
    }

    memo[0][data[0].weight] = data[0].value;
    int max_value = data[0].value;
    int max_weight = data[0].weight;

    for (int i = 1; i < n; i++) {
      if (data[i].value > memo[i - 1][data[i].weight]) {
        memo[i][data[i].weight] = data[i].value;
      }
      for (int j = 1; j <= w; j++) {
        if (memo[i - 1][j] > 0 && memo[i - 1][j] > memo[i][j]) {
          memo[i][j] = memo[i - 1][j];
        }

        if (j + data[i].weight <= w &&
            memo[i - 1][j + data[i].weight] < data[i].value + memo[i - 1][j]) {
          memo[i][j + data[i].weight] = data[i].value + memo[i - 1][j];
        }

        if (memo[i][j] == max_value) {
          if (max_weight > j) {
            max_weight = j;
          }
        }
        if (memo[i][j] > max_value) {
          max_value = memo[i][j];
          max_weight = j;
        }
      }
    }
    printf("Case %d:\\n", cnt);
    printf("%d\\n", max_value);
    printf("%d\\n", max_weight);
    cnt++;
  }

  return 0;
}

Error: -11

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;

typedef struct {
  int g;
  int ye;
} SET;

int main() {

  int n, w;
  int cnt = 1;
  while (1) {

    scanf("%d", &w);
    if (w == 0)
      break;
    scanf("%d", &n);
    SET data[1111] = {0};
    int memo[1111][11111] = {{0}};
    int max_y = 0;
    int max_g = 1111;

    for (int i = 0; i < n; i++) {
      scanf("%d,%d", &data[i].ye, &data[i].g);
    }
    memo[0][data[0].g] = data[0].ye;
    for (int i = 1; i < n; i++) {
      if (data[i].ye > memo[i - 1][data[i].g]) {
        memo[i][data[i].g] = data[i].ye;
      }
      for (int j = 1; j <= w; j++) {
        if (memo[i - 1][j] > 0) {
          if (memo[i - 1][j] > memo[i][j]) {
            memo[i][j] = memo[i - 1][j];
          }
          if (j + data[i].g <= w) {
            memo[i][j + data[i].g] = memo[i - 1][j] + data[i].ye;
          }
        }
        if (max_y <= memo[i][j]) {
          if (max_y == memo[i][j]) {
            if (max_g > j) {
              max_g = j;
            }
          } else {
            max_g = j;
            max_y = memo[i][j];
          }
        }
      }
    }
    printf("Case %d:\\n%d\\n%d\\n", cnt, max_y, max_g);
    cnt++;
  }
}
Problem p00043 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

#define loop(i, a, b) for (int i = (a); i < int(b); i++)
#define rep(i, b) loop(i, 0, b)

int v[14];

int t[][3] = {{0, 0, 0}, {1, 1, 1}, {2, 2, 2}, {3, 3, 3}, {4, 4, 4},
              {5, 5, 5}, {6, 6, 6}, {7, 7, 7}, {8, 8, 8},

              {0, 1, 2}, {1, 2, 3}, {2, 3, 4}, {3, 4, 5}, {4, 5, 6},
              {5, 6, 7}, {6, 7, 8}};

bool dfs(vector<int> rest, int depth) {
  if (depth == 4) {
    int ze = 0, tw = 0;
    for (int x : rest)
      if (x == 0)
        ze++;
      else if (x == 2)
        tw++;
    return ze == 8 && tw == 1;
  }
  bool res = false;
  rep(i, 16) {
    for (int x : t[i])
      rest[x]--;
    for (int x : rest)
      if (x < 0)
        goto UNDO;
    if (dfs(rest, depth + 1)) {
      res = true;
      goto END;
    }
  UNDO:;
    for (int x : t[i])
      rest[x]++;
  }
END:;
  return res;
}

vector<int> solve() {
  vector<int> ans;
  vector<int> rest(9);
  rep(i, 13) rest[v[i]]++;
  rep(i, 9) {
    if (rest[i] != 4) {
      rest[i]++;
      if (dfs(rest, 0))
        ans.push_back(i + 1);
      rest[i]--;
    }
  }
  return ans;
}

int main() {
  char buf[20];
  while (cin >> buf) {
    rep(i, 13) v[i] = buf[i] - '0' - 1;
    vector<int> ans = solve();
    if (ans.size()) {
      rep(i, ans.size()) {
        cout << ans[i];
        if (i == ans.size() - 1)
          cout << endl;
        else
          cout << " ";
      }
    } else
      cout << 0 << endl;
  }
}

Error: TLE

Fixed submission:

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

using namespace std;

#define loop(i, a, b) for (int i = (a); i < (int)(b); i++)
#define rep(i, b) loop(i, 0, b)
#define all(c) (c).begin(), (c).end()

typedef vector<int> vi;
typedef vector<vi> vvi;

const vvi r3{{1, 1, 1}, {2, 2, 2}, {3, 3, 3}, {4, 4, 4}, {5, 5, 5}, {6, 6, 6},
             {7, 7, 7}, {8, 8, 8}, {9, 9, 9}, {1, 2, 3}, {2, 3, 4}, {3, 4, 5},
             {4, 5, 6}, {5, 6, 7}, {6, 7, 8}, {7, 8, 9}};

const vvi r2{{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5},
             {6, 6}, {7, 7}, {8, 8}, {9, 9}};

int n = r3.size();
int m = r2.size();

bool ok(vi const &s) {
  rep(i, n) rep(j, n) rep(k, n) rep(l, n) rep(p, m) {
    vi take;
    take.reserve(14);
    take.insert(take.end(), all(r3[i]));
    take.insert(take.end(), all(r3[j]));
    take.insert(take.end(), all(r3[k]));
    take.insert(take.end(), all(r3[l]));
    take.insert(take.end(), all(r2[p]));
    sort(all(take));
    if (take == s)
      return true;
  }
  return false;
}

int main() {
  string str;
  while (cin >> str) {
    vi ans;
    vi s(14);
    rep(i, 13) s[i] = str[i] - '0';
    loop(i, 1, 10) {
      s[13] = i;
      sort(all(s));
      if (ok(s))
        ans.push_back(i);
    }
    if (ans.size()) {
      rep(i, ans.size() - 1) cout << ans[i] << " ";
      cout << ans.back() << endl;
    } else {
      cout << 0 << endl;
    }
  }
}
Problem p00043 (C++) — buggy submission, verdict: Runtime Error

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

int u[10];

bool solve() {
  bool f = false;
  int sum = 0;
  for (int i = 1; i < 10; i++)
    sum += u[i];
  if (sum == 0)
    return true;
  for (int i = 1; i < 10; i++) {
    if (u[i] > 2) {
      u[i] -= 3;
      if (solve()) {
        f = true;
      }
      u[i] += 3;
    }
  }
  for (int i = 1; i < 8; i++) {
    if (u[i] > 0 && u[i + 1] > 0 && u[i + 2] > 0) {
      u[i]--;
      u[i + 1]--;
      u[i + 2]--;
      if (solve()) {
        f = true;
      }
      u[i]++;
      u[i + 1]++;
      u[i + 2]++;
    }
  }
  return f;
}

int main(void) {
  char buff[20];
  while (scanf("%s", buff) != EOF) {
    int d[10] = {0};
    vector<int> v;
    for (int i = 0; i < 13; i++) {
      d[buff[i] - '0']++;
    }
    for (int i = 1; i < 10; i++) {
      for (int j = 1; j < 10; j++) {
        u[j] = d[j];
      }
      if (u[i] == 4)
        continue;
      if (u[i] > 4) {
        v.clear();
        break;
      }
      u[i]++;
      if (u[i] > 4)
        continue;
      bool f = false;
      for (int j = 1; j < 10; j++) {
        if (u[j] > 1) {
          u[j] -= 2;
          if (solve()) {
            f = true;
          }
          u[j] += 2;
        }
      }
      if (f)
        v.push_back(i);
    }
    if (v.size() == 0) {
      cout << "0" << endl;
      continue;
    }
    for (int i = 0; i < v.size() - 1; i++)
      cout << v[i] << " ";
    cout << v[v.size() - 1] << endl;
  }
}

Error: -11

Fixed submission:

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

int u[10];

bool solve() {
  bool f = false;
  int sum = 0;
  for (int i = 1; i < 10; i++)
    sum += u[i];
  if (sum == 0)
    return true;
  for (int i = 1; i < 10; i++) {
    if (u[i] > 2) {
      u[i] -= 3;
      if (solve()) {
        f = true;
      }
      u[i] += 3;
    }
  }
  for (int i = 1; i < 8; i++) {
    if (u[i] > 0 && u[i + 1] > 0 && u[i + 2] > 0) {
      u[i]--;
      u[i + 1]--;
      u[i + 2]--;
      if (solve()) {
        f = true;
      }
      u[i]++;
      u[i + 1]++;
      u[i + 2]++;
    }
  }
  return f;
}

int main(void) {
  char buff[20];
  while (scanf("%s", buff) != EOF) {
    int d[10] = {0};
    vector<int> v;
    for (int i = 0; i < 13; i++) {
      d[buff[i] - '0']++;
    }
    for (int i = 1; i < 10; i++) {
      for (int j = 1; j < 10; j++) {
        u[j] = d[j];
      }
      if (u[i] == 4)
        continue;
      if (u[i] > 4) {
        v.clear();
        break;
      }
      u[i]++;
      if (u[i] > 4)
        continue;
      bool f = false;
      for (int j = 1; j < 10; j++) {
        if (u[j] > 1) {
          u[j] -= 2;
          if (solve()) {
            f = true;
          }
          u[j] += 2;
        }
      }
      if (f)
        v.push_back(i);
    }
    if (v.size() == 0)
      cout << "0" << endl;
    for (int i = 0; i < v.size() - 1; i++)
      cout << v[i] << " ";
    cout << v[v.size() - 1] << endl;
  }
}
Problem p00044 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
#define MAX 50100

int main(void) {
  bool prime[MAX] = {false};

  prime[0] = prime[1] = true;
  for (int i = 2; i * i <= MAX - 1; i++) {
    if (!prime[i]) {
      for (int j = i * 2; j <= MAX - 1; j += i) {
        prime[j] = true;
      }
    }
  }

  int n;
  while (cin >> n) {
    for (int i = n - 1;; i--) {
      if (!prime[i]) {
        cout << i << ' ';
        break;
      }
    }
    for (int i = n + 1;; i++) {
      if (!prime[i]) {
        cout << i << endl;
        break;
      }
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
#define MAX 50010

int prime[MAX];

void p(void) {
  int num = 0, judge[MAX] = {0};
  judge[0] = judge[1] = 1;
  for (int i = 2; i * i < MAX; i++) {
    if (judge[i] == 0) {
      for (int j = i * 2; j < MAX; j += i) {
        judge[j] = 1;
      }
    }
  }

  for (int i = 2; i < MAX; i++) {
    if (judge[i] == 0) {
      prime[num] = i;
      num++;
    }
  }
}

int main(void) {
  int n;

  p();
  while (cin >> n) {
    int key = 1;
    while (1) {
      if (n == prime[key]) {
        cout << prime[key - 1] << ' ' << prime[key + 1] << endl;
        break;
      } else
        key++;
    }
  }

  return 0;
}
Problem p00044 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  bool dt[51001];
  dt[0] = false;
  dt[1] = false;
  dt[2] = true;
  int count = 0;
  for (int i = 3; i < 51001; i++) {
    for (int j = 2; j < i && j < 5000; j++) {
      if (dt[j] == true) {
        if (i % j == 0)
          count++;
      }
    }
    if (count == 0) {
      dt[i] = true;
    } else {
      dt[i] = false;
    }
    count = 0;
  }
  int n = 0;
  while (cin >> n) {
    int i = 0;
    int ansf = 0, ansl = 0;
    for (i = 1; i < n; i++) {
      if (dt[i] == true) {
        ansf = i;
      }
    }
    i++;
    while (i > 0) {
      if (dt[i] == true) {
        ansl = i;
        i = -10;
      }
      i++;
    }
    cout << ansf << " " << ansl << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  bool dt[51001];
  int count = 0;
  for (int i = 1; i < 51001; i++) {
    for (int j = 1; j < 7001; j++) {
      if (i % j == 0)
        count++;
    }
    if (count == 2) {
      dt[i] = true;
    } else {
      dt[i] = false;
    }
    count = 0;
  }
  int n = 0;
  while (cin >> n) {
    int i = 0;
    int ansf = 0, ansl = 0;
    for (i = 1; i < n; i++) {
      if (dt[i] == true) {
        ansf = i;
      }
    }
    i++;
    while (i > 0) {
      if (dt[i] == true) {
        ansl = i;
        i = -10;
      }
      i++;
    }
    cout << ansf << " " << ansl << endl;
  }
  return 0;
}
Problem p00044 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int main() {
  bool is_prime[1000000];
  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i < 1000000; i++) {
    is_prime[i] = true;
  }

  for (int i = 2; i * i < 1000000; i++) {
    if (is_prime[i]) {
      for (int j = i * i; j < 1000000; j += i) {
        is_prime[j] = false;
      }
    }
  }

  while (1) {
    int n;
    cin >> n;

    if (cin.eof())
      break;

    int i = n - 1, j = n + 1;
    while (1) {
      if (is_prime[i]) {
        break;
      }
      i -= 1;
    }

    while (1) {
      if (is_prime[j])
        break;
      j++;
    }
    cout << i << ' ' << j << endl;
  }
  return 0;
}


Error: -11

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  bool is_prime[100000];

  is_prime[0] = is_prime[1] = false;
  for (int i = 2; i < 1000000; i++) {
    is_prime[i] = true;
  }

  for (int i = 2; i * i < 1000000; i++) {
    if (is_prime[i]) {
      for (int j = i * i; j < 1000000; j += i) {
        is_prime[j] = false;
      }
    }
  }

  while (1) {
    int n;
    cin >> n;

    if (cin.eof())
      break;

    int i, j;
    while (1) {
      i = n - 1;
      if (is_prime[i])
        break;
      i--;
    }

    while (1) {
      j = n + 1;
      if (is_prime[j])
        break;
      j++;
    }

    cout << i << ' ' << j << endl;
  }
  return 0;
}
Problem p00044 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
int table[55000];

int main() {
  for (int i = 2; i < 1000; i++) {
    if (table[i] == 0) {
      for (int j = i * 2; j <= 55000; j += i) {
        table[j] = 1;
      }
    }
  }
  int a;
  while (~scanf("%d", &a)) {
    int i;
    for (i = a - 1; table[i] == 1; i--)
      ;
    printf("%d ", i);
    for (i = a + 1; table[i] == 1; i++)
      ;
    printf("%d\\n", i);
  }
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int table[55000] = {};
  for (int i = 2; i * i < 55000; ++i) {
    if (!table[i]) {
      for (int j = i * 2; j <= 55000; j += i) {
        table[j] = 1;
      }
    }
  }

  int n;
  while (cin >> n, n) {
    int i;
    for (i = n - 1; table[i] == 1; --i)
      ;
    cout << i << " ";
    for (i = n + 1; table[i] == 1; ++i)
      ;
    cout << i << endl;
  }
  return 0;
}