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

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

const int MAX = 100;
const int INF = (1 << 10);

int main() {
  int n, m;
  int s, g1, g2;
  int pipe[MAX][MAX];

  while (1) {
    scanf("%d%d%d%d%d", &n, &m, &s, &g1, &g2);

    if (n == 0 && m == 0 && s == 0 && g1 == 0 && g2 == 0)
      break;

    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= n; j++) {
        pipe[i][j] = INF;
      }
      pipe[i][i] = 0;
    }

    for (int i = 0; i < m; i++) {
      int b1, b2, c;
      scanf("%d%d%d", &b1, &b2, &c);
      pipe[b1][b2] = c;
    }

    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        for (int k = 1; k <= n; k++) {
          pipe[j][k] = min(pipe[j][k], pipe[j][i] + pipe[i][k]);
        }
      }
    }
    int ans = INF;
    for (int i = 1; i <= n; i++) {
      ans = min(ans, pipe[s][i] + pipe[i][g1] + pipe[i][g2]);
    }
    printf("%d\n", ans);
  }
}

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

const int MAX = 101;
const int INF = (1 << 21);

int main() {
  int n, m;
  int s, g1, g2;
  int pipe[MAX][MAX];

  while (1) {
    scanf("%d%d%d%d%d", &n, &m, &s, &g1, &g2);

    if (n == 0 && m == 0 && s == 0 && g1 == 0 && g2 == 0)
      break;

    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= n; j++) {
        pipe[i][j] = INF;
      }
      pipe[i][i] = 0;
    }

    for (int i = 0; i < m; i++) {
      int b1, b2, c;
      scanf("%d%d%d", &b1, &b2, &c);
      pipe[b1][b2] = c;
    }

    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        for (int k = 1; k <= n; k++) {
          pipe[j][k] = min(pipe[j][k], pipe[j][i] + pipe[i][k]);
        }
      }
    }
    int ans = INF;
    for (int i = 1; i <= n; i++) {
      ans = min(ans, pipe[s][i] + pipe[i][g1] + pipe[i][g2]);
    }
    printf("%d\n", ans);
  }
}
Problem p01130 (C++)

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

template <class T> inline void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}

constexpr int MAX_N = 100;
constexpr int INF = (1 << 29);
int dist[MAX_N][MAX_N];

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

  for (int n, m, s, g1, g2; cin >> n >> m >> s >> g1 >> g2 && n;) {
    --s;
    --g1;
    --g2;
    fill_n((int *)dist, sizeof(dist) / sizeof(int), INF);

    for (int i = 0; i < m; ++i) {
      int from, to, cost;
      cin >> from >> to >> cost;
      dist[from - 1][to - 1] = cost;
    }

    for (int k = 0; k < n; ++k) {
      for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
          chmin(dist[i][j], dist[i][k] + dist[k][j]);
        }
      }
    }

    int ans = INF;
    for (int i = 0; i < n; ++i) {
      chmin(ans, dist[s][i] + dist[i][g1] + dist[i][g2]);
    }

    assert(ans != INF);
    cout << ans << endl;
  }

  return EXIT_SUCCESS;
}

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

template <class T> inline void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}

constexpr int MAX_N = 100;
constexpr int INF = (1 << 29);
int dist[MAX_N][MAX_N];

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

  for (int n, m, s, g1, g2; cin >> n >> m >> s >> g1 >> g2 && n;) {
    --s;
    --g1;
    --g2;
    fill_n((int *)dist, sizeof(dist) / sizeof(int), INF);

    for (int i = 0; i < n; ++i) {
      dist[i][i] = 0;
    }

    for (int i = 0; i < m; ++i) {
      int from, to, cost;
      cin >> from >> to >> cost;
      dist[from - 1][to - 1] = cost;
    }

    for (int k = 0; k < n; ++k) {
      for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
          chmin(dist[i][j], dist[i][k] + dist[k][j]);
        }
      }
    }

    int ans = INF;
    for (int i = 0; i < n; ++i) {
      chmin(ans, dist[s][i] + dist[i][g1] + dist[i][g2]);
    }

    assert(ans != INF);
    cout << ans << endl;
  }

  return EXIT_SUCCESS;
}
Problem p01131 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <string>
using namespace std;
string table[] = {"",    ".,!? ", "abc",  "def", "ghi",
                  "jkl", "mno",   "pqrs", "tuv", "wxyz"};
char str[128];
int main() {
  int a;
  scanf("%d", &a);
  while (a--) {
    scanf("%s", str);
    int renzoku = 0;
    char now = '0';
    for (int i = 0; str[i]; i++) {
      if (now != str[i]) {
        if (now != '0') {
          printf("%c",
                 table[now - '0'][(renzoku - 1) % table[now - '0'].size()]);
        }
        renzoku = 1;
        now = str[i];
      } else
        renzoku++;
    }
    printf("\n");
  }
}

Fixed submission:
#include <stdio.h>
#include <string>
using namespace std;
string table[] = {"",    ".,!? ", "abc",  "def", "ghi",
                  "jkl", "mno",   "pqrs", "tuv", "wxyz"};
char str[1280];
int main() {
  int a;
  scanf("%d", &a);
  while (a--) {
    scanf("%s", str);
    int renzoku = 0;
    char now = '0';
    for (int i = 0; str[i]; i++) {
      if (now != str[i]) {
        if (now != '0') {
          printf("%c",
                 table[now - '0'][(renzoku - 1) % table[now - '0'].size()]);
        }
        renzoku = 1;
        now = str[i];
      } else
        renzoku++;
    }
    printf("\n");
  }
}
Problem p01131 (C++)

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

char bt[9][8] = {".,!? ", "abc",  "def", "ghi", "jkl",
                 "mno",   "pqrs", "tuv", "wxyz"};

int main() {
  int N;
  for (scanf("%d", &N); N--;) {
    char mes[128];
    scanf(" %s", mes);
    int len = strlen(mes);
    int prv = 0, count = 0;
    for (int i = 0; i < len; i++) {
      if (mes[i] == '0') {
        if (prv > 0) {
          --prv;
          printf("%c", bt[prv][--count % strlen(bt[prv])]);
        }
        count = 0;
      } else {
        count++;
      }
      prv = mes[i] - '0';
    }
    if (prv > 0) {
      --prv;
      printf("%c", bt[prv][--count % strlen(bt[prv])]);
    }
    puts("");
  }
  return 0;
}

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

