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

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

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
const char ch[] = {"ULDR"};

bool g[10][10][10][10];

int main() {
  char c;

  for (int i = 1; i <= 5; i++) {
    for (int j = 1; j <= 4; j++) {
      scanf(" %c", &c);
      if (c == '0')
        continue;
      g[i][j][i][j + 1] = true;
      g[i][j + 1][i][j] = true;
    }
    if (i == 5)
      continue;
    for (int j = 1; j <= 5; j++) {
      scanf(" %c", &c);
      if (c == '0')
        continue;
      g[i][j][i + 1][j] = true;
      g[i + 1][j][i][j] = true;
    }
  }

  int dir = 3;
  int x = 1, y = 2;
  while (true) {
    printf("%c", ch[dir]);
    if (x == 1 && y == 1)
      break;
    for (int t = 1; t >= -1; t--) {
      int nd = (dir + t + 4) % 4;
      int nx = x + dx[nd];
      int ny = y + dy[nd];
      if (g[x][y][nx][ny]) {
        x = nx;
        y = ny;
        dir = nd;
        break;
      }
    }
  }
  puts("");
  return 0;
}

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

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
const char ch[] = {"ULDR"};

bool g[10][10][10][10];

int main() {
  char c;

  for (int i = 1; i <= 5; i++) {
    for (int j = 1; j <= 4; j++) {
      scanf(" %c", &c);
      if (c == '0')
        continue;
      g[i][j][i][j + 1] = true;
      g[i][j + 1][i][j] = true;
    }
    if (i == 5)
      continue;
    for (int j = 1; j <= 5; j++) {
      scanf(" %c", &c);
      if (c == '0')
        continue;
      g[i][j][i + 1][j] = true;
      g[i + 1][j][i][j] = true;
    }
  }

  int dir = 3;
  int x = 1, y = 2;
  while (true) {
    printf("%c", ch[dir]);
    if (x == 1 && y == 1)
      break;
    for (int t = 1; t >= -2; t--) {
      int nd = (dir + t + 4) % 4;
      int nx = x + dx[nd];
      int ny = y + dy[nd];
      if (g[x][y][nx][ny]) {
        x = nx;
        y = ny;
        dir = nd;
        break;
      }
    }
  }
  puts("");
  return 0;
}
Problem p00037 (C++)

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

using namespace std;

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

int maze[20][20];

char s[6];
char ans[100];
int sz = 0;

int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

char sym[] = "RDLU";

int main() {
  REP(i, 9) {
    scanf("%s", s);
    REP(j, strlen(s)) {
      if (i % 2 == 0) {
        maze[i + 1][2 * j + 2] = s[j] - '0';
      } else {
        maze[i + 1][2 * j + 1] = s[j] - '0';
      }
    }
  }
  REP(i, 11) {
    REP(j, 11) { fprintf(stderr, "%d", maze[i][j]); }
    fprintf(stderr, "\n");
  }
  ans[0] = 'R';
  sz++;
  int x = 3, y = 1;
  int dir = 0;
  while (x != 1 || y != 1) {
    REP(i, 4) {
      int d = (dir + 3 + i) % 4;
      if (maze[y + dy[d]][x + dx[d]]) {
        dir = d;
        break;
      }
    }
    x += dx[dir] * 2;
    y += dy[dir] * 2;
    ans[sz++] = sym[dir];
  }
  printf("%s\n", ans);
  return 0;
}

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

using namespace std;

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

int maze[20][20];

char s[6];
char ans[100];
int sz = 0;

int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

char sym[] = "RDLU";

