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

#include <iostream>
#include <string>
using namespace std;
int main() {
  int n, i, j, k;
  string s, t;
  cin >> n;
  getline(cin, s);
  while (n--) {
    getline(cin, s);
    t = s;
    for (i = 0; i < 99; i++)
      for (j = 0; j < 26; j++) {
        for (k = 0; k < s.size(); k++)
          if (t[k] - 32)
            t[k] = 97 + (i * s[k] + j) % 26;
        if (t.find("this") + 1 || t.find("that") + 1)
          goto loop;
      }
  loop:
    cout << t << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
int main() {
  int n, i, j, k;
  string s, t;
  cin >> n;
  while (n--) {
    getline(cin, s);
    t = s;
    for (i = 0; i < 99; i++)
      for (j = 0; j < 99; j++) {
        for (k = 0; k < s.size(); k++)
          t[k] = s[k] - 32 ? 97 + (i * s[k] + j) % 26 : 32;
        if (t.find("this") + 1 || t.find("that") + 1)
          i = j = 30;
      }
    cout << t << endl;
  }
  return 0;
}
Problem p00040 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()

int main() {
  int n;
  cin >> n;
  cin.ignore();
  while (n--) {
    string s;
    getline(cin, s);
    int f = 0;
    for (int a = 0; f == 0; a++) {
      string buf(s);
      REP(b, 26) {
        REP(i, s.length()) {
          if (s[i] != ' ') {
            buf[i] = ((s[i] - 'a') * a + b) % 26 + 'a';
          }
        }
        if (buf.find("that") != -1 || buf.find("this") != -1) {
          cout << buf << endl;
          f = 1;
          break;
        }
      }
    }
  }
}

Error: TLE

Fixed submission:

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

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()

int main() {
  int n;
  cin >> n;
  while (n--) {
    string s;
    cin.ignore();
    getline(cin, s);
    int f = 0;
    for (int a = 0; f == 0; a++) {
      string buf(s);
      REP(b, 26) {
        REP(i, s.length()) {
          if (s[i] != ' ') {
            buf[i] = ((s[i] - 'a') * a + b) % 26 + 'a';
          }
        }
        if (buf.find("that") != -1 || buf.find("this") != -1) {
          cout << buf << endl;
          f = 1;
          break;
        }
      }
    }
  }
}
Problem p00040 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  int n, alpha, beta;
  string s, org;
  cin >> n;
  cin.ignore();
  for (int i = 0; i < n; ++i) {
    getline(cin, s);
    org = s;
    while (1) {
      for (alpha = 3; alpha < 26; ++alpha) {
        if (alpha) {
          if (26 % alpha == 0)
            continue;
        }
        for (beta = 0; beta <= 26; ++beta) {
          if (org.find("this", 0) != string::npos ||
              org.find("that", 0) != string::npos) {
            cout << org << endl;
            goto skip;
          } else {
            for (int k = 0; k < 26; ++k) {
              if ((s[0] - 'a') == (alpha * k + beta) % 26) {
                for (int l = 0; l < s.length(); ++l) {
                  for (int m = 0; m < 26; ++m) {
                    if (isalpha(s[l])) {
                      int t = s[l] - 'a';
                      org[l] = (alpha * t + beta) % 26 + 'a';
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  skip:;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  int n, alpha, beta;
  string s, org;
  cin >> n;
  cin.ignore();
  for (int i = 0; i < n; ++i) {
    getline(cin, s);
    org = s;
    while (1) {
      for (alpha = 3; alpha < 26; ++alpha) {
        if (alpha) {
          if (26 % alpha == 0)
            continue;
        }
        for (beta = 0; beta <= 26; ++beta) {
          for (int k = 0; k < 26; ++k) {
            if ((s[0] - 'a') == (alpha * k + beta) % 26) {
              for (int l = 0; l < s.length(); ++l) {
                for (int m = 0; m < 26; ++m) {
                  if (isalpha(s[l])) {
                    int t = s[l] - 'a';
                    org[l] = (alpha * t + beta) % 26 + 'a';
                  }
                }
              }
              if (org.find("this", 0) != string::npos ||
                  org.find("that", 0) != string::npos)
                goto result;
            }
          }
        }
      }
    }
  result:;
    cout << org << endl;
  }
  return 0;
}
Problem p00040 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
std::string decipher(std::string src, int alpha, int beta) {
  for (auto &i : src) {
    if ('a' <= i && i <= 'z') {
      i = ((i - 'a') * alpha + beta) % 26 + 'a';
    }
  }
  return src;
}
int main() {
  int n;
  std::cin >> n;
  std::cin.ignore();
  for (int i = 0; i < n; i++) {
    std::string origin;
    std::getline(std::cin, origin);
    for (int alpha = 0; alpha < 100; alpha++) {
      for (int beta = 0; beta < 26; beta++) {
        const std::string deciphered = decipher(origin, alpha, beta);
        if (deciphered.find("this") != std::string::npos ||
            deciphered.find("that") != std::string::npos) {
          std::cout << deciphered << std::endl;
          alpha = beta = 100;
        }
      }
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <sstream>
#include <string>
int calc_alpha(const std::string &w, const char *str) {
  auto distance = [](char from, char to) {
    int result = static_cast<int>(to - from);
    return result + (result < 0 ? 26 : 0);
  };
  int dis1[3], ratio[3];
  for (int j = 0; j < 3; j++) {
    dis1[j] = distance(w[j], w[j + 1]);
    int dis2 = distance(str[j], str[j + 1]);
    while (true) {
      if (dis1[j] % dis2 == 0) {
        ratio[j] = dis1[j] / dis2;
        break;
      }
      dis1[j] += 26;
    }
  }
  return ratio[0] == ratio[1] && ratio[1] == ratio[2] ? ratio[0] : -1;
}
int main() {
  int n;
  std::cin >> n;
  std::cin.ignore();
  for (int i = 0; i < n; i++) {
    std::string str;
    std::getline(std::cin, str);
    std::stringstream ss(str);
    int alpha, beta;
    for (std::string w; ss >> w;) {
      if (w.length() == 4) {
        alpha = w[0] == w[3] ? calc_alpha(w, "that") : calc_alpha(w, "this");
        if (alpha != -1) {
          beta = static_cast<int>(w[0] - 'a' - ('t' - 'a') * alpha % 26);
          break;
        }
      }
    }
    char deciphered[26];
    for (char j = 'a'; j <= 'z'; j++) {
      char ciphered = ((j - 'a') * alpha + beta) % 26 + 'a';
      deciphered[ciphered - 'a'] = j;
    }
    for (auto j : str) {
      std::cout << (j == ' ' ? ' ' : deciphered[j - 'a']);
    }
    std::cout << std::endl;
  }
  return 0;
}
Problem p00040 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int trans[26];

void makelist(int a, int b) {
  for (int i = 0; i < 26; i++) {
    trans[i] = (a * i + b) % 26;
  }
}

string translate(string s) {
  for (int i = 0; i < s.size(); i++) {
    if (s[i] >= 'a' && s[i] <= 'z') {
      s[i] = trans[s[i] - 'a'] + 'a';
    }
  }
  return s;
}

int main() {
  // a!=2,13 **
  int n;
  cin >> n;
  cin.ignore();
  for (int count = 0; count < n; count++) {
    string s;
    getline(cin, s);
    string tmp = s;
    int a = 0, b = 100;
    while (s.find("that") == string::npos && s.find("this") == string::npos) {
      b++;
      if (b >= 26) {
        b = 0;
        a++;
      }

      while (a % 2 == 0 || a % 13 == 0)
        a++;
      makelist(a, b);
      s = translate(tmp);
      // cout<<s<<endl;
    }
    cout << s << endl;
  }
}

Error: TLE

Fixed submission:

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

int trans[26];

void makelist(int a, int b) {
  for (int i = 0; i < 26; i++) {
    trans[i] = (a * i + b) % 26;
  }
}

string translate(string s) {
  for (int i = 0; i < s.size(); i++) {
    if (s[i] >= 'a' && s[i] <= 'z') {
      s[i] = trans[s[i] - 'a'] + 'a';
    }
  }
  return s;
}

int main() {
  // a!=2,13 **
  int n;
  cin >> n;
  for (int count = 0; count < n; count++) {
    string s;
    cin.ignore();
    getline(cin, s);
    string tmp = s;
    int a = 0, b = 100;
    while (s.find("that") == string::npos && s.find("this") == string::npos) {
      b++;
      if (b >= 26) {
        b = 0;
        a++;
      }

      while (a % 2 == 0 || a % 13 == 0)
        a++;
      makelist(a, b);
      s = translate(tmp);
      // cout<<s<<endl;
    }
    cout << s << endl;
  }
}
Problem p00041 (C++) — buggy submission, verdict: 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");
  }
}

Error: TLE

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, verdict: Runtime Error

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

#define MAX 1001

int dp[MAX][MAX];
int v[MAX], w[MAX];

int main() {
  int W, n, u = 0;
  char t;
  while (cin >> W) {
    if (W == 0)
      break;
    cin >> n;
    u++;

    for (int i = 0; i < n; i++) {
      cin >> v[i] >> t >> w[i];
    }

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

    int mv, mw;
    mv = mw = 0;
    for (int i = 0; i <= W; i++) {
      if (dp[n][i] > mv) {
        mv = dp[n][i];
        mw = i;
      }
    }

    cout << "Case " << u << ":" << endl;
    cout << mv << endl;
    cout << mw << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

#define MAX 1001

int dp[MAX][MAX];
int v[MAX], w[MAX];

int main() {
  int W, n, u = 0;
  char t;
  while (cin >> W) {
    if (W == 0)
      break;
    cin >> n;
    u++;

    dp[MAX][MAX] = {0};
    v[MAX] = {0};
    w[MAX] = {0};
    for (int i = 0; i < n; i++) {
      cin >> v[i] >> t >> w[i];
    }

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

    int mv = 0, mw;
    for (int i = 0; i <= W; i++) {
      if (dp[n][i] > mv) {
        mv = dp[n][i];
        mw = i;
      }
    }

    cout << "Case " << u << ":" << endl;
    cout << mv << endl;
    cout << mw << endl;
  }
  return 0;
}
Problem p00042 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>

int max(int a, int b) {
  if (a >= b)
    return a;
  else
    return b;
}

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

void swap(int *a, int *b) {
  int tmp = *a;
  *a = *b;
  *b = tmp;
}

void babbleSort(int *data, int n) {
  for (int i = 0; i < n; i++) {
    for (int j = n - 1; j > i; j--) {
      if (data[j] >= data[j - 1]) {
        swap(&data[j], &data[j - 1]);
      }
    }
  }
}

using namespace std;

/*
cin.ignore();
        getline(cin,dataStr);
        data = new int[n];
        stringToInteger(&dataStr,data,n);
*/

void stringToInteger(string *str, int *data, int n) {
  for (int i = 1; i <= n; i++) {
    int spaceN = str->find(' ', 0);

    data[i] = atoi(str->substr(0, spaceN).c_str());
    str->erase(0, spaceN + 1);
  }
}

int dp[1010];
// dp[i] i:重さ

int main() {

  int w, n;
  int cnt = 1;
  while (1) {
    cin >> w;
    if (w == 0)
      break;

    cin >> n;

    for (int i = 0; i <= w; i++) {
      dp[i] = 0;
    }
    int value, weight;
    for (int i = 0; i < n; i++) {
      scanf("%d,%d", &value, &weight);
      for (int j = w; j >= 0; j--) {
        if ((j == 0 || dp[j] > 0) && ((j + weight) <= w)) {
          int tmp = dp[j] + value;
          dp[j + weight] = max(tmp, dp[j + weight]);
          //	cout<<j+weight<<" "<<dp[j+weight]<<endl;
        }
      }
    }

    int ans = 0;
    int ansCnt = 0;
    for (int i = 0; i <= w; i++) {
      if (ans < dp[i]) {
        ans = dp[i];
        ansCnt = i;
      }
    }
    cout << "Case " << cnt << ":" << endl;
    cout << ans << endl;
    cout << ansCnt << endl;
    cnt++;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <math.h>
#include <stdio.h>

using namespace std;

int w;
int num;
int caseN;

int *dataV, *dataW;
int maxV, maxW;

void calc(int n, int sumV, int sumW) {
  if (sumW > w) {
    return;
  }
  if (n == num) {
    if (maxV <= sumV) {
      maxV = sumV;
      maxW = sumW;
    }
    return;
  } else {
    calc(n + 1, sumV, sumW);
    calc(n + 1, sumV + dataV[n], sumW + dataW[n]);
  }
}

int main() {

  char tmp;
  caseN = 0;

  while (cin >> w) {
    if (w == 0) {
      break;
    }
    cin >> num;
    dataV = new int(num);
    dataW = new int(num);
    maxV = 0;
    maxW = 0;

    for (int i = 0; i < num; i++) {
      cin >> dataV[i];
      cin >> tmp;
      cin >> dataW[i];
    }
    calc(0, 0, 0);

    cout << "Case " << caseN + 1 << ":" << endl;
    cout << maxV << endl;
    cout << maxW << endl;

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string.h>
#include <vector>

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

#define pb push_back

using namespace std;

int dp[1010][10010];

int main() {
  int w_max, count = 0;
  while (cin >> w_max, w_max) {
    count++;
    int n;
    cin >> n;

    memset(dp, 0, sizeof(dp));

    int v, w;
    pair<int, int> w_sum;
    w_sum = make_pair(0, 0);

    ascent(i, 1, n + 1) {
      scanf("%d,%d", &v, &w);

      ascent(j, 1, w_max + 1) {
        if (w <= j) {
          if (dp[i - 1][j] < dp[i - 1][j - w] + v) {
            dp[i][j] = dp[i - 1][j - w] + v;

            if (w_sum.first < dp[i][j] ||
                w_sum.first == dp[i][j] && j < w_sum.second) {
              w_sum = make_pair(dp[i][j], j);

              // cout << "i = " << i << " " << "wsum = " << w_sum.second <<
              // endl;
            }
          } else {
            dp[i][j] = dp[i - 1][j];
          }
        } else {
          dp[i][j] = dp[i - 1][j];
        }
      }
    }
    /*
    ascent(i, 0, n+1) {
      ascent(j, 0, w_max+1) {
        printf("%3d ", dp[i][j]);
      }
      puts("");
    }
    */
    cout << "Case " << count << ":" << endl;
    cout << dp[n][w_max] << endl;
    cout << w_sum.second << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string.h>
#include <vector>

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

#define pb push_back

using namespace std;

int dp[2000][20000];

int main() {
  int w_max, count = 0;
  while (cin >> w_max, w_max) {
    count++;
    int n;
    cin >> n;

    memset(dp, 0, sizeof(dp));

    int v, w;
    pair<int, int> w_sum;
    w_sum = make_pair(0, 0);

    ascent(i, 1, n + 1) {
      scanf("%d,%d", &v, &w);

      ascent(j, 1, w_max + 1) {
        if (w <= j) {
          if (dp[i - 1][j] < dp[i - 1][j - w] + v) {
            dp[i][j] = dp[i - 1][j - w] + v;

            if (w_sum.first < dp[i][j] ||
                w_sum.first == dp[i][j] && j < w_sum.second) {
              w_sum = make_pair(dp[i][j], j);

              // cout << "i = " << i << " " << "wsum = " << w_sum.second <<
              // endl;
            }
          } else {
            dp[i][j] = dp[i - 1][j];
          }
        } else {
          dp[i][j] = dp[i - 1][j];
        }
      }
    }
    /*
    ascent(i, 0, n+1) {
      ascent(j, 0, w_max+1) {
        printf("%3d ", dp[i][j]);
      }
      puts("");
    }
    */
    cout << "Case " << count << ":" << endl;
    cout << dp[n][w_max] << endl;
    cout << w_sum.second << endl;
  }
  return 0;
}
Problem p00042 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
using namespace std;
typedef pair<int, int> P;

int main() {
  int W, N, cou = 0;
  int tr[1001], we[1001];
  while (cin >> W, W) {
    cou++;
    cin >> N;
    for (int i = 0; i < N; i++) {
      scanf("%d,%d", &tr[i], &we[i]);
    }

    int dp[1010];
    for (int i = 0; i <= W; i++) {
      dp[i] = 0;
    }
    for (int i = 0; i < N; i++) {
      for (int j = W; j >= we[i]; j--) {
        dp[j] = max(dp[j], dp[j - we[i]] + tr[i]);
      }
    }
    int ret = 0;
    for (int i = 0; i <= W; i++) {
      if (dp[i] > dp[ret])
        ret = i;
    }
    cout << "Case " << cou << ":" << endl;
    cout << dp[ret] << endl;
    cout << ret << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
using namespace std;
typedef pair<int, int> P;

int main() {
  int W, N, cou = 0;
  int tr[1001], we[1001];
  while (cin >> W, W) {
    cou++;
    cin >> N;
    for (int i = 1; i <= N; i++) {
      scanf("%d,%d", &tr[i], &we[i]);
    }

    P dp[100][1001];
    for (int i = 0; i <= N; i++) {
      for (int j = 0; j <= N; j++) {
        dp[i][j] = P(0, 0);
      }
    }
    dp[0][0] = P(0, 0);
    for (int i = 1; i <= N; i++) {
      dp[i][0] = P(0, 0);
      for (int j = 0; j < i; j++) {
        if (dp[i - 1][j].second + we[i] <= W) {
          P p;
          p = P(dp[i - 1][j].first + tr[i], dp[i - 1][j].second + we[i]);
          if (p.first == dp[i - 1][j].first) {
            if (p.second < dp[i - 1][j].second)
              dp[i][j + 1] = p;
            else
              dp[i][j + 1] = dp[i - 1][j];
          } else if (p.first > dp[i - 1][j].first) {
            dp[i][j + 1] = p;
          } else
            dp[i][j + 1] = dp[i - 1][j];
        } else
          dp[i][j + 1] = dp[i - 1][j];
      }
    }
    int ans1 = 0, ans2 = 0;
    for (int i = 0; i <= N; i++) {
      if (dp[N][i].first == ans1) {
        if (dp[N][i].second < ans2)
          ans2 = dp[N][i].second;
      } else if (dp[N][i].first > ans1) {
        ans1 = dp[N][i].first;
        ans2 = dp[N][i].second;
      }
    }

    cout << "Case " << cou << ":" << endl;
    cout << ans1 << endl;
    cout << ans2 << endl;
  }
}
Problem p00042 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
/* 変数名を被らせない
   デバッグコードを取り除く */
#define ANSWER(x) cout << x << endl
#define debug(x) cout << #x << ": " << x
const int MAX_INT = 1000000;
int n, w, ans, cnt = 1;
int value[1001], weight[1001], dp[1001][1001];
;
int solve(int a, int b);
int main() {
  while (1) {
    memset(dp, -1, sizeof(dp));
    cin >> w;
    if (w == 0)
      break;
    cin >> n;
    for (int i = 0; i < n; i++) {
      scanf("%d,%d", &value[i], &weight[i]);
    }
    ans = solve(0, w);
    for (int i = 0; i <= w; i++) {
      if (solve(0, i) == ans) {
        cout << "Case " << cnt << ':' << endl << ans << endl << i << endl;
        break;
      }
    }
    cnt++;
  }
}
int solve(int a, int b) {
  if (dp[a][b] != -1)
    return dp[a][b];
  if (a == n)
    return 0;

  else if (b < weight[a])
    return dp[a][b] = solve(a + 1, b);
  else
    return dp[a][b] =
               max(solve(a + 1, b - weight[a]) + value[a], solve(a + 1, b));
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
/* 変数名を被らせない
   デバッグコードを取り除く */
#define ANSWER(x) cout << x << endl
#define debug(x) cout << #x << ": " << x
const int MAX_INT = 1000000;
int n, w, ans, cnt = 1;
int value[1001], weight[1001];
int solve(int a, int b);
int main() {
  while (1) {
    cin >> w;
    if (w == 0)
      break;
    cin >> n;
    for (int i = 0; i < n; i++) {
      scanf("%d,%d", &value[i], &weight[i]);
    }
    ans = solve(0, w);
    for (int i = 0; i <= w; i++) {
      if (solve(0, i) == ans) {
        cout << "Case " << cnt << ':' << endl << ans << endl << i << endl;
        break;
      }
    }
    cnt++;
  }
}
int solve(int a, int b) {
  if (a == n)
    return 0;

  else if (b < weight[a])
    return solve(a + 1, b);
  else
    return max(solve(a + 1, b - weight[a]) + value[a], solve(a + 1, b));
}