char bt[9][8] = {".,!? ", "abc",  "def", "ghi", "jkl",
                 "mno",   "pqrs", "tuv", "wxyz"};

int main() {
  int N;
  for (scanf("%d", &N); N--;) {
    char mes[1280];
    scanf(" %s", mes);
    int len = strlen(mes);
    int prv = 0, count = 0;
    for (int i = 0; i < len; i++) {
      if (mes[i] == '0') {
        if (prv > 0) {
          --prv;
          printf("%c", bt[prv][--count % strlen(bt[prv])]);
        }
        count = 0;
      } else {
        count++;
      }
      prv = mes[i] - '0';
    }
    if (prv > 0) {
      --prv;
      printf("%c", bt[prv][--count % strlen(bt[prv])]);
    }
    puts("");
  }
  return 0;
}
Problem p01131 (C++)

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

int main() {
  int n;
  string bottan[] = {
      "",
      ".,!? ", // 1
      "abc",   // 2
      "def",   // 3
      "ghi",   // 4
      "jkl",   // 5
      "mno",   // 6
      "pqrs",  // 7
      "tuv",   // 8
      "wxyz",  // 9
  };
  int cnt[] = {5, 3, 3, 3, 3, 3, 4, 3, 4};

  cin >> n;
  while (n--) {
    string str;
    cin >> str;
    int len = str.size();
    int cnt1 = 0, cnt2 = 0;
    for (int i = 0; i < len; i++) {
      if (str[i] == '0') {
        if (cnt1 != 0) {
          if (cnt2 % cnt[cnt1] == 0) {
            cout << bottan[cnt1][cnt[cnt1] - 1];
          } else {
            cout << bottan[cnt1][cnt2 % cnt[cnt1] - 1];
          }
        }
        cnt1 = 0;
        cnt2 = 0;
      } else {
        if (cnt1 == 0) {
          cnt1 = str[i] - '0';
          cnt2++;
        } else {
          cnt2++;
        }
      }
    }
    cout << endl;
  }
}

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

int main() {
  int n;
  string bottan[] = {
      "",
      ".,!? ", // 1
      "abc",   // 2
      "def",   // 3
      "ghi",   // 4
      "jkl",   // 5
      "mno",   // 6
      "pqrs",  // 7
      "tuv",   // 8
      "wxyz",  // 9
  };
  int cnt[] = {0, 5, 3, 3, 3, 3, 3, 4, 3, 4};

  cin >> n;
  while (n--) {
    string str;
    cin >> str;
    int len = str.size();
    int cnt1 = 0, cnt2 = 0;
    for (int i = 0; i < len; i++) {
      if (str[i] == '0') {
        if (cnt1 != 0) {
          if (cnt2 % cnt[cnt1] == 0) {
            cout << bottan[cnt1][cnt[cnt1] - 1];
          } else {
            cout << bottan[cnt1][cnt2 % cnt[cnt1] - 1];
          }
        }
        cnt1 = 0;
        cnt2 = 0;
      } else {
        if (cnt1 == 0) {
          cnt1 = str[i] - '0';
          cnt2++;
        } else {
          cnt2++;
        }
      }
    }
    cout << endl;
  }
}
Problem p01131 (C++)

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

const string button[] = {
    "", ".,!? ", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz",
};

void solve() {
  string line;
  cin >> line;
  char pre;
  int count = -1;
  for (unsigned i = 0; i < line.size(); ++i) {
    if (line[i] == '0') {
      int key = pre - '0';
      if (key == 0)
        continue;
      int index = count % button[key].size();
      cout << button[key][index];
      count = -2;
    }
    count++;
    pre = line[i];
  }
  cout << endl;
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    solve();
  }
  return 0;
}

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

const string button[] = {
    "", ".,!? ", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz",
};

void solve() {
  string line;
  cin >> line;
  char pre = '0';
  int count = -1;
  for (unsigned i = 0; i < line.size(); ++i) {
    if (line[i] == '0') {
      int key = pre - '0';
      if (key == 0)
        continue;
      int index = count % button[key].size();
      cout << button[key][index];
      count = -2;
    }
    count++;
    pre = line[i];
  }
  cout << endl;
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    solve();
  }
  return 0;
}
Problem p01131 (C++)

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

using namespace std;

#define all(i) i.begin(), i.end()
#define rep(i, n) for (int i = 0; i < n; i++)

int main() {
  int num = 0;
  cin >> num;
  string dat[12] = {"",    ".,!? ", "abc",  "def", "ghi",
                    "jkl", "mno",   "pqrs", "tuv", "wxyz"};
  rep(i, num) {
    string str;
    cin >> str;
    vector<int> hit(10, 0);
    rep(j, (int)str.size()) {
      hit[str[j] - '0'] += 1;
      if (j == 0 || str[j - 1] == '0') {
      } else if (str[j] == '0') {
        int tmp = str[j - 1] - '0', n = hit[tmp];
        while (n <= (int)dat[tmp].size()) {
          n -= (int)dat[tmp].size();
        }
        printf("%c", dat[tmp][n - 1]);
        rep(i, 10) { hit[i] = 0; }
      }
    }
    printf("\n");
  }
  return 0;
}

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

using namespace std;

#define all(i) i.begin(), i.end()
#define rep(i, n) for (int i = 0; i < n; i++)

int main() {
  int num = 0;
  cin >> num;
  string dat[12] = {"",    ".,!? ", "abc",  "def", "ghi",
                    "jkl", "mno",   "pqrs", "tuv", "wxyz"};
  rep(i, num) {
    string str;
    cin >> str;
    vector<int> hit(10, 0);
    rep(j, (int)str.size()) {
      hit[str[j] - '0'] += 1;
      if (j == 0 || str[j - 1] == '0') {
      } else if (str[j] == '0') {
        int tmp = str[j - 1] - '0', n = hit[tmp];
        while (1) {
          if (n <= (int)dat[tmp].size()) {
            break;
          }
          n -= (int)dat[tmp].size();
        }
        printf("%c", dat[tmp][n - 1]);
        rep(i, 10) { hit[i] = 0; }
      }
    }
    printf("\n");
  }
  return 0;
}
Problem p01131 (C++)

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

using namespace std;