int main() {
  REP(i, 9) {
    scanf("%s", s);
    REP(j, strlen(s)) {
      if (i % 2 == 0) {
        maze[i + 1][2 * j + 2] = s[j] - '0';
      } else {
        maze[i + 1][2 * j + 1] = s[j] - '0';
      }
    }
  }
  ans[0] = 'R';
  sz++;
  int x = 3, y = 1;
  int dir = 0;
  while (x != 1 || y != 1) {
    REP(i, 4) {
      int d = (dir + 3 + i) % 4;
      if (maze[y + dy[d]][x + dx[d]]) {
        dir = d;
        break;
      }
    }
    x += dx[dir] * 2;
    y += dy[dir] * 2;
    ans[sz++] = sym[dir];
  }
  printf("%s\n", ans);
  return 0;
}
Problem p00038 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int main(void) {

  int a[5] = {0};
  string str = "null";
  while (scanf("%d,%d,%d,%d,%d", a[0], a[1], a[2], a[3], a[4]) != EOF) {
    sort(a, a + 5);
    if (((a[0] == a[1]) && (a[2] == a[3]) && (a[2] == a[1])) ||
        ((a[1] == a[2]) && (a[2] == a[3]) && (a[3] == a[4])))
      str = "four card";
    else if (((a[0] == a[1]) && (a[1] == a[2]) && (a[3] == a[4])) ||
             ((a[0] == a[1]) && (a[2] == a[3]) && (a[3] == a[4])))
      str = "full house";
    else if (((a[0] == 1) && (a[1] == 10) && (a[2] == 11) && (a[3] == 12) &&
              (a[4] == 13)) ||
             ((a[0] + 1 == a[1]) && (a[1] + 1 == a[2]) && (a[2] + 1 == a[3]) &&
              (a[3] + 1 == a[4])))
      str = "straight";
    else if (((a[0] == a[1]) && (a[1] == a[2])) ||
             ((a[1] == a[2]) && (a[2] == a[3])) ||
             ((a[2] == a[3]) && (a[3] == a[4])))
      str = "three card";
    else if (((a[0] == a[1]) && ((a[2] == a[3]) || (a[3] == a[4]))) ||
             ((a[1] == a[2]) && (a[3] == a[4])))
      str = "two pair";
    else if ((a[0] == a[1]) || (a[1] == a[2]) || (a[2] == a[3]) ||
             (a[3] == a[4]))
      str = "one pair";

    cout << str << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int main(void) {

  int a[5] = {0};
  string str = "null";
  while (scanf("%d,%d,%d,%d,%d", &a[0], &a[1], &a[2], &a[3], &a[4]) != EOF) {
    sort(a, a + 5);
    if (((a[0] == a[1]) && (a[2] == a[3]) && (a[2] == a[1])) ||
        ((a[1] == a[2]) && (a[2] == a[3]) && (a[3] == a[4])))
      str = "four card";
    else if (((a[0] == a[1]) && (a[1] == a[2]) && (a[3] == a[4])) ||
             ((a[0] == a[1]) && (a[2] == a[3]) && (a[3] == a[4])))
      str = "full house";
    else if (((a[0] == 1) && (a[1] == 10) && (a[2] == 11) && (a[3] == 12) &&
              (a[4] == 13)) ||
             ((a[0] + 1 == a[1]) && (a[1] + 1 == a[2]) && (a[2] + 1 == a[3]) &&
              (a[3] + 1 == a[4])))
      str = "straight";
    else if (((a[0] == a[1]) && (a[1] == a[2])) ||
             ((a[1] == a[2]) && (a[2] == a[3])) ||
             ((a[2] == a[3]) && (a[3] == a[4])))
      str = "three card";
    else if (((a[0] == a[1]) && ((a[2] == a[3]) || (a[3] == a[4]))) ||
             ((a[1] == a[2]) && (a[3] == a[4])))
      str = "two pair";
    else if ((a[0] == a[1]) || (a[1] == a[2]) || (a[2] == a[3]) ||
             (a[3] == a[4]))
      str = "one pair";

    cout << str << endl;
  }
  return 0;
}
Problem p00038 (C++)

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

int c[5]; // card

bool straight() {
  if (c[4] == 13 && c[3] == 12 && c[2] == 11 && c[1] == 10 && c[0] == 1)
    return true;
  for (int i = 0; i < 4; i++) {
    if (c[i + 1] - c[i] != 1)
      return false;
  }
  return true;
}

int main(void) {
  while (~scanf("%d %d %d %d %d", c, c + 1, c + 2, c + 3, c + 4)) {
    sort(c, c + 5);
    if (c[0] == c[3] || c[1] == c[4]) {
      puts("four card");
    } else if ((c[0] == c[2] && c[3] == c[4]) ||
               (c[0] == c[1] && c[2] == c[4])) {
      puts("full house");
    } else if (straight()) {
      puts("straight");
    } else if (c[0] == c[2] || c[1] == c[3] || c[2] == c[4]) {
      puts("three card");
    } else if ((c[0] == c[1] && (c[2] == c[3] || c[3] == c[4])) ||
               (c[1] == c[2] && c[3] == c[4])) {
      puts("two pair");
    } else if (c[0] == c[1] || c[1] == c[2] || c[2] == c[3] || c[3] == c[4]) {
      puts("one pair");
    } else {
      puts("null");
    }
  }

  return 0;
}

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

int c[5]; // card

bool straight() {
  if (c[4] == 13 && c[3] == 12 && c[2] == 11 && c[1] == 10 && c[0] == 1)
    return true;
  for (int i = 0; i < 4; i++) {
    if (c[i + 1] - c[i] != 1)
      return false;
  }
  return true;
}

int main(void) {
  while (~scanf("%d,%d,%d,%d,%d", c, c + 1, c + 2, c + 3, c + 4)) {
    sort(c, c + 5);
    if (c[0] == c[3] || c[1] == c[4]) {
      puts("four card");
    } else if ((c[0] == c[2] && c[3] == c[4]) ||
               (c[0] == c[1] && c[2] == c[4])) {
      puts("full house");
    } else if (straight()) {
      puts("straight");
    } else if (c[0] == c[2] || c[1] == c[3] || c[2] == c[4]) {
      puts("three card");
    } else if ((c[0] == c[1] && (c[2] == c[3] || c[3] == c[4])) ||
               (c[1] == c[2] && c[3] == c[4])) {
      puts("two pair");
    } else if (c[0] == c[1] || c[1] == c[2] || c[2] == c[3] || c[3] == c[4]) {
      puts("one pair");
    } else {
      puts("null");
    }
  }

  return 0;
}
Problem p00038 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
string s[] = {"one pair", "two pair", "three card", "straight", "null"};

int str(int n) {
  cout << s[n] << endl;
  return 0;
}

int main() {
  int a[5], t[14], ans, m;
  while (scanf("%d,%d,%d,%d,%d", &a[0], &a[1], &a[2], &a[3], &a[4])) {
    ans = -1, m = 0;
    sort(a, a + 5);
    fill(t, t + 14, 0);
    for (int i = 0; i < 5; i++) {
      t[a[i]]++;
      if (m < t[a[i]])
        m = t[a[i]];
    }

    int flag = 0;
    if (a[0] == 1 && a[1] == 10 && a[2] == 11 && a[3] == 12 && a[4] == 13) {
      str(3);
      continue;
    }
    for (int i = 1; i < 5 && a[i - 1] + 1 == a[i]; i++)
      flag = i;
    if (flag == 4) {
      str(3);
      continue;
    }

    if (m == 4) {
      cout << "four card" << endl;
      continue;
    }
    if (m == 3) {
      int f = 1;
      for (int i = 1; i <= 13; i++) {
        if (t[i] == 2)
          f = 0;
      }
      if (!f) {
        cout << "full house" << endl;
        continue;
      }
      ans = 2;
    }

    for (int i = 1; i <= 13; i++) {
      if (!ans && t[i] == 2)
        ans = 1;
      if (ans != 1 && t[i] == 2)
        ans = 0;
    }
    if (ans == -1)
      ans = 4;
    str(ans);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
string s[] = {"one pair", "two pair", "three card", "straight", "null"};

int str(int n) {
  cout << s[n] << endl;
  return 0;
}

int main() {
  int a[5], t[14], ans, m;
  while (scanf("%d,%d,%d,%d,%d", &a[0], &a[1], &a[2], &a[3], &a[4]) != EOF) {
    ans = -1, m = 0;
    sort(a, a + 5);
    fill(t, t + 14, 0);
    for (int i = 0; i < 5; i++) {
      t[a[i]]++;
      if (m < t[a[i]])
        m = t[a[i]];
    }

    int flag = 0;
    if (a[0] == 1 && a[1] == 10 && a[2] == 11 && a[3] == 12 && a[4] == 13) {
      str(3);
      continue;
    }
    for (int i = 1; i < 5 && a[i - 1] + 1 == a[i]; i++)
      flag = i;
    if (flag == 4) {
      str(3);
      continue;
    }

    if (m == 4) {
      cout << "four card" << endl;
      continue;
    }
    if (m == 3) {
      int f = 1;
      for (int i = 1; i <= 13; i++) {
        if (t[i] == 2)
          f = 0;
      }
      if (!f) {
        cout << "full house" << endl;
        continue;
      }
      ans = 2;
    }

    for (int i = 1; i <= 13; i++) {
      if (!ans && t[i] == 2)
        ans = 1;
      if (ans != 1 && t[i] == 2)
        ans = 0;
    }
    if (ans == -1)
      ans = 4;
    str(ans);
  }
  return 0;
}
Problem p00038 (C++)

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

bool isstraight(int c[]);

int main() {
  int card[5];

  while (scanf("%d,%d,%d,%d,%d", &card[0], &card[1], &card[2], &card[3],
               &card[4])) {
    sort(card, card + 5);
    int n = 0;
    for (int i = 0; i < 4; i++) {
      for (int j = i + 1; j < 5; j++) {
        if (card[i] == card[j]) {
          n++;
        }
      }
    }

    switch (n) {
    case 1:
      cout << "one pair" << endl;
      break;
    case 2:
      cout << "two pair" << endl;
      break;
    case 3:
      cout << "three card" << endl;
      break;
    case 4:
      cout << "full house" << endl;
      break;
    case 6:
      cout << "four card" << endl;
      break;
    case 10:
      cout << "four card" << endl;
      break;
    default:
      if (isstraight(card)) {
        cout << "straight" << endl;
      } else {
        cout << "null" << endl;
      }
      break;
    }
  }

  return 0;
}

bool isstraight(int c[]) {
  for (int i = 1; i < 9; i++) {
    if (c[0] == i && c[1] == i + 1 && c[2] == i + 2 && c[3] == i + 3 &&
        c[4] == i + 4) {
      return true;
    }
    if (c[0] == 1 && c[1] == 10 && c[2] == 11 && c[3] == 12 && c[4] == 13) {
      return true;
    }
  }
  return false;
}

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

bool isstraight(int c[]);

int main() {
  int card[5];

  while (~scanf("%d,%d,%d,%d,%d", &card[0], &card[1], &card[2], &card[3],
                &card[4])) {
    sort(card, card + 5);
    int n = 0;
    for (int i = 0; i < 4; i++) {
      for (int j = i + 1; j < 5; j++) {
        if (card[i] == card[j]) {
          n++;
        }
      }
    }

    switch (n) {
    case 1:
      cout << "one pair" << endl;
      break;
    case 2:
      cout << "two pair" << endl;
      break;
    case 3:
      cout << "three card" << endl;
      break;
    case 4:
      cout << "full house" << endl;
      break;
    case 6:
      cout << "four card" << endl;
      break;
    case 10:
      cout << "four card" << endl;
      break;
    default:
      if (isstraight(card)) {
        cout << "straight" << endl;
      } else {
        cout << "null" << endl;
      }
      break;
    }
  }

  return 0;
}

bool isstraight(int c[]) {
  for (int i = 1; i < 9; i++) {
    if (c[0] == i && c[1] == i + 1 && c[2] == i + 2 && c[3] == i + 3 &&
        c[4] == i + 4) {
      return true;
    }
    if (c[0] == 1 && c[1] == 10 && c[2] == 11 && c[3] == 12 && c[4] == 13) {
      return true;
    }
  }
  return false;
}
Problem p00038 (C++)

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

using namespace std;

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

int card[5];

int c[13];

void solve() {
  memset(c, 0, sizeof(c));
  REP(i, 5) { c[card[i] - 1]++; }
  bool f1 = false, f2 = false;
  REP(i, 13) {
    if (c[i] >= 4) {
      f1 = true;
    }
  }
  if (f1) {
    cout << "four card" << endl;
    return;
  }
  REP(i, 13) {
    if (c[i] == 3) {
      f1 = true;
    } else if (c[i] == 2) {
      f2 = true;
    }
  }
  if (f1 && f2) {
    cout << "full house" << endl;
    return;
  }
  f1 = f2 = false;
  REP(i, 10) {
    bool f = true;
    REP(j, 5) {
      if (c[(i + j) % 13] != 1) {
        f = false;
      }
    }
    if (f) {
      f1 = true;
    }
  }
  if (f1) {
    cout << "straight" << endl;
    return;
  }
  REP(i, 13) {
    if (c[i] == 3) {
      f1 = true;
    }
  }
  if (f1) {
    cout << "three card" << endl;
    return;
  }
  int k = 0;
  REP(i, 13) {
    if (c[i] == 2) {
      k++;
    }
  }
  if (k == 2) {
    cout << "two pair" << endl;
  } else if (k == 1) {
    cout << "one pair" << endl;
  } else {
    cout << "null" << endl;
  }
}

int main() {
  while (
      scanf("%d,%d,%d,%d,%d", card, card + 1, card + 2, card + 3, card + 4)) {
    solve();
  }
  return 0;
}

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

using namespace std;

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

int card[5];

int c[13];

void solve() {
  memset(c, 0, sizeof(c));
  REP(i, 5) { c[card[i] - 1]++; }
  bool f1 = false, f2 = false;
  REP(i, 13) {
    if (c[i] >= 4) {
      f1 = true;
    }
  }
  if (f1) {
    cout << "four card" << endl;
    return;
  }
  REP(i, 13) {
    if (c[i] == 3) {
      f1 = true;
    } else if (c[i] == 2) {
      f2 = true;
    }
  }
  if (f1 && f2) {
    cout << "full house" << endl;
    return;
  }
  f1 = f2 = false;
  REP(i, 10) {
    bool f = true;
    REP(j, 5) {
      if (c[(i + j) % 13] != 1) {
        f = false;
      }
    }
    if (f) {
      f1 = true;
    }
  }
  if (f1) {
    cout << "straight" << endl;
    return;
  }
  REP(i, 13) {
    if (c[i] == 3) {
      f1 = true;
    }
  }
  if (f1) {
    cout << "three card" << endl;
    return;
  }
  int k = 0;
  REP(i, 13) {
    if (c[i] == 2) {
      k++;
    }
  }
  if (k == 2) {
    cout << "two pair" << endl;
  } else if (k == 1) {
    cout << "one pair" << endl;
  } else {
    cout << "null" << endl;
  }
}

int main() {
  while (scanf("%d,%d,%d,%d,%d", card, card + 1, card + 2, card + 3,
               card + 4) != EOF) {
    solve();
  }
  return 0;
}
Problem p00038 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
typedef long long LL;
#define SORT(c) sort((c).begin(), (c).end())

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

using namespace std;
int main(void) {
  for (;;) {
    int n[5];
    char c;
    REP(i, 5) {
      if (i)
        cin >> c;
      if ((scanf("%d", &n[i])) == 0)
        return 0;
    }
    int num[16];
    REP(i, 16) num[i] = 0;
    REP(i, 5)++ num[n[i]];
    num[14] = num[1];
    string s = "null";
    REP(i, 11) {
      int st = 0;
      REP(j, 5) if (num[i + j])++ st;
      if (st == 5)
        s = "straight";
    }
    int pa = 0;
    REP(i, 5) REP(j, i) if (n[i] == n[j])++ pa;
    if (pa == 1)
      s = "one pair";
    if (pa == 2)
      s = "two pair";
    if (pa == 3)
      s = "three card";
    if (pa == 4)
      s = "full house";
    if (pa == 6)
      s = "four card";
    cout << s << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
typedef long long LL;
#define SORT(c) sort((c).begin(), (c).end())

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

using namespace std;
int main(void) {
  for (;;) {
    int n[5];
    char c;
    REP(i, 5) {
      if (i)
        cin >> c;
      n[i] = 0;
      cin >> n[i];
      if (n[i] == 0)
        return 0;
    }
    int num[16];
    REP(i, 16) num[i] = 0;
    REP(i, 5)++ num[n[i]];
    num[14] = num[1];
    string s = "null";
    REP(i, 11) {
      int st = 0;
      REP(j, 5) if (num[i + j])++ st;
      if (st == 5)
        s = "straight";
    }
    int pa = 0;
    REP(i, 5) REP(j, i) if (n[i] == n[j])++ pa;
    if (pa == 1)
      s = "one pair";
    if (pa == 2)
      s = "two pair";
    if (pa == 3)
      s = "three card";
    if (pa == 4)
      s = "full house";
    if (pa == 6)
      s = "four card";
    cout << s << endl;
  }
}
Problem p00038 (C++)

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

int main() {
  int c;
  int cards[14], pairs[5];
  while (scanf("%d", &c), c) {
    memset(cards, 0, sizeof(cards));
    memset(pairs, 0, sizeof(pairs));
    cards[c]++;
    for (int i = 0; i < 4; i++) {
      scanf(",%d", &c);
      cards[c]++;
    }
    for (int i = 1; i <= 13; i++) {
      pairs[cards[i]]++;
    }
    if (pairs[4]) {
      cout << "four card" << endl;
    } else if (pairs[3] && pairs[2]) {
      cout << "full house" << endl;
    } else if (pairs[1] == 5) {
      int i = 1;
      for (; i <= 10; i++) {
        if (cards[i] && cards[i + 1] && cards[i + 2] && cards[i + 3] &&
            cards[((i + 3) % 13) + 1]) {
          cout << "straight" << endl;
          break;
        }
      }
      if (i > 10)
        cout << "null" << endl;
    } else if (pairs[3]) {
      cout << "three card" << endl;
    } else if (pairs[2] == 2) {
      cout << "two pair" << endl;
    } else if (pairs[2]) {
      cout << "one pair" << endl;
    } else {
      cout << "null" << endl;
    }
  }
  return 0;
}

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

int main() {
  int c;
  int cards[14], pairs[5];
  while (scanf("%d", &c) != -1) {
    memset(cards, 0, sizeof(cards));
    memset(pairs, 0, sizeof(pairs));
    cards[c]++;
    for (int i = 0; i < 4; i++) {
      scanf(",%d", &c);
      cards[c]++;
    }
    for (int i = 1; i <= 13; i++) {
      pairs[cards[i]]++;
    }
    if (pairs[4]) {
      cout << "four card" << endl;
    } else if (pairs[3] && pairs[2]) {
      cout << "full house" << endl;
    } else if (pairs[1] == 5) {
      int i = 1;
      for (; i <= 10; i++) {
        if (cards[i] && cards[i + 1] && cards[i + 2] && cards[i + 3] &&
            cards[((i + 3) % 13) + 1]) {
          cout << "straight" << endl;
          break;
        }
      }
      if (i > 10)
        cout << "null" << endl;
    } else if (pairs[3]) {
      cout << "three card" << endl;
    } else if (pairs[2] == 2) {
      cout << "two pair" << endl;
    } else if (pairs[2]) {
      cout << "one pair" << endl;
    } else {
      cout << "null" << endl;
    }
  }
  return 0;
}
Problem p00039 (C++)

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

int changeTheChar(char c) {

  if (c == 'I') {
    return 1;
  } else if (c == 'V') {
    return 5;
  } else if (c == 'X') {
    return 10;
  } else if (c == 'L') {
    return 50;
  } else if (c == 'C') {
    return 100;
  } else if (c == 'D') {
    return 500;
  } else if (c == 'M') {
    return 1000;
  }

  return 0;
}

int main() {

  while (true) {

    string str;
    cin >> str;

    vector<int> roman(str.size(), 0);
    // ????????????
    for (int i = 0; i < str.size(); i++) {
      roman[i] = changeTheChar(str[i]);
    }

    for (int i = 0; i < roman.size(); i++) {
      // cout << roman[i] << " ";
    }

    int res = 0;
    while (roman.size() > 0) {
      int maxI = 0;
      int maxIndex;
      // ?????§???????´¢
      for (int i = roman.size() - 1; i >= 0; i--) {
        if (maxI <= roman[i]) {
          maxI = roman[i];
          maxIndex = i;
        }
      }
      // cout << maxIndex << endl;
      // cout << maxI << endl;

      if (maxIndex != 0) {
        res += roman[maxIndex] - roman[maxIndex - 1];
        roman.erase(roman.begin());
        roman.erase(roman.begin());
      } else {
        res += roman[maxIndex];
        roman.erase(roman.begin());
      }

      // cout << res << endl;
    }

    cout << res << endl;
  }

  return 0;
}

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

int changeTheChar(char c) {

  if (c == 'I') {
    return 1;
  } else if (c == 'V') {
    return 5;
  } else if (c == 'X') {
    return 10;
  } else if (c == 'L') {
    return 50;
  } else if (c == 'C') {
    return 100;
  } else if (c == 'D') {
    return 500;
  } else if (c == 'M') {
    return 1000;
  }

  return 0;
}

int main() {
  string str;
  while (cin >> str) {

    vector<int> roman(str.size(), 0);
    // ????????????
    for (int i = 0; i < str.size(); i++) {
      roman[i] = changeTheChar(str[i]);
    }

    for (int i = 0; i < roman.size(); i++) {
      // cout << roman[i] << " ";
    }

    int res = 0;
    while (roman.size() > 0) {
      int maxI = 0;
      int maxIndex;
      // ?????§???????´¢
      for (int i = roman.size() - 1; i >= 0; i--) {
        if (maxI <= roman[i]) {
          maxI = roman[i];
          maxIndex = i;
        }
      }
      // cout << maxIndex << endl;
      // cout << maxI << endl;

      if (maxIndex != 0) {
        res += roman[maxIndex] - roman[maxIndex - 1];
        roman.erase(roman.begin());
        roman.erase(roman.begin());
      } else {
        res += roman[maxIndex];
        roman.erase(roman.begin());
      }

      // cout << res << endl;
    }

    cout << res << endl;
  }

  return 0;
}
Problem p00040 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
using namespace std;
string change(int a, int b, string str) {
  for (int i = 0; i < str.size(); i++)
    str[i] = ((str[i] - 'a') * a + b) % 26 + 'a';
  return str;
}
string out(int a, int b, string str) {
  for (int i = 0; i < str.size(); i++) {
    if ('a' <= str[i] && str[i] <= 'z') {
      for (int j = 0; j < 26; j++) {
        if ((j * a + b) % 26 + 'a' == str[i]) {
          str[i] = j + 'a';
          break;
        }
      }
    }
  }
  return str;
}
int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    string str;
    getchar();
    getline(cin, str);
    for (int j = 1;; j++) {
      for (int k = 0; k < 26; k++) {
        for (int l = 0; l < str.size() - 3; l++) {
          if (change(j, k, "that") == str.substr(l, 4)) {
            cout << out(j, k, str) << endl;
            goto exit;
          }
          if (change(j, k, "this") == str.substr(l, 4)) {
            cout << out(j, k, str) << endl;
            goto exit;
          }
        }
      }
    }
  exit:;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
using namespace std;
string change(int a, int b, string str) {
  for (int i = 0; i < str.size(); i++)
    str[i] = ((str[i] - 'a') * a + b) % 26 + 'a';
  return str;
}
string out(int a, int b, string str) {
  for (int i = 0; i < str.size(); i++) {
    if ('a' <= str[i] && str[i] <= 'z') {
      for (int j = 0; j < 26; j++) {
        if ((j * a + b) % 26 + 'a' == str[i]) {
          str[i] = j + 'a';
          break;
        }
      }
    }
  }
  return str;
}
int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) {
    string str;
    if (!i)
      getchar();
    getline(cin, str);
    for (int j = 1;; j++) {
      for (int k = 0; k < 26; k++) {
        for (int l = 0; l < str.size() - 3; l++) {
          if (change(j, k, "that") == str.substr(l, 4)) {
            cout << out(j, k, str) << endl;
            goto exit;
          }
          if (change(j, k, "this") == str.substr(l, 4)) {
            cout << out(j, k, str) << endl;
            goto exit;
          }
        }
      }
    }
  exit:;
  }
  return 0;
}
Problem p00041 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
int d[4];
bool flg = false;
string s = "+-*";
int calc(int a, int op, int b) {
  if (op == 0)
    return a + b;
  if (op == 1)
    return a - b;
  return a * b;
}
bool solve(int a, int b, int c) {
  flg++;
  if (calc(calc(calc(d[0], a, d[1]), b, d[2]), c, d[3]) == 10) {
    return printf("(((%d %c %d) %c %d) %c %d)\n", d[0], s[a], d[1], s[b], d[2],
                  s[c], d[3]);
  }
  if (calc(calc(d[0], a, d[1]), b, calc(d[2], c, d[3])) == 10) {
    return printf("((%d %c %d) %c (%d %c %d))\n", d[0], s[a], d[1], s[b], d[2],
                  s[c], d[3]);
  }
  if (calc(calc(d[0], a, calc(d[1], b, d[2])), c, d[3]) == 10) {
    return printf("((%d %c (%d %c %d)) %c %d)\n", d[0], s[a], d[1], s[b], d[2],
                  s[c], d[3]);
  }
  return flg = false;
}

bool judge() { rep(i, 3) rep(j, 3) rep(k, 3) if (solve(i, j, k)) return true; }
int main() {
  while (flg = true) {
    rep(i, 4) cin >> d[i];
    if (!d[0] & d[1] & d[2] & d[3])
      break;
    sort(d, d + 4);
    do
      if (judge())
        break;
    while (next_permutation(d, d + 4));
    if (!flg)
      puts("0");
  }
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
int d[4];
bool flg = false;
string s = "+-*";
int calc(int a, int op, int b) {
  if (op == 0)
    return a + b;
  if (op == 1)
    return a - b;
  return a * b;
}
bool solve(int a, int b, int c) {
  flg++;
  if (calc(calc(calc(d[0], a, d[1]), b, d[2]), c, d[3]) == 10) {
    return printf("(((%d %c %d) %c %d) %c %d)\n", d[0], s[a], d[1], s[b], d[2],
                  s[c], d[3]);
  }
  if (calc(calc(d[0], a, d[1]), b, calc(d[2], c, d[3])) == 10) {
    return printf("((%d %c %d) %c (%d %c %d))\n", d[0], s[a], d[1], s[b], d[2],
                  s[c], d[3]);
  }
  if (calc(calc(d[0], a, calc(d[1], b, d[2])), c, d[3]) == 10) {
    return printf("((%d %c (%d %c %d)) %c %d)\n", d[0], s[a], d[1], s[b], d[2],
                  s[c], d[3]);
  }
  return flg = false;
}

bool judge() { rep(i, 3) rep(j, 3) rep(k, 3) if (solve(i, j, k)) return true; }
int main() {
  while (flg = true) {
    rep(i, 4) cin >> d[i];
    if (!d[0] && !d[1] && !d[2] && !d[3])
      break;
    sort(d, d + 4);
    do
      if (judge())
        break;
    while (next_permutation(d, d + 4));
    if (!flg)
      puts("0");
  }
}
Problem p00042 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <list>
#define MAX(x, y) ((x > y) ? x : y)

using namespace std;

FILE *wfp = fopen("output.txt", "w");
FILE *rfp = fopen("input.txt", "r");

int W;
int N;

int kati[10000];
int omosa[10000];

int DP[1005][10005]; //[i][j]i番目までにjより小さくなるような価値
int DP_W[10005];     // そのときの重さ

int CASE = 0;
int main() {
  /// wfp=stdout;
  /// rfp=stdin;
  while (true) {
    CASE++;
    fill(DP_W, DP_W + 10005, 0);
    for (int i = 0; i < 10000; i++)
      DP[0][i] = 0;
    for (int i = 0; i < 1000; i++)
      DP[i][0] = 0;
    fscanf(rfp, "%d", &W);
    if (W == 0)
      break;
    fscanf(rfp, "%d", &N);
    for (int i = 0; i < N; i++) {
      fscanf(rfp, "%d,%d", &kati[i], &omosa[i]);
    }
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < W; j++) {
        if (j + 1 - omosa[i] >= 0)
          DP[i + 1][j + 1] =
              MAX(DP[i][j + 1], DP[i][j + 1 - omosa[i]] + kati[i]);
        else
          DP[i + 1][j + 1] = DP[i][j + 1];
      }
    }
    DP_W[0] = 0;
    for (int i = 1; i <= W; i++) {
      if (DP[N][i] == DP[N][i - 1])
        DP_W[i] = DP_W[i - 1];
      else
        DP_W[i] = i;
    }

    fprintf(wfp, "Case %d:\n", CASE);
    fprintf(wfp, "%d\n", DP[N][W]);
    fprintf(wfp, "%d\n", DP_W[W]);
  }
  fclose(rfp);
  fclose(wfp);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <list>
#define MAX(x, y) ((x > y) ? x : y)

using namespace std;

FILE *wfp = fopen("output.txt", "w");
FILE *rfp = fopen("input.txt", "r");

int W;
int N;

int kati[10000];
int omosa[10000];

int DP[1005][10005]; //[i][j]i番目までにjより小さくなるような価値
int DP_W[10005];     // そのときの重さ

int CASE = 0;
int main() {
  wfp = stdout;
  rfp = stdin;
  while (true) {
    CASE++;
    fill(DP_W, DP_W + 10005, 0);
    for (int i = 0; i < 10000; i++)
      DP[0][i] = 0;
    for (int i = 0; i < 1000; i++)
      DP[i][0] = 0;
    fscanf(rfp, "%d", &W);
    if (W == 0)
      break;
    fscanf(rfp, "%d", &N);
    for (int i = 0; i < N; i++) {
      fscanf(rfp, "%d,%d", &kati[i], &omosa[i]);
    }
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < W; j++) {
        if (j + 1 - omosa[i] >= 0)
          DP[i + 1][j + 1] =
              MAX(DP[i][j + 1], DP[i][j + 1 - omosa[i]] + kati[i]);
        else
          DP[i + 1][j + 1] = DP[i][j + 1];
      }
    }
    DP_W[0] = 0;
    for (int i = 1; i <= W; i++) {
      if (DP[N][i] == DP[N][i - 1])
        DP_W[i] = DP_W[i - 1];
      else
        DP_W[i] = i;
    }

    fprintf(wfp, "Case %d:\n", CASE);
    fprintf(wfp, "%d\n", DP[N][W]);
    fprintf(wfp, "%d\n", DP_W[W]);
  }
  fclose(rfp);
  fclose(wfp);
  return 0;
}
Problem p00042 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int dk[10001][1001];

int main() {
  int i, j, takara[1001][2], w, n, c, maxv;
  c = 0;
  while (0 <= scanf("%d", &w)) {
    if (w == 0)
      break;
    scanf("%d", &n);
    ++c;

    for (i = 1; i <= n; i++) {
      scanf("%d,%d", &takara[i][0], &takara[i][1]);
    }

    memset(dk, 0, sizeof(dk));

    for (i = 1; i <= n; i++) {
      for (j = 1; j <= w; j++) {
        if (j >= takara[i][1]) {
          dk[i][j] = (dk[i - 1][j - takara[i][1]] + takara[i][0] > dk[i - 1][j]
                          ? dk[i - 1][j - takara[i][1]] + takara[i][0]
                          : dk[i - 1][j]);
          continue;
        }
        dk[i][j] = dk[i - 1][j];
      }
    }

    maxv = dk[n][w];

    for (i = 0; i <= w; i++) {
      if (maxv == dk[n][i])
        break;
    }

    printf("Case %d:\n", c);
    printf("%d\n%d\n", maxv, i);
  }
  return 0;
}

Fixed submission:
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

short dk[10001][1001];

int main() {
  int i, j, takara[1001][2], w, n, c, maxv;
  c = 0;
  while (0 <= scanf("%d", &w)) {
    if (w == 0)
      break;
    scanf("%d", &n);
    ++c;

    for (i = 1; i <= n; i++) {
      scanf("%d,%d", &takara[i][0], &takara[i][1]);
    }

    memset(dk, 0, sizeof(dk));

    for (i = 1; i <= n; i++) {
      for (j = 1; j <= w; j++) {
        if (j >= takara[i][1]) {
          dk[i][j] = (dk[i - 1][j - takara[i][1]] + takara[i][0] > dk[i - 1][j]
                          ? dk[i - 1][j - takara[i][1]] + takara[i][0]
                          : dk[i - 1][j]);
          continue;
        }
        dk[i][j] = dk[i - 1][j];
      }
    }

    maxv = dk[n][w];

    for (i = 0; i <= w; i++) {
      if (maxv == dk[n][i])
        break;
    }

    printf("Case %d:\n", c);
    printf("%d\n%d\n", maxv, i);
  }
  return 0;
}
Problem p00042 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int MOD = 1000000007;
int main() {
  int w, n;
  int ct = 1;
  while (cin >> w >> n, w) {
    int va[n], we[n];
    for (int i = 0; i < n; i++)
      scanf("%d, %d", &va[i], &we[i]);
    int dp[1010][1010];
    for (int i = 0; i < 1010; i++)
      for (int j = 0; j < 1010; j++)
        dp[i][j] = 0;
    for (int i = 0; i <= n; i++)
      for (int j = 0; j <= w; j++) {
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
        if (j + we[i] <= w)
          dp[i + 1][j + we[i]] = max(dp[i + 1][j + we[i]], dp[i][j] + va[i]);
      }
    int macs = 0, sumwe = 1010;
    for (int i = 0; i <= n; i++)
      for (int j = 0; j <= w; j++)
        if (dp[i][j] == dp[n][w])
          sumwe = min(j, sumwe);
    cout << "Case " << ct << ":" << endl << dp[n][w] << endl << sumwe << endl;
    ct++;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int MOD = 1000000007;
int main() {
  int w, n;
  int ct = 1;
  while (cin >> w >> n, w) {
    int va[n], we[n];
    for (int i = 0; i < n; i++)
      scanf("%d, %d", &va[i], &we[i]);
    int dp[1010][1010];
    for (int i = 0; i < 1010; i++)
      for (int j = 0; j < 1010; j++)
        dp[i][j] = 0;
    for (int i = 0; i < n; i++)
      for (int j = 0; j <= w; j++) {
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
        if (j + we[i] <= w)
          dp[i + 1][j + we[i]] = max(dp[i + 1][j + we[i]], dp[i][j] + va[i]);
      }
    int macs = 0, sumwe = 1010;
    for (int i = 0; i <= n; i++)
      for (int j = 0; j <= w; j++)
        if (dp[i][j] == dp[n][w])
          sumwe = min(j, sumwe);
    cout << "Case " << ct << ":" << endl << dp[n][w] << endl << sumwe << endl;
    ct++;
  }
  return 0;
}
Problem p00042 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
#include <string.h>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define max_n 1000
#define max_w 1000
using namespace std;

int N, W;
long long dp[max_n][max_w];
long long dw[max_n][max_w];
int w[max_n], v[max_n];

long long rec(int i, int j) {
  int res;
  if (dp[i][j] != -1)
    return dp[i][j];
  if (i == N) {
    res = 0;
    dw[i][j] = 0;
  } else if (j < w[i]) {
    res = rec(i + 1, j);
    dw[i][j] = dw[i + 1][j];
  } else {
    long long r1 = rec(i + 1, j);
    long long r2 = rec(i + 1, j - w[i]) + v[i];
    if (r1 > r2) {
      res = r1;
      dw[i][j] = dw[i + 1][j];
    } else if (r2 > r1) {
      res = r2;
      dw[i][j] = dw[i + 1][j - w[i]] + w[i];
    } else {
      if (dw[i + 1][j] > (dw[i + 1][j - w[i]] + w[i])) {
        res = r2;
        dw[i][j] = dw[i + 1][j - w[i]] + w[i];
      } else {
        res = r1;
        dw[i][j] = dw[i + 1][j];
      }
    }
  }
  return dp[i][j] = res;
}

int main() {
  int p = 1;
  while (cin >> W, W) {
    cin >> N;
    rep(i, N) { scanf("%d,%d", &v[i], &w[i]); }
    memset(dp, -1, sizeof(dp));
    memset(dw, -1, sizeof(dw));
    cout << "Case " << p << ":" << endl;
    cout << rec(0, W) << endl;
    cout << dw[0][W] << endl;
    p++;
    /*cout<<"value="<<rec(0,W)<<"
    cost="<<dw[0][W]<<endl;//????????§??????????????????
    cout<<"cost="<<dw[0][W]<<endl;*/
  }
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
#include <string.h>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define max_n 1000
#define max_w 1000
using namespace std;

int N, W;
long long dp[max_n + 1][max_w + 1];
long long dw[max_n + 1][max_w + 1];
int w[max_n], v[max_n];

long long rec(int i, int j) {
  int res;
  if (dp[i][j] != -1)
    return dp[i][j];
  if (i == N) {
    res = 0;
    dw[i][j] = 0;
  } else if (j < w[i]) {
    res = rec(i + 1, j);
    dw[i][j] = dw[i + 1][j];
  } else {
    long long r1 = rec(i + 1, j);
    long long r2 = rec(i + 1, j - w[i]) + v[i];
    if (r1 > r2) {
      res = r1;
      dw[i][j] = dw[i + 1][j];
    } else if (r2 > r1) {
      res = r2;
      dw[i][j] = dw[i + 1][j - w[i]] + w[i];
    } else {
      if (dw[i + 1][j] > (dw[i + 1][j - w[i]] + w[i])) {
        res = r2;
        dw[i][j] = dw[i + 1][j - w[i]] + w[i];
      } else {
        res = r1;
        dw[i][j] = dw[i + 1][j];
      }
    }
  }
  return dp[i][j] = res;
}

int main() {
  int p = 1;
  while (cin >> W, W) {
    cin >> N;
    rep(i, N) { scanf("%d,%d", &v[i], &w[i]); }
    memset(dp, -1, sizeof(dp));
    memset(dw, -1, sizeof(dw));
    cout << "Case " << p << ":" << endl;
    cout << rec(0, W) << endl;
    cout << dw[0][W] << endl;
    p++;
    /*cout<<"value="<<rec(0,W)<<"
    cost="<<dw[0][W]<<endl;//????????§??????????????????
    cout<<"cost="<<dw[0][W]<<endl;*/
  }
  return 0;
}
Problem p00042 (C++)

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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define all(v) (v).begin(), (v).end()
#define rev(s) (s).rbegin(), (s).rend()
#define MP make_pair
#define X first
#define Y second

using namespace std;

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

const int INF = 1000000000;
const double EPS = 1e-10;

int main() {
  freopen("test.txt", "r", stdin);
  int casenum = 0;
  while (1) {
    int W, n;
    cin >> W >> n;
    if (!W)
      break;

    vector<int> w(n + 1);
    vector<int> v(n + 1);
    FOR(i, 1, n + 1) { scanf("%d,%d", &v[i], &w[i]); }

    vector<vector<int>> table(n + 1, vector<int>(W + 1));

    FOR(i, 1, n + 1) {
      for (int j = 0; j <= W; j++) {
        if (j >= w[i]) {
          table[i][j] = max(table[i - 1][j], table[i - 1][j - w[i]] + v[i]);
        } else {
          table[i][j] = table[i - 1][j];
        }
      }
    }

    int idx = 0;
    rep(i, W + 1) {
      if (table[n][idx] < table[n][i]) {
        idx = i;
      }
    }

    cout << "Case " << ++casenum << ':' << endl;
    cout << table[n][idx] << endl;
    cout << idx << endl;
  }

  return 0;
}

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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define all(v) (v).begin(), (v).end()
#define rev(s) (s).rbegin(), (s).rend()
#define MP make_pair
#define X first
#define Y second

using namespace std;

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

const int INF = 1000000000;
const double EPS = 1e-10;

int main() {
  //	freopen("test.txt", "r", stdin);
  int casenum = 0;
  while (1) {
    int W, n;
    cin >> W >> n;
    if (!W)
      break;

    vector<int> w(n + 1);
    vector<int> v(n + 1);
    FOR(i, 1, n + 1) { scanf("%d,%d", &v[i], &w[i]); }

    vector<vector<int>> table(n + 1, vector<int>(W + 1));

    FOR(i, 1, n + 1) {
      for (int j = 0; j <= W; j++) {
        if (j >= w[i]) {
          table[i][j] = max(table[i - 1][j], table[i - 1][j - w[i]] + v[i]);
        } else {
          table[i][j] = table[i - 1][j];
        }
      }
    }

    int idx = 0;
    rep(i, W + 1) {
      if (table[n][idx] < table[n][i]) {
        idx = i;
      }
    }

    cout << "Case " << ++casenum << ':' << endl;
    cout << table[n][idx] << endl;
    cout << idx << endl;
  }

  return 0;
}
Problem p00042 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <iostream>
#define int long long
using namespace std;
int dp[1001], N, v[100], w[100], W;
signed main() {
  for (int l = 1;; l++) {
    fill(dp, dp + 1001, 0);
    /*fill(v,v+100,0);
    fill(w,w+100,0);*/
    cin >> W;
    if (W == 0)
      break;
    cin >> N;
    for (int i = 0; i < N; i++) {
      scanf("%lld,%lld", &v[i], &w[i]);
    }
    for (int i = 0; i < N; i++) {
      for (int j = W; j >= w[i]; j--) {
        dp[j] = max(dp[j], dp[j - w[i]] + v[i]);
      }
    }
    for (int i = W; i >= 0; i--) {
      if (dp[W] != dp[i]) {
        cout << "Case " << l << ":" << endl << dp[W] << endl << i + 1 << endl;
        break;
      }
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#define int long long
using namespace std;
int dp[1001], N, v[1001], w[1001], W;
signed main() {
  for (int l = 1;; l++) {
    fill(dp, dp + 1001, 0);
    /*fill(v,v+100,0);
    fill(w,w+100,0);*/
    cin >> W;
    if (W == 0)
      break;
    cin >> N;
    for (int i = 0; i < N; i++) {
      scanf("%lld,%lld", &v[i], &w[i]);
    }
    for (int i = 0; i < N; i++) {
      for (int j = W; j >= w[i]; j--) {
        dp[j] = max(dp[j], dp[j - w[i]] + v[i]);
      }
    }
    for (int i = W; i >= 0; i--) {
      if (dp[W] != dp[i]) {
        cout << "Case " << l << ":" << endl << dp[W] << endl << i + 1 << endl;
        break;
      }
    }
  }
  return 0;
}
Problem p00042 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <array>
#include <cstdio>
#include <iostream>
using namespace std;
array<pair<int, int>, 1001> pairs;
array<array<pair<int, int>, 10001>, 1001> m;
int main() {
  for (int caseNo = 0;; caseNo++) {
    int w;
    scanf("%d", &w);
    if (w == 0)
      break;
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
      scanf("%d,%d", &pairs[i].first, &pairs[i].second);
    }
    for (int j = 0; j <= w; j++) {
      m[0][j].first = 0;
      m[0][j].second = 0;
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j <= w; j++) {
        if (pairs[i].second <= j) {
          if (m[i - 1][j].first >
              m[i - 1][j - pairs[i].second].first + pairs[i].first) {
            m[i][j].first = m[i - 1][j].first;
            m[i][j].second = m[i - 1][j].second;
          } else {
            m[i][j].first =
                m[i - 1][j - pairs[i].second].first + pairs[i].first;
            m[i][j].second =
                m[i - 1][j - pairs[i].second].second + pairs[i].second;
          }
        } else {
          m[i][j].first = m[i - 1][j].first;
          m[i][j].second = m[i - 1][j].second;
        }
      }
    }
    printf("Case %d:\n%d\n%d\n", caseNo + 1, m[n][w].first, m[n][w].second);
  }
}

Fixed submission:
#include <array>
#include <cstdio>
#include <iostream>
using namespace std;
array<pair<int, int>, 1001> pairs;
array<array<pair<int, int>, 1001>, 1001> m;
int main() {
  for (int caseNo = 0;; caseNo++) {
    int w;
    scanf("%d", &w);
    if (w == 0)
      break;
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
      scanf("%d,%d", &pairs[i].first, &pairs[i].second);
    }
    for (int j = 0; j <= w; j++) {
      m[0][j].first = 0;
      m[0][j].second = 0;
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j <= w; j++) {
        if (pairs[i].second <= j) {
          if (m[i - 1][j].first >
              m[i - 1][j - pairs[i].second].first + pairs[i].first) {
            m[i][j].first = m[i - 1][j].first;
            m[i][j].second = m[i - 1][j].second;
          } else {
            m[i][j].first =
                m[i - 1][j - pairs[i].second].first + pairs[i].first;
            m[i][j].second =
                m[i - 1][j - pairs[i].second].second + pairs[i].second;
          }
        } else {
          m[i][j].first = m[i - 1][j].first;
          m[i][j].second = m[i - 1][j].second;
        }
      }
    }
    printf("Case %d:\n%d\n%d\n", caseNo + 1, m[n][w].first, m[n][w].second);
  }
}
Problem p00042 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <cstring>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
const int MAX_N = 1000, MAX_M = 10000;
int n;
typedef pair<int, int> P;
P ilist[MAX_N];
P memo[MAX_N + 1][MAX_M + 1];

P comp(const P &a, const P &b) {
  if (a.first < b.first)
    return b;
  return a;
}

P dp(int now, int lim) {
  if (memo[now][lim].first > -1) {
    return memo[now][lim];
  }
  P res;
  if (now == n)
    return make_pair(0, 0);
  else if (lim < ilist[now].second) {
    res = dp(now + 1, lim);
  } else {
    res = dp(now + 1, lim - ilist[now].second);
    res.second += ilist[now].second;
    res.first += ilist[now].first;
    res = comp(dp(now + 1, lim), res);
  }
  return memo[now][lim] = res;
}

int main() {
  int lim, cnt = 0, w, v;
  P item;
  char buf;
  while (cin >> lim, lim) {
    for (int i = 0; i <= MAX_N; ++i) {
      for (int j = 0; j <= MAX_M; ++j) {
        memo[i][j] = make_pair(-1, -1);
      }
    }
    ++cnt;
    cin >> n;
    for (int i = 0; i < n; ++i) {
      cin >> v >> buf >> w;
      ilist[i] = make_pair(v, w);
    }
    item = dp(0, lim);
    cout << "Case " << cnt << ":" << endl;
    cout << item.first << endl << item.second << endl;
  }
  return 0;
}

Fixed submission:
#include <cstring>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
const int MAX_N = 1000, MAX_M = 1000;
int n;
typedef pair<int, int> P;
P ilist[MAX_N];
P memo[MAX_N + 1][MAX_M + 1];

P comp(const P &a, const P &b) {
  if (a.first < b.first)
    return b;
  return a;
}

P dp(int now, int lim) {
  if (memo[now][lim].first > -1) {
    return memo[now][lim];
  }
  P res;
  if (now == n)
    return make_pair(0, 0);
  else if (lim < ilist[now].second) {
    res = dp(now + 1, lim);
  } else {
    res = dp(now + 1, lim - ilist[now].second);
    res.second += ilist[now].second;
    res.first += ilist[now].first;
    res = comp(dp(now + 1, lim), res);
  }
  return memo[now][lim] = res;
}

int main() {
  int lim, cnt = 0, w, v;
  P item;
  char buf;
  while (cin >> lim, lim) {
    for (int i = 0; i <= MAX_N; ++i) {
      for (int j = 0; j <= MAX_M; ++j) {
        memo[i][j] = make_pair(-1, -1);
      }
    }
    ++cnt;
    cin >> n;
    for (int i = 0; i < n; ++i) {
      cin >> v >> buf >> w;
      ilist[i] = make_pair(v, w);
    }
    item = dp(0, lim);
    cout << "Case " << cnt << ":" << endl;
    cout << item.first << endl << item.second << endl;
  }
  return 0;
}