int main() {
  char *dic[] = {"",    ".,!? ", "abc",  "def", "ghi",
                 "jkl", "mno",   "pqrs", "tuv", "wxyz"};
  int len[] = {-1, 5, 3, 3, 3, 3, 3, 4, 3, 4};
  int n;
  scanf("%d", &n);
  while (n--) {
    char s[80];
    scanf("%s", s);
    char bef = '@';
    int cnt = 0;
    for (int i = 0; s[i]; i++) {
      if (s[i] == '0') {
        if (cnt > 0)
          putchar(dic[bef - '0'][(cnt - 1) % len[bef - '0']]);
        cnt = 0;
        bef = '@';
      } else {
        cnt++;
        bef = s[i];
      }
    }
    putchar('\n');
  }
  return 0;
}

Fixed submission:
#include <cstdio>

using namespace std;

int main() {
  char *dic[] = {"",    ".,!? ", "abc",  "def", "ghi",
                 "jkl", "mno",   "pqrs", "tuv", "wxyz"};
  int len[] = {-1, 5, 3, 3, 3, 3, 3, 4, 3, 4};
  int n;
  scanf("%d", &n);
  while (n--) {
    char s[1025];
    scanf("%s", s);
    char bef = '@';
    int cnt = 0;
    for (int i = 0; s[i]; i++) {
      if (s[i] == '0') {
        if (cnt > 0)
          putchar(dic[bef - '0'][(cnt - 1) % len[bef - '0']]);
        cnt = 0;
        bef = '@';
      } else {
        cnt++;
        bef = s[i];
      }
    }
    putchar('\n');
  }
  return 0;
}
Problem p01131 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
struct Data {
  char c;
  int n;
};
int main(void) {

  int n;
  cin >> n;
  string str;
  string c[] = {".,!? ", "abc",  "def", "ghi", "jkl",
                "mno",   "pqrs", "tuv", "wxyz"};
  while (n--) {
    cin >> str;
    Data a[100];
    char ch = 'A';
    int t = -1;
    for (int i = 0; i < str.size(); i++) {
      if (ch == str[i]) {
        a[t].n++;
      } else {
        ch = str[i];
        t++;
        a[t].c = ch;
        a[t].n = 1;
      }
    }
    t++;
    for (int i = 0; i < t; i++) {
      if (a[i].c == '0')
        continue;
      cout << c[a[i].c - '0' - 1][(a[i].n - 1) % c[a[i].c - '0' - 1].size()];
    }
    cout << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
struct Data {
  char c;
  int n;
};
int main(void) {

  int n;
  cin >> n;
  string str;
  string c[] = {".,!? ", "abc",  "def", "ghi", "jkl",
                "mno",   "pqrs", "tuv", "wxyz"};
  while (n--) {
    cin >> str;
    Data a[1200];
    char ch = 'A';
    int t = -1;
    for (int i = 0; i < str.size(); i++) {
      if (ch == str[i]) {
        a[t].n++;
      } else {
        ch = str[i];
        t++;
        a[t].c = ch;
        a[t].n = 1;
      }
    }
    t++;
    for (int i = 0; i < t; i++) {
      if (a[i].c == '0')
        continue;
      cout << c[a[i].c - '0' - 1][(a[i].n - 1) % c[a[i].c - '0' - 1].size()];
    }
    cout << endl;
  }

  return 0;
}
Problem p01132 (C++)

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

using namespace std;

int main() {
  int n[1000], c10[1000], c50[1000], c100[1000], c500[1000], i = 0;

  while (1) {
    cin >> n[i];
    if (n[i] == 0)
      break;
    cin >> c10[i] >> c50[i] >> c100[i] >> c500[i];
    i++;
  }

  for (int j = 0; j < i; j++) {
    int m, k10, k50, k100, k500;
    m = c10[j] * 10 + c50[j] * 50 + c100[j] * 100 + c500[j] * 500 - n[j];

    k500 = c500[j] - m / 500;
    if (k500 < 0)
      k500 = 0;
    m -= 500 * (m / 500);

    k100 = c100[j] - m / 100;
    if (k100 < 0)
      k100 = 0;
    m -= 100 * (m / 100);

    k50 = c50[j] - m / 50;
    if (k50 < 0)
      k50 = 0;
    m -= 50 * (m / 50);

    k10 = c10[j] - m / 10;
    if (k10 < 0)
      k10 = 0;
    m -= 10 * (m / 10);

    if (k10 > 0)
      cout << 10 << " " << k10 << endl;
    if (k50 > 0)
      cout << 50 << " " << k50 << endl;
    if (k100 > 0)
      cout << 100 << " " << k100 << endl;
    if (k500 > 0)
      cout << 500 << " " << k500 << endl;
    if (j < i - 1)
      cout << endl;
  }

  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;

int main() {
  int n[10000], c10[10000], c50[10000], c100[10000], c500[10000], i = 0;

  while (1) {
    cin >> n[i];
    if (n[i] == 0)
      break;
    cin >> c10[i] >> c50[i] >> c100[i] >> c500[i];
    i++;
  }

  for (int j = 0; j < i; j++) {
    int m, k10, k50, k100, k500;
    m = c10[j] * 10 + c50[j] * 50 + c100[j] * 100 + c500[j] * 500 - n[j];

    k500 = c500[j] - m / 500;
    if (k500 < 0)
      k500 = 0;
    m -= 500 * (m / 500);

    k100 = c100[j] - m / 100;
    if (k100 < 0)
      k100 = 0;
    m -= 100 * (m / 100);

    k50 = c50[j] - m / 50;
    if (k50 < 0)
      k50 = 0;
    m -= 50 * (m / 50);

    k10 = c10[j] - m / 10;
    if (k10 < 0)
      k10 = 0;
    m -= 10 * (m / 10);

    if (k10 > 0)
      cout << 10 << " " << k10 << endl;
    if (k50 > 0)
      cout << 50 << " " << k50 << endl;
    if (k100 > 0)
      cout << 100 << " " << k100 << endl;
    if (k500 > 0)
      cout << 500 << " " << k500 << endl;
    if (j < i - 1)
      cout << endl;
  }

  return 0;
}
Problem p01133 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <math.h>
#include <stdio.h>
#define MAX_NUM 100000000

double dist(int x, int y) { return sqrt((double)(x * x + y * y)); }

double min(double a, double b) {
  if (a > b)
    return b;
  else
    return a;
}

int n, cx[20], cy[20], dx, dy;

double dfs(int now, double sum, int bit) {
  int i;
  double ret = MAX_NUM;
  if (sum >= dist(dx - cx[now], dy - cy[now]))
    return ret;
  if (bit == (1 << n) - 1)
    return sum;
  for (i = 0; i < n; i++) {
    if (~bit & (1 << i)) {
      ret = min(
          dfs(i, sum + dist(cx[i] - cx[now], cy[i] - cy[now]), bit | (1 << i)),
          ret);
    }
  }
  return ret;
}

int main(void) {
  int hx, hy, i;
  double ans;
  while (1) {
    scanf("%d%d%d%d%d", &n, &hx, &hy, &dx, &dy);
    if (n == 0 && hx == 0 && hy == 0 && dx == 0 && dy == 0)
      break;
    for (i = 0; i < n; i++)
      scanf("%d%d", &cx[i], &cy[i]);
    ans = MAX_NUM;
    for (i = 0; i < n; i++)
      ans = min(ans, dfs(i, dist(cx[i] - hx, cy[i] - hy), 1 << i));
    if (ans != MAX_NUM)
      printf("YES\n");
    else
      printf("NO\n");
  }
  return 0;
}

Fixed submission:
#include <math.h>
#include <stdio.h>
#define MAX_NUM 100000000

double dist(int x, int y) { return sqrt((double)(x * x + y * y)); }

double min(double a, double b) {
  if (a > b)
    return b;
  else
    return a;
}

int n, cx[20], cy[20], dx, dy;

double dfs(int now, double sum, int bit) {
  int i;
  double ret = MAX_NUM;
  for (i = 0; i < n; i++)
    if (~bit & (1 << i) && sum + dist(cx[now] - cx[i], cy[now] - cy[i]) >=
                               dist(dx - cx[i], dy - cy[i]))
      return ret;
  if (sum >= dist(dx - cx[now], dy - cy[now]))
    return ret;
  if (bit == (1 << n) - 1)
    return sum;
  for (i = 0; i < n; i++) {
    if (~bit & (1 << i)) {
      ret = min(
          dfs(i, sum + dist(cx[i] - cx[now], cy[i] - cy[now]), bit | (1 << i)),
          ret);
    }
  }
  return ret;
}

int main(void) {
  int hx, hy, i;
  double ans;
  while (1) {
    scanf("%d%d%d%d%d", &n, &hx, &hy, &dx, &dy);
    if (n == 0 && hx == 0 && hy == 0 && dx == 0 && dy == 0)
      break;
    for (i = 0; i < n; i++)
      scanf("%d%d", &cx[i], &cy[i]);
    ans = MAX_NUM;
    for (i = 0; i < n; i++)
      ans = min(ans, dfs(i, dist(cx[i] - hx, cy[i] - hy), 1 << i));
    if (ans != MAX_NUM)
      printf("YES\n");
    else
      printf("NO\n");
  }
  return 0;
}
Problem p01133 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <iostream>
#include <map>
// S
using namespace std;
typedef pair<int, int> P;
int cx[30], cy[30], n, hx, hy, dx, dy;
map<P, double> mp;
bool func(int S, int now, double time) {
  if (S == (1 << n) - 1)
    return 1;
  for (int i = 0; i < n; i++) {
    if (!(S & (1 << i))) {
      double pre = mp[P(now, i)];
      double dis = mp[P(i, n + 1)];
      if (pre + time < dis && func(S | (1 << i), i, time + pre))
        return 1;
    }
  }
  return 0;
}
int main() {
  while (cin >> n >> hx >> hy >> dx >> dy, n || hx || hy || dx || dy) {
    for (int i = 0; i < n; i++)
      cin >> cx[i] >> cy[i];
    cx[n] = hx;
    cy[n] = hy;
    cx[n + 1] = dx;
    cy[n + 1] = dy;
    for (int i = 0; i <= n + 1; i++)
      for (int j = 0; j <= n + 1; j++)
        mp[P(i, j)] = sqrt((cx[i] - cx[j]) * (cx[i] - cx[j]) +
                           (cy[i] - cy[j]) * (cy[i] - cy[j]));
    if (func(0, n, 0.0))
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
    mp.clear();
  }
  return 0;
}

Fixed submission:
#include <cmath>
#include <iostream>
#include <map>
// S
using namespace std;
typedef pair<int, int> P;
int cx[30], cy[30], n, hx, hy, dx, dy;
map<P, double> mp;
bool func(int S, int now, double time) {
  if (S == (1 << n) - 1)
    return 1;
  for (int i = 0; i < n; i++) {
    if (!(S & (1 << i))) {
      if (mp[P(now, i)] + time >= mp[P(i, n + 1)])
        return 0;
    }
  }
  for (int i = 0; i < n; i++) {
    if (!(S & (1 << i))) {
      double pre = mp[P(now, i)];
      double dis = mp[P(i, n + 1)];
      if (pre + time < dis && func(S | (1 << i), i, time + pre))
        return 1;
    }
  }
  return 0;
}
int main() {
  while (cin >> n >> hx >> hy >> dx >> dy, n || hx || hy || dx || dy) {
    for (int i = 0; i < n; i++)
      cin >> cx[i] >> cy[i];
    cx[n] = hx;
    cy[n] = hy;
    cx[n + 1] = dx;
    cy[n + 1] = dy;
    for (int i = 0; i <= n + 1; i++)
      for (int j = 0; j <= n + 1; j++)
        mp[P(i, j)] = sqrt((cx[i] - cx[j]) * (cx[i] - cx[j]) +
                           (cy[i] - cy[j]) * (cy[i] - cy[j]));
    if (func(0, n, 0.0))
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
    mp.clear();
  }
  return 0;
}
Problem p01133 (C++)

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

#define EPS 1e-10

struct Point {
  double x, y, d;
};
int n;
Point hero, demon;
vector<Point> crystal;
bool ok;

double dist(Point a, Point b) {
  return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
void solve(int now, double time, vector<bool> visited) {
  int i;
  bool f = true;
  if (time - crystal[now].d > -EPS)
    return;
  for (i = 0; i < n; i++) {
    if (visited[i])
      continue;
    visited[i] = true;
    solve(i, time + dist(crystal[now], crystal[i]), visited);
    visited[i] = false;
    f = false;
  }
  if (f)
    ok = true;
  return;
}

int main() {
  vector<bool> visited;
  int i;
  double hx, hy, dx, dy, x, y;
  double time;
  while (cin >> n >> hx >> hy >> dx >> dy, hx || hy || dx || dy) {
    hero.x = hx;
    hero.y = hy;
    demon.x = dx;
    demon.y = dy;
    crystal.clear();
    visited.resize(n);
    fill(visited.begin(), visited.end(), false);
    for (i = 0; i < n; i++) {
      cin >> x >> y;
      Point p = {x, y, 0};
      p.d = dist(p, demon);
      crystal.push_back(p);
    }
    ok = false;
    for (i = 0; i < n; i++) {
      time = dist(hero, crystal[i]);
      visited[i] = true;
      solve(i, time, visited);
      visited[i] = false;
      if (ok)
        break;
    }
    puts(ok ? "YES" : "NO");
  }
}

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

#define EPS 1e-10

struct Point {
  double x, y, d;
};
int n;
Point hero, demon;
vector<Point> crystal;
bool ok;

double dist(Point a, Point b) {
  return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
void solve(int now, double time, vector<bool> visited) {
  int i;
  bool f = true;
  if (time - crystal[now].d > -EPS)
    return;
  for (i = 0; i < n; i++) {
    if (visited[i])
      continue;
    if (time + dist(crystal[now], crystal[i]) - crystal[i].d > EPS)
      return;
  }
  for (i = 0; i < n; i++) {
    if (visited[i])
      continue;
    visited[i] = true;
    solve(i, time + dist(crystal[now], crystal[i]), visited);
    visited[i] = false;
    f = false;
  }
  if (f)
    ok = true;
  return;
}

int main() {
  vector<bool> visited;
  int i;
  double hx, hy, dx, dy, x, y;
  double time;
  while (cin >> n >> hx >> hy >> dx >> dy, hx || hy || dx || dy) {
    hero.x = hx;
    hero.y = hy;
    demon.x = dx;
    demon.y = dy;
    crystal.clear();
    visited.resize(n);
    fill(visited.begin(), visited.end(), false);
    for (i = 0; i < n; i++) {
      cin >> x >> y;
      Point p = {x, y, 0};
      p.d = dist(p, demon);
      crystal.push_back(p);
    }
    ok = false;
    for (i = 0; i < n; i++) {
      time = dist(hero, crystal[i]);
      visited[i] = true;
      solve(i, time, visited);
      visited[i] = false;
      if (ok)
        break;
    }
    puts(ok ? "YES" : "NO");
  }
}
Problem p01133 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
typedef long long LL;
typedef pair<double, int> P;

int xs[21];
int ys[21];
double h[21];
int n;
double Hypot(int x, int y) { return sqrt(x * x + y * y); }
bool ok(int x, int y, int done, double t) {
  if (done + 1 == (1 << n)) {
    return true;
  }

  vector<P> q;
  for (int i = 0; i < n; i++) {
    if (done & (1 << i))
      continue;
    double d = t + Hypot(xs[i] - x, ys[i] - y);
    if (d >= h[i]) {
      return false;
    }
    q.push_back(P(d, i));
  }
  sort(q.begin(), q.end());
  for (auto p : q) {
    int i = p.second;
    if (ok(xs[i], ys[i], done | (1 << i), p.first)) {
      return true;
    }
  }
  return false;
}
void exec() {
  int hx, hy, dx, dy;
  cin >> n >> hx >> hy >> dx >> dy;
  if ((n | hx | hy | dx | dy) == 0) {
    exit(1);
  }
  for (int i = 0; i < n; i++) {
    cin >> xs[i] >> ys[i];
    h[i] = Hypot(xs[i] - dx, ys[i] - dy);
  }
  if (ok(hx, hy, 0, 0)) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }
}
int main() {
  while (1)
    exec();
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
typedef long long LL;
typedef pair<double, int> P;

int xs[21];
int ys[21];
double h[21];
int n;
double Hypot(int x, int y) { return sqrt(x * x + y * y); }
bool ok(int x, int y, int done, double t) {
  if (done + 1 == (1 << n)) {
    return true;
  }

  vector<P> q;
  for (int i = 0; i < n; i++) {
    if (done & (1 << i))
      continue;
    double d = t + Hypot(xs[i] - x, ys[i] - y);
    if (d >= h[i]) {
      return false;
    }
    q.push_back(P(d, i));
  }
  sort(q.begin(), q.end());
  for (auto p : q) {
    int i = p.second;
    if (ok(xs[i], ys[i], done | (1 << i), p.first)) {
      return true;
    }
  }
  return false;
}
void exec() {
  int hx, hy, dx, dy;
  cin >> n >> hx >> hy >> dx >> dy;
  if ((n | hx | hy | dx | dy) == 0) {
    exit(0);
  }
  for (int i = 0; i < n; i++) {
    cin >> xs[i] >> ys[i];
    h[i] = Hypot(xs[i] - dx, ys[i] - dy);
  }
  if (ok(hx, hy, 0, 0)) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }
}
int main() {
  while (1)
    exec();
}
Problem p01133 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <math.h>
#include <stdio.h>
using namespace std;
double eps = 1e-9;
int x[23];
int y[23];
double dist[23][23];
int n;
int solve(int a, int b, double s) {
  if (a == (1 << n) - 1)
    return 1;
  for (int i = 0; i < n; i++) {
    if (a & (1 << i))
      continue;
    if (s + dist[b][i] + eps > dist[n + 1][i])
      return 0;
    if (solve(a + (1 << i), i, s + dist[b][i]))
      return 1;
  }
  return 0;
}
int main() {
  int a, hx, hy, dx, dy;
  while (scanf("%d%d%d%d%d", &a, &hx, &hy, &dx, &dy), a) {
    n = a;
    for (int i = 0; i < a; i++)
      scanf("%d%d", x + i, y + i);
    x[a] = hx;
    x[a + 1] = dx;
    y[a] = hy;
    y[a + 1] = dy;
    for (int i = 0; i < a + 2; i++)
      for (int j = 0; j < a + 2; j++)
        dist[i][j] =
            sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]));
    if (solve(0, a, 0))
      printf("YES\n");
    else
      printf("NO\n");
  }
}

Fixed submission:
#include <algorithm>
#include <math.h>
#include <stdio.h>
using namespace std;
double eps = 1e-9;
int x[23];
int y[23];
double dist[23][23];
int n;
int solve(int a, int b, double s) {
  if (a == (1 << n) - 1)
    return 1;
  for (int i = 0; i < n; i++) {
    if (a & (1 << i))
      continue;
    if (s + dist[b][i] + eps > dist[n + 1][i])
      return 0;
  }
  for (int i = 0; i < n; i++) {
    if (a & (1 << i))
      continue;

    if (solve(a + (1 << i), i, s + dist[b][i]))
      return 1;
  }
  return 0;
}
int main() {
  int a, hx, hy, dx, dy;
  while (scanf("%d%d%d%d%d", &a, &hx, &hy, &dx, &dy), a) {
    n = a;
    for (int i = 0; i < a; i++)
      scanf("%d%d", x + i, y + i);
    x[a] = hx;
    x[a + 1] = dx;
    y[a] = hy;
    y[a + 1] = dy;
    for (int i = 0; i < a + 2; i++)
      for (int j = 0; j < a + 2; j++)
        dist[i][j] =
            sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]));
    if (solve(0, a, 0))
      printf("YES\n");
    else
      printf("NO\n");
  }
}
Problem p01133 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#define INF (1 << 30)
#define EPS (double)(1e-5)
using namespace std;
int n, hx, hy, dx, dy;
int cx[22], cy[22];

double L(int x, int y) { return sqrt(x * x + y * y); }

bool solve(int m, int bit, double l) {
  if (bit == (1 << n) - 1)
    return true;
  for (int i = 1; i < n; i++) {
    if (bit & (1 << i))
      continue;
    if (L(cx[m] - cx[i], cy[m] - cy[i]) + l + EPS > L(cx[i] - dx, cy[i] - dy))
      return false;
    if (solve(i, bit | (1 << i), l + L(cx[m] - cx[i], cy[m] - cy[i])))
      return true;
  }
  return false;
}

int main(void) {
  while (cin >> n >> cx[0] >> cy[0] >> dx >> dy && n) {
    n++;
    for (int i = 1; i < n; i++) {
      cin >> cx[i] >> cy[i];
    }
    if (solve(0, 1, 0))
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#define INF (1 << 30)
#define EPS (double)(1e-5)
using namespace std;
int n, hx, hy, dx, dy;
int cx[22], cy[22];

double L(int x, int y) { return sqrt(x * x + y * y); }

bool solve(int m, int bit, double l) {
  if (bit == (1 << n) - 1)
    return true;
  for (int i = 1; i < n; i++) {
    if (bit & (1 << i))
      continue;
    if (L(cx[m] - cx[i], cy[m] - cy[i]) + l + EPS > L(cx[i] - dx, cy[i] - dy))
      return false;
  }
  for (int i = 1; i < n; i++) {
    if (bit & (1 << i))
      continue;
    if (solve(i, bit | (1 << i), l + L(cx[m] - cx[i], cy[m] - cy[i])))
      return true;
  }
  return false;
}

int main(void) {
  while (cin >> n >> cx[0] >> cy[0] >> dx >> dy && n) {
    n++;
    for (int i = 1; i < n; i++) {
      cin >> cx[i] >> cy[i];
    }
    if (solve(0, 1, 0))
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }
}
Problem p01136 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

vector<int> v[31];
ll dp[31][50];
int main() {
  int n;
  while (scanf("%d", &n), n) {
    rep(i, n) {
      int f;
      scanf("%d", &f);
      rep(j, f) {
        int k;
        scanf("%d", &k);
        v[k].push_back(i);
      }
    }
    rep(i, n) dp[0][i] = 1LL << i;
    for (int i = 1; i <= 30; i++) {
      rep(j, n) dp[i][j] = dp[i - 1][j];
      rep(t, n) for (int j : v[i]) for (int k : v[i]) {
        dp[i][j] |= dp[i][k];
        dp[i][k] |= dp[i][j];
      }
    }
    rep(i, 31) rep(j, n) {
      if (dp[i][j] == (1LL << n) - 1) {
        printf("%d\n", i);
        goto g;
      }
    }
    puts("-1");
  g:;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

vector<int> v[31];
ll dp[31][50];
int main() {
  int n;
  while (scanf("%d", &n), n) {
    rep(i, 31) v[i].clear();
    rep(i, n) {
      int f;
      scanf("%d", &f);
      rep(j, f) {
        int k;
        scanf("%d", &k);
        v[k].push_back(i);
      }
    }
    rep(i, n) dp[0][i] = 1LL << i;
    for (int i = 1; i <= 30; i++) {
      rep(j, n) dp[i][j] = dp[i - 1][j];
      rep(t, n) for (int j : v[i]) for (int k : v[i]) {
        dp[i][j] |= dp[i][k];
        dp[i][k] |= dp[i][j];
      }
    }
    rep(i, 31) rep(j, n) {
      if (dp[i][j] == (1LL << n) - 1) {
        printf("%d\n", i);
        goto g;
      }
    }
    puts("-1");
  g:;
  }
}
Problem p01136 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n;
  while (cin >> n, n) {
    bool f[50][50] = {false};
    vector<int> day[30];
    for (int i = 0; i < n; i++)
      f[i][i] = true;
    for (int i = 0, a; i < n; i++) {
      cin >> a;
      for (int j = 0, b; j < a; j++) {
        cin >> b;
        day[b - 1].push_back(i);
      }
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < day[i].size(); j++)
        for (int k = 0; k < day[i].size(); k++)
          for (int l = 0; l < n; l++)
            f[day[i][j]][l] = f[day[i][k]][l] =
                f[day[i][j]][l] | f[day[i][k]][l];
      for (int j = 0; j < n; j++) {
        bool flag = true;
        for (int k = 0; k < n; k++)
          flag &= f[j][k];
        if (flag) {
          cout << i + 1 << endl;
          goto jump;
        }
      }
    }
    cout << "-1" << endl;
  jump:;
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n;
  while (cin >> n, n) {
    bool f[50][50] = {false};
    vector<int> day[30];
    for (int i = 0; i < n; i++)
      f[i][i] = true;
    for (int i = 0, a; i < n; i++) {
      cin >> a;
      for (int j = 0, b; j < a; j++) {
        cin >> b;
        day[b - 1].push_back(i);
      }
    }
    for (int i = 0; i < 30; i++) {
      for (int j = 0; j < day[i].size(); j++)
        for (int k = 0; k < day[i].size(); k++)
          for (int l = 0; l < n; l++)
            f[day[i][j]][l] = f[day[i][k]][l] =
                f[day[i][j]][l] | f[day[i][k]][l];
      for (int j = 0; j < n; j++) {
        bool flag = true;
        for (int k = 0; k < n; k++)
          flag &= f[j][k];
        if (flag) {
          cout << i + 1 << endl;
          goto jump;
        }
      }
    }
    cout << "-1" << endl;
  jump:;
  }
  return 0;
}
Problem p01136 (C++)

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

int main() {
  int a;
  while (cin >> a, a) {
    vector<int> b[30];
    for (int c = 0; c < a; c++) {
      int d;
      cin >> d;
      for (int e = 0; e < d; e++) {
        int f;
        cin >> f;
        f--;
        b[f].push_back(c);
      }
    }
    for (int c = 0; c < 30; c++) {
      bool e[30]{};
      if (b[c].size())
        e[b[c][0]] = true;
      for (int d = c; d >= 0; d--) {
        bool f = false;
        for (int g = 0; g < b[d].size(); g++) {
          if (e[b[d][g]])
            f = true;
        }
        if (f) {
          for (int g = 0; g < b[d].size(); g++) {
            e[b[d][g]] = true;
          }
        }
      }
      bool k = true;
      for (int i = 0; i < a; i++) {
        if (!e[i])
          k = false;
      }
      if (k) {
        cout << c + 1 << endl;
        goto l;
      }
    }
    puts("-1");
  l:;
  }
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  int a;
  while (cin >> a, a) {
    vector<int> b[30];
    for (int c = 0; c < a; c++) {
      int d;
      cin >> d;
      for (int e = 0; e < d; e++) {
        int f;
        cin >> f;
        f--;
        b[f].push_back(c);
      }
    }
    for (int c = 0; c < 30; c++) {
      bool e[50]{};
      if (b[c].size())
        e[b[c][0]] = true;
      for (int d = c; d >= 0; d--) {
        bool f = false;
        for (int g = 0; g < b[d].size(); g++) {
          if (e[b[d][g]])
            f = true;
        }
        if (f) {
          for (int g = 0; g < b[d].size(); g++) {
            e[b[d][g]] = true;
          }
        }
      }
      bool k = true;
      for (int i = 0; i < a; i++) {
        if (!e[i])
          k = false;
      }
      if (k) {
        cout << c + 1 << endl;
        goto l;
      }
    }
    puts("-1");
  l:;
  }
}
Problem p01136 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
int N;
int main() {
  for (; scanf("%d", &N), N;) {
    set<int> dp[35];
    vector<int> D[35];
    for (int i = 0; i < N; i++) {
      int t, d;
      dp[i].insert(i);
      scanf("%d", &t);
      for (int j = 0; j < t; j++) {
        scanf("%d", &d);
        D[d].push_back(i);
      }
    }
    for (int i = 0; i < 31; i++) {
      set<int> a;
      for (int j = 0; j < D[i].size(); j++) {
        for (set<int>::iterator it = dp[D[i][j]].begin();
             it != dp[D[i][j]].end(); it++)
          a.insert(*it);
      }
      if (a.size() == N) {
        printf("%d\n", i);
        goto e;
      }
      for (int j = 0; j < D[i].size(); j++)
        dp[D[i][j]] = a;
    }
    puts("-1");
  e:;
  }
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
int N;
int main() {
  for (; scanf("%d", &N), N;) {
    set<int> dp[99];
    vector<int> D[99];
    for (int i = 0; i < N; i++) {
      int t, d;
      dp[i].insert(i);
      scanf("%d", &t);
      for (int j = 0; j < t; j++) {
        scanf("%d", &d);
        D[d].push_back(i);
      }
    }
    for (int i = 0; i < 31; i++) {
      set<int> a;
      for (int j = 0; j < D[i].size(); j++) {
        for (set<int>::iterator it = dp[D[i][j]].begin();
             it != dp[D[i][j]].end(); it++)
          a.insert(*it);
      }
      if (a.size() == N) {
        printf("%d\n", i);
        goto e;
      }
      for (int j = 0; j < D[i].size(); j++)
        dp[D[i][j]] = a;
    }
    puts("-1");
  e:;
  }
}
Problem p01136 (C++)

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

typedef long long STATES;

int main(void) {
  STATES free_days[31], dp[51], goal_state, next;
  STATES *p;
  short int i, j, n, answer, free_day, k;

  while (scanf("%d", &n) == 1 && n) {
    memset(free_days, 0, sizeof(free_days));
    memset(dp, 0, sizeof(dp));

    goal_state = 0;
    for (i = 0; i < n; i++)
      goal_state |= 1LL << i;

    for (i = 0; i < n; i++) {
      scanf("%d", &k);
      for (j = 0; j < k; j++) {
        scanf("%d", &free_day);
        free_days[free_day] |= 1LL << i;
      }
    }

    answer = -1;
    for (i = 0; i < n; i++)
      dp[i] = 1LL << i;
    p = free_days + 1;
    for (i = 1; i <= 30; i++, p++) {
      next = 0;
      for (j = 0; j < n; j++)
        if (*p & 1LL << j)
          next |= dp[j];
      if (next == goal_state) {
        answer = i;
        break;
      }
      for (j = 0; j < n; j++)
        if (*p & 1LL << j)
          dp[j] |= next;
    }

    printf("%d\n", answer);
  }

  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstring>
using namespace std;

typedef long long STATES;

int main(void) {
  STATES free_days[31], dp[51], goal_state, next;
  STATES *p;
  int i, j, n, free_day, k;
  short answer;

  while (scanf("%d", &n) == 1 && n) {
    memset(free_days, 0, sizeof(free_days));
    memset(dp, 0, sizeof(dp));

    goal_state = 0;
    for (i = 0; i < n; i++)
      goal_state |= 1LL << i;

    for (i = 0; i < n; i++) {
      scanf("%d", &k);
      for (j = 0; j < k; j++) {
        scanf("%d", &free_day);
        free_days[free_day] |= 1LL << i;
      }
    }

    answer = -1;
    for (i = 0; i < n; i++)
      dp[i] = 1LL << i;
    p = free_days + 1;
    for (i = 1; i <= 30; i++, p++) {
      next = 0;
      for (j = 0; j < n; j++)
        if (*p & 1LL << j)
          next |= dp[j];
      if (next == goal_state) {
        answer = i;
        break;
      }
      for (j = 0; j < n; j++)
        if (*p & 1LL << j)
          dp[j] |= next;
    }

    printf("%d\n", answer);
  }

  return 0;
}
Problem p01137 (C++)

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

int main() {
  while (1) {
    int e;
    cin >> e;
    if (e == 0)
      break;
    int ans = e + 1;
    for (int x = 0; x * x <= e; x++)
      for (int y = 0; y * y <= e; y++)
        for (int z = 0; z * z * z <= e; z++)
          if (x + y * y + z * z * z == e)
            ans = min(ans, x + y + z);
    cout << ans << endl;
  }
}

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

int main() {
  while (1) {
    int e;
    cin >> e;
    if (e == 0)
      break;
    int ans = e + 1;
    for (int y = 0; y * y <= e; y++)
      for (int z = 0; z * z * z <= e; z++)
        if (y * y + z * z * z <= e)
          ans = min(ans, e - y * y - z * z * z + y + z);
    cout << ans << endl;
  }
}
Problem p01137 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using msi = map<string, int>;
using mii = map<int, int>;
using psi = pair<string, int>;
using pii = pair<int, int>;
using vlai = valarray<int>;
#define rep(i, n) for (int i = 0; i < n; i++)
#define range(i, s, n) for (int i = s; i < n; i++)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define fs first
#define sc second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define INF 1E9
#define EPS 1E-9
#define MOD (ll)(1E9 + 7)
#define PI 3.1415926535897932384

template <class T> ostream &operator<<(std::ostream &o, const vector<T> &v) {
  rep(i, v.size()) { o << (i > 0 ? " " : "") << v[i]; }
  return o;
}

int main() {
  int e, n = 1000000;
  while (1) {
    cin >> e;
    int mi = INF;
    if (e == 0)
      break;
    for (int i = 0; i <= e; i++) {
      for (int j = 0; j * j <= (e - i * i * i); j++) {
        if (j * j + i * i * i > e)
          break;
        int x = e - (j * j + i * i * i);
        if (mi > (i + j + x)) {
          mi = i + j + x;
        }
      }
    }
    cout << mi << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using msi = map<string, int>;
using mii = map<int, int>;
using psi = pair<string, int>;
using pii = pair<int, int>;
using vlai = valarray<int>;
#define rep(i, n) for (int i = 0; i < n; i++)
#define range(i, s, n) for (int i = s; i < n; i++)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define fs first
#define sc second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define INF 1E9
#define EPS 1E-9
#define MOD (ll)(1E9 + 7)
#define PI 3.1415926535897932384

template <class T> ostream &operator<<(std::ostream &o, const vector<T> &v) {
  rep(i, v.size()) { o << (i > 0 ? " " : "") << v[i]; }
  return o;
}

int main() {
  int e, n = 1000000;
  while (1) {
    cin >> e;
    int mi = INF;
    if (e == 0)
      break;
    for (int i = 0; i * i * i <= e; i++) {
      for (int j = 0; j * j <= (e - i * i * i); j++) {
        if (j * j + i * i * i > e)
          break;
        int x = e - (j * j + i * i * i);
        if (mi > (i + j + x)) {
          mi = i + j + x;
        }
      }
    }
    cout << mi << endl;
  }
}
Problem p01137 (C++)

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

using namespace std;

int main(int argc, char *argv[]) {
  int e;
  while (cin >> e, e) {
    int m = e;
    for (int z = 0; pow(z, 3) <= e; ++z) {
      for (int y = 0; pow(y, 2) <= e - pow(z, 3); ++y) {
        int x = e - pow(y, 2) - pow(z, 3);
        m = min(m, x + y + z);
      }
    }
    cout << m << endl;
  }
  return 0;
}


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

using namespace std;

int main(int argc, char *argv[]) {
  int e;
  while (cin >> e, e) {
    int m = e;
    for (int z = 0; z * z * z <= e; ++z) {
      for (int y = 0; y * y <= e - z * z * z; ++y) {
        int x = e - y * y - z * z * z;
        m = min(m, x + y + z);
      }
    }
    cout << m << endl;
  }
  return 0;
}
Problem p01137 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <math.h>
using namespace std;
int e;
int main() {
  for (;;) {
    cin >> e;
    ;
    int m = e;
    if (e == 0)
      break;
    for (int y = 0; pow(y, 2) <= e; y++) {
      for (int z = 0; pow(y, 2) + pow(z, 3) <= e; z++) {
        int x = e - pow(z, 3) - pow(y, 2);
        if (x + y + z < m) {
          m = x + y + z;
        }
      }
    }
    cout << m << endl;
  }
}

Fixed submission:
#include <iostream>
#include <math.h>
using namespace std;
int e;
int main() {
  for (;;) {
    cin >> e;
    ;
    int m = e;
    if (e == 0)
      break;
    for (int y = 0; y * y <= e; y++) {
      for (int z = 0; y * y + z * z * z <= e; z++) {
        int x = e - z * z * z - y * y;
        if (x + y + z < m) {
          m = x + y + z;
        }
      }
    }
    cout << m << endl;
  }
}
Problem p01137 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <iostream>
using namespace std;
int main() {
  int e;
  while (cin >> e, e) {
    int ret = 1e6;
    for (int z = 0; z <= e; ++z) {
      int y = (int)pow(e - z * z * z, 1.0 / 2.0);
      int x = e - z * z * z - y * y;
      ret = min(ret, x + y + z);
    }
    cout << ret << endl;
  }
  return 0;
}

Fixed submission:
#include <cmath>
#include <iostream>
using namespace std;
int main() {
  int e;
  while (cin >> e, e) {
    int ret = 1e6;
    for (int z = 0; z * z * z <= e; ++z) {
      int y = (int)pow(e - z * z * z, 1.0 / 2.0);
      int x = e - z * z * z - y * y;
      ret = min(ret, x + y + z);
    }
    cout << ret << endl;
  }
  return 0;
}
Problem p01137 (C++)

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

int main() {
  int e;
  while (cin >> e, e) {
    int num;
    int ans = 1e9;
    for (int i = 0; i < e; i++) {
      for (int j = 0; j < e; j++) {
        num = e - i * i * i - j * j;
        if (num < 0)
          break;
        ans = min(ans, num + i + j);
      }
    }
    cout << ans << endl;
  }
}

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

int main() {
  int e;
  while (cin >> e, e) {
    int num;
    int ans = 1e9;
    for (int i = 0; i < e; i++) {
      if (i * i * i > e)
        break;
      for (int j = 0; j < e; j++) {
        num = e - i * i * i - j * j;
        if (num < 0)
          break;
        ans = min(ans, num + i + j);
      }
    }
    cout << ans << endl;
  }
}