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

#include <cstring>
#include <iostream>

using namespace std;

const int MAX_w = 1000;
const int MAX_v = 1000;

int dp[MAX_w + 1][MAX_v + 1];

int n, wet;
int w[MAX_w], v[MAX_v];

// i番目以降の品物から重さの和がj以下なるように選んだときの、
// 取りうる価値の総和の最大値を返す関数
int rec(int i, int j) {
  int res;
  if (dp[i][j] != -1) {

    return dp[i][j];
  }

  if (i == n) {
    // 品物がもう残っていないときは、価値の和の最大値は0で確定
    res = 0;
  } else if (j < w[i]) {
    // 残りの容量が足りず品物iを入れられないので、入れないパターンだけ処理
    // i+1 以降の品物のみを使ったときの最大値をそのままこの場合の最大値にする
    res = rec(i + 1, j);
  } else {
    // 品物iを入れるか入れないか選べるので、両方試して価値の和が大きい方を選ぶ
    res = max(rec(i + 1, j), rec(i + 1, j - w[i]) + v[i]);
  }

  return dp[i][j] = res;
}

void solve(int ca) {
  // 0番目以降で容量W以下の場合の結果を表示する
  int wmin;

  memset(dp, -1, sizeof(dp));

  cout << "Case"
       << " " << ca << ":" << endl;

  int ans = rec(0, wet);
  cout << ans << endl;

  for (int w = wet - 1; w >= 0; w--) {
    if (ans != rec(0, w)) {
      wmin = w + 1;
      break;
    }
  }

  cout << wmin << endl;
}

int main() {

  char x;
  int ca = 0;

  while (1) {

    ca++;
    cin >> wet;
    if (wet == 0)
      break;
    cin >> n;

    for (int i = 0; i < n; i++) {

      cin >> v[i] >> x >> w[i];
    }

    solve(ca);
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <cstring>
#include <iostream>

using namespace std;

const int MAX_w = 1000;
const int MAX_v = 10000;

int dp[MAX_w + 1][MAX_v + 1];

int n, wet;
int w[MAX_w], v[MAX_v];

// i番目以降の品物から重さの和がj以下なるように選んだときの、
// 取りうる価値の総和の最大値を返す関数
int rec(int i, int j) {
  int res;
  if (dp[i][j] != -1) {

    return dp[i][j];
  }

  if (i == n) {
    // 品物がもう残っていないときは、価値の和の最大値は0で確定
    res = 0;
  } else if (j < w[i]) {
    // 残りの容量が足りず品物iを入れられないので、入れないパターンだけ処理
    // i+1 以降の品物のみを使ったときの最大値をそのままこの場合の最大値にする
    res = rec(i + 1, j);
  } else {
    // 品物iを入れるか入れないか選べるので、両方試して価値の和が大きい方を選ぶ
    res = max(rec(i + 1, j), rec(i + 1, j - w[i]) + v[i]);
  }

  return dp[i][j] = res;
}

void solve(int ca) {
  // 0番目以降で容量W以下の場合の結果を表示する
  int wmin;

  memset(dp, -1, sizeof(dp));

  cout << "Case"
       << " " << ca << ":" << endl;

  int ans = rec(0, wet);
  cout << ans << endl;

  for (int w = wet - 1; w >= 0; w--) {
    if (ans != rec(0, w)) {
      wmin = w + 1;
      break;
    }
  }

  cout << wmin << endl;
}

int main() {

  char x;
  int ca = 0;

  while (1) {

    ca++;
    cin >> wet;
    if (wet == 0)
      break;
    cin >> n;

    for (int i = 0; i < n; i++) {

      cin >> v[i] >> x >> w[i];
    }

    solve(ca);
  }

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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX(x, y) ((x) > (y) ? (x) : (y))

int dp[1002][1002];

int main(void) {
  int w;
  int n;
  int cnt;
  int max;

  cnt = 1;
  while (1) {
    int value[1000];
    int weight[1000];

    scanf("%d", &w);

    if (w == 0) {
      break;
    }

    scanf("%d", &n);

    memset(dp, -1, sizeof(dp));

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

    for (int i = 1; i <= n; i++) {
      for (int j = 0; j <= w; j++) {
        if (dp[i - 1][j] != -1 && j + weight[i - 1] <= w) {
          dp[i][j + weight[i - 1]] =
              MAX(dp[i - 1][j] + value[i - 1], dp[i][j + weight[i - 1]]);
        }
        if (dp[i - 1][j] != -1) {
          dp[i][j] = MAX(dp[i - 1][j], dp[i][j]);
        }
      }
    }

    max = 0;
    for (int i = 1; i <= w; i++) {
      if (dp[n][i] > dp[n][max]) {
        max = i;
      }
    }

    printf("Case %d:\\n%d\\n%d\\n", cnt++, dp[n][max], max);
  }

  return (0);
}

Error: TLE

Fixed submission:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int dp[1001][1001];

int main(void) {
  int w;
  int n;
  int cnt;
  int max;

  cnt = 1;
  while (1) {
    int value[1000];
    int weight[1000];

    scanf("%d", &w);
    scanf("%d", &n);

    memset(dp, -1, sizeof(dp));

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

    for (int i = 1; i <= n; i++) {
      for (int j = 0; j <= w; j++) {
        if (dp[i - 1][j] != -1 && j + weight[i - 1] <= w) {
          dp[i][j + weight[i - 1]] = dp[i - 1][j] + value[i - 1];
        }
        if (dp[i - 1][j] != -1) {
          dp[i][j] = dp[i - 1][j];
        }
      }
    }

    for (int i = 0; i <= w; i++) {
      if (dp[n][i] > dp[n][max]) {
        max = i;
      }
    }

    printf("Case:%2d\\n%d\\n%d\\n", cnt++, dp[n][max], max);
  }

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

#include <algorithm>
#include <cstdio>
#include <cstring>

using namespace std;

int dp[1001][1001];

int main() {
  int W, N, v[1024], w[1024], c = 0, res = 0;
  while (scanf("%d", &W) && W) {
    c++;
    scanf("%d", &N);
    for (int i = 0; i < N; ++i)
      scanf("%d,%d", v + i, w + i);
    res = 0;
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i < N; ++i) {
      for (int j = 0; j <= W; j++) {
        if (j < w[i])
          dp[i + 1][j] = dp[i][j];
        else
          dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]);
      }
    }
    for (int j = 0; j < W; ++j)
      if (dp[N][j] < dp[N][W])
        res++;
    printf("Case %d:\\n%d\\n%d\\n", c, dp[N][W], res);
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>

using namespace std;

int dp[10000][10000];

int main() {
  int W, N, v[1024], w[1024], c = 0, res = 0;
  while (scanf("%d", &W) && W) {
    c++;
    scanf("%d", &N);
    for (int i = 0; i < N; ++i)
      scanf("%d,%d", v + i, w + i);
    res = 0;
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i < N; ++i) {
      for (int j = 0; j <= W; j++) {
        if (j < w[i])
          dp[i + 1][j] = dp[i][j];
        else
          dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]);
      }
    }
    for (int j = 0; j < W; ++j)
      if (dp[N][j] < dp[N][W])
        res++;
    printf("Case %d:\\n%d\\n%d\\n", c, dp[N][W], res);
  }
  return 0;
}
Problem p00042 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

const int MAX_N = 1002;
const int MAX_W = 10001;
const int MAX_V = 10001;

int dp[MAX_N][MAX_W];
int w[MAX_N];
int v[MAX_N];

int N, W;

int main() {
  int c = 1;
  while (cin >> W, W) {
    cin >> N;
    for (int i = 0; i < N; i++) {
      scanf("%d,%d", &v[i], &w[i]);
    }
    memset(dp, 0, sizeof(dp));

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

    int weight = 1000000;
    for (int i = 0; i <= N; i++) {
      for (int j = 0; j <= W; j++) {
        if (dp[i][j] == dp[N][W]) {
          weight = min(weight, j);
        }
      }
    }
  out:;
    printf("Case %d:\\n%d\\n%d\\n", c++, dp[N][W], weight);
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

const int MAX_N = 10001;
const int MAX_W = 10001;
const int MAX_V = 100001;

int dp[MAX_N][MAX_W];
int w[MAX_N];
int v[MAX_N];

int N, W;

int main() {
  int c = 1;
  while (cin >> W, W) {
    cin >> N;
    for (int i = 0; i < N; i++) {
      scanf("%d,%d", &v[i], &w[i]);
    }
    memset(dp, 0, sizeof(dp));

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

    int weight = 1000000;
    for (int i = 0; i <= N; i++) {
      for (int j = 0; j <= W; j++) {
        if (dp[i][j] == dp[N][W]) {
          weight = min(weight, j);
        }
      }
    }
  out:;
    printf("Case %d:\\n%d\\n%d\\n", c++, dp[N][W], weight);
  }

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define INF 100000000
#define rep(i, a) for (int i = 0; i < (a); i++)

using namespace std;
typedef long long ll;

int dp[1001][10001]; // dp[i][j]  i番目以前の物で、重さがj以下の時の価値
int W[1001], V[1001];

int w, n;

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

  cout << dp[0][w] << endl;
  for (int i = w; i >= 1; i--) {
    if (dp[0][i] != dp[0][i - 1]) {
      cout << i << endl;
      break;
    }
  }
}

int main() {
  int count = 1;
  while (1) {
    memset(dp, 0, sizeof(dp));

    cin >> w >> n;
    if (w == 0)
      break;
    char a;
    rep(i, n) cin >> V[i] >> a >> W[i];

    cout << "Case " << count << ":" << endl;
    count++;
    solve();
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define INF 100000000
#define rep(i, a) for (int i = 0; i < (a); i++)

using namespace std;
typedef long long ll;

int dp[2000][20000]; // dp[i][j]  i番目以前の物で、重さがj以下の時の価値
int W[2000], V[2000];

int w, n;

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

  cout << dp[0][w] << endl;
  for (int i = w; i >= 1; i--) {
    if (dp[0][i] != dp[0][i - 1]) {
      cout << i << endl;
      break;
    }
  }
}

int main() {
  int count = 1;
  while (1) {
    memset(dp, 0, sizeof(dp));

    cin >> w >> n;
    if (w == 0)
      break;
    char a;
    rep(i, n) cin >> V[i] >> a >> W[i];

    cout << "Case " << count << ":" << endl;
    count++;
    solve();
  }
  return 0;
}
Problem p00042 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#define MAX_N 1000
#define MAX_W 1000
using namespace std;
int W;
int N;

int w[MAX_N];
int v[MAX_N];
int dp[MAX_N + 1][MAX_W + 1];
bool input() {
  scanf("%d", &W);
  if (!W)
    return false;
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    scanf("%d,%d", &v[i], &w[i]);
  }
  return true;
}
void solve() {

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

int main() {

  int cnt = 0;

  while (input()) {
    solve();
    int i;
    for (i = W; i >= 0; i--) {
      if (dp[N][W] != dp[N][i])
        break;
    }
    printf("Case %d:\\n", ++cnt);
    printf("%d\\n", dp[N][W]);
    printf("%d\\n", i + 1);
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int dp[1000][10000];
int main() {
  int count = 1, i, j, W, w, n;
  while (1) {
    memset(dp, 0, sizeof(dp));
    cin >> W;
    if (W == 0)
      break;
    cin >> n;
    int v[n], w[n];
    for (i = 0; i < n; i++) {
      scanf("%d,%d", &v[i], &w[i]);
    }
    for (i = 0; i < n; i++) {
      for (j = 0; j <= W; j++) {
        if (w[i] > j)
          dp[i + 1][j] = dp[i][j];
        else
          dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]);
      }
    }
    for (i = W; i >= 0; i--) {
      if (dp[n][W] > dp[n][i])
        break;
    }
    cout << "Case " << count << ':' << endl << dp[n][W] << endl;
    cout << i + 1 << endl;
    count++;
  }
  return 0;
}
Problem p00042 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define MAX_N 1000
#define MAX_W 1000

int main(void) {
  int W, N, cnt = 0;
  while (cin >> W && W && cin >> N) {
    cnt++;
    int dp[MAX_W + 1][MAX_N + 1] = {0};
    for (int i = 0; i < N; i++) {
      int v, w;
      scanf("%d,%d", &v, &w);
      for (int j = 0; j < W + 1; j++) {
        dp[j][i + 1] = max(dp[j][i], (j - w >= 0 ? dp[j - w][i] + v : 0));
      }
    }
    cout << "Case " << cnt << ":" << endl;
    for (int j = 0; j < W + 1; j++) {
      if (dp[j][N] == dp[W][N]) {
        cout << dp[j][N] << endl << j << endl;
        break;
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define MAX_N 1000
#define MAX_W 1000

int N, W;
int v[MAX_N], w[MAX_N];
int memo[MAX_N][MAX_W];
int resW;

int solve(int index, int weight) {
  if (memo[index][weight] > 0)
    return memo[index][weight];

  int res;
  if (index == N)
    res = 0;
  else if (weight < w[index])
    res = solve(index + 1, weight);
  else {
    res = max(solve(index + 1, weight),
              solve(index + 1, weight - w[index]) + v[index]);
  }
  return memo[index][weight] = res;
}

void askw(int ans, int weight, int index) {
  if (ans == 0) {
    if (weight < resW)
      resW = weight;
  }
  if (index == N)
    return;
  if (ans - v[index] >= 0) {
    askw(ans - v[index], weight + w[index], index + 1);
    askw(ans, weight, index + 1);
  }
}

int main(void) {
  int cnt = 0, ans;
  while (cin >> W, W) {
    cnt++;
    cin >> N;
    for (int i = 0; i < N; i++) {
      scanf("%d,%d", &v[i], &w[i]);
    }
    resW = 1e9;
    memset(memo, -1, sizeof(memo));
    ans = solve(0, W);
    askw(ans, 0, 0);
    cout << "Case " << cnt << ":" << endl;
    cout << ans << endl;
    cout << resW << endl;
  }
  return 0;
}
Problem p00042 (C++) — buggy submission, verdict: Time Limit Exceeded

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

bool cmp(const pair<int, int> &a, const pair<int, int> &b) {
  if (a.first != b.first)
    return a.first < b.first;
  else
    return a.second > b.second;
}
int main() {
  int n, W;
  int t = 1;
  while (cin >> W >> n && W) {
    vector<pair<int, int>> cur;
    cur.push_back({0, 0});
    for (int i = 0; i < n; i++) {
      int w, c;
      scanf("%d,%d", &c, &w);
      for (int j = 0, t = cur.size(); j < t; j++)
        cur.push_back(pair<int, int>(cur[j].first + w, cur[j].second + c));
      sort(cur.begin(), cur.end(), cmp);
      int maxi = -1;
      vector<pair<int, int>> nex;
      for (int j = 0; j < cur.size(); j++) {
        if (maxi >= cur[j].second)
          continue;

        if (cur[j].first <= W)
          nex.push_back(cur[j]);
        maxi = cur[j].second;
      }
      cur = nex;
    }
    int ans = 0, ans2;
    for (int i = 0; i < cur.size(); i++) {
      if (cur[i].first <= W) {
        ans = cur[i].second;
        ans2 = cur[i].first;
      }
    }

    cout << "Case " << t++ << ":" << endl;
    cout << ans << endl;
    cout << ans2 << endl;
  }
}

Error: TLE

Fixed submission:

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

bool cmp(const pair<int, int> &a, const pair<int, int> &b) {
  if (a.first != b.first)
    return a.first < b.first;
  else
    return a.second > b.second;
}
int main() {
  int n, W;
  int t = 1;
  while (cin >> W >> n && W) {
    vector<pair<int, int>> cur;
    cur.push_back({0, 0});
    for (int i = 0; i < n; i++) {
      int w, c;
      scanf("%d,%d", &c, &w);
      for (int j = 0, t = cur.size(); j < t; j++)
        cur.push_back(pair<int, int>(cur[j].first + w, cur[j].second + c));
      sort(cur.begin(), cur.end(), cmp);
      int maxi = -1;
      vector<pair<int, int>> nex;
      for (int j = 0; j < cur.size(); j++) {
        if (maxi >= cur[j].second)
          continue;
        nex.push_back(cur[j]);
        maxi = cur[j].second;
      }
      cur = nex;
    }
    int ans = 0, ans2;
    for (int i = 0; i < cur.size(); i++) {
      if (cur[i].first <= W) {
        ans = cur[i].second;
        ans2 = cur[i].first;
      }
    }

    cout << "Case " << t++ << ":" << endl;
    cout << ans << endl;
    cout << ans2 << endl;
  }
}
Problem p00042 (C++) — buggy submission, verdict: Runtime Error

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

int dp[1005][1005];
int W;
int n;
int v[1005], w[1005];
int now = 1;

int main() {
  while (1) {
    scanf("%d", &W);
    if (!W)
      break;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
      scanf("%d,%d", &v[i], &w[i]);
    }

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

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

    int res = W;
    for (int i = W; i >= 0; i--) {
      if (dp[n][i] == dp[n][W])
        res = std::min(res, i);
      else
        break;
    }

    printf("Case %d:\\n", now);
    printf("%d\\n", dp[n][W]);
    printf("%d\\n", res);

    now++;
  }

  return 0;
}

Error: 0

Fixed submission:

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

int dp[1000][1001];
int W;
int n;
int v[1000], w[1000];
int now = 1;

int main() {
  while (1) {
    scanf("%d", &W);
    if (!W)
      break;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
      scanf("%d,%d", &v[i], &w[i]);
    }

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

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

    int res = W;
    for (int i = W; i >= 0; i--) {
      if (dp[n][i] == dp[n][W])
        res = std::min(res, i);
      else
        break;
    }

    printf("Case %d:\\n", now);
    printf("%d\\n", dp[n][W]);
    printf("%d\\n", res);

    now++;
  }

  return 0;
}
Problem p00042 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <deque>
using namespace std;
typedef pair<int, int> P;

int W, N, v[1000], w[1000];
int I;

P dp[1001][1001];
bool done[1001][1001];
P solve(int i, int r) {
  if (done[i][r])
    return dp[i][r];
  P result;
  if (i == N)
    result = P(0, r);
  else {
    if (r - w[i] >= 0) {
      P p = solve(i + 1, r - w[i]);
      p.first += v[i];
      result = max(p, solve(i + 1, r));
    } else {
      result = solve(i + 1, r);
    }
  }
  done[i][r] = true;
  return dp[i][r] = result;
}

int main() {
  while (I++, scanf("%d%d", &W, &N), W) {
    for (int i = 0; i < N; i++)
      scanf("%d,%d", v + i, w + i);
    memset(done, 0, sizeof(done));
    P p = solve(0, W);
    printf("Case %d:\\n%d\\n%d\\n", I, p.first, W - p.second);
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <deque>
using namespace std;
typedef pair<int, int> P;

int W, N, v[1000], w[1000];
int I;

P dp[1000][1000];
bool done[1000][1000];
P solve(int i, int r) {
  if (done[i][r])
    return dp[i][r];
  P result;
  if (i == N)
    result = P(0, r);
  else {
    if (r - w[i] >= 0) {
      P p = solve(i + 1, r - w[i]);
      p.first += v[i];
      result = max(p, solve(i + 1, r));
    } else {
      result = solve(i + 1, r);
    }
  }
  done[i][r] = true;
  return dp[i][r] = result;
}

int main() {
  while (I++, scanf("%d%d", &W, &N), W) {
    for (int i = 0; i < N; i++)
      scanf("%d,%d", v + i, w + i);
    memset(done, 0, sizeof(done));
    P p = solve(0, W);
    printf("Case %d:\\n%d\\n%d\\n", I, p.first, W - p.second);
  }
}
Problem p00042 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define rep(i, n) for (int i = 0; i < n; i++)
int dp[1001][1001];

int main() {
  int W, cnt = 1;
  while (cin >> W, W) {
    int n, v[1000], w[1000];
    memset(dp, -1, sizeof(dp));
    cin >> n;
    rep(i, n) { scanf("%d,%d", &v[i], &w[i]); }
    dp[0][0] = 0;
    rep(i, n) {
      rep(j, W + 1) {
        if (j < w[i])
          dp[i + 1][j] = dp[i][j];
        else
          dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]);
      }
    }
    int mw = 0;
    rep(i, W + 1) {
      if (dp[n][i] > dp[n][mw])
        mw = i;
    }
    cout << "Case " << cnt << ":" << endl;
    cnt++;
    cout << dp[n][mw] << endl;
    cout << mw << endl;
  }
}

Error: TLE

Fixed submission:

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

struct treasure_t {
  int value;
  int weight;
  treasure_t(const int v, const int w) {
    value = v;
    weight = w;
  }
};

vector<treasure_t> treasures;
int W, maxW, maxV;

void solve(int n, int w, int v) {
  if (w > W)
    return;
  if (n == treasures.size()) {
    if ((v > maxV) || (v == maxV && w < maxW)) {
      maxV = v;
      maxW = w;
    }
    return;
  }
  solve(n + 1, w + treasures[n].weight, v + treasures[n].value);
  solve(n + 1, w, v);
}

int main() {
  int cnt = 1;
  while (cin >> W, W) {
    int n;
    cin >> n;
    treasures.clear();
    maxW = 0;
    maxV = 0;
    for (int i = 0; i < n; i++) {
      int v, w;
      scanf("%d,%d", &v, &w);
      treasure_t t(v, w);
      treasures.push_back(t);
    }
    solve(0, 0, 0);
    cout << "Case " << cnt << ":" << endl;
    cout << maxV << endl;
    cout << maxW << endl;
    cnt++;
  }
  return 0;
}
Problem p00042 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  const int nmax = 1000;
  char c;
  int count = 1;
  int dp[nmax + 1];
  int n, w;
  int val[nmax + 1], weight[nmax + 1];
  while (cin >> w) {
    if (w == 0)
      break;
    cin >> n;
    for (int i = 1; i <= n; i++) {
      cin >> val[i] >> c >> weight[i];
    }
    for (int i = 0; i <= w; i++)
      dp[i] = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = w; j >= weight[i]; j--) {
        dp[j] = max(dp[j], dp[j - weight[i]] + val[i]);
      }
    }
    cout << "Case " << count++ << ":" << endl;
    cout << dp[w] << endl;
    while (w > 0 && dp[w] == dp[w - 1])
      w--;
    cout << w << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

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

int main() {
  const int nmax = 100;
  char c;
  int dp[nmax + 1][nmax + 1];
  int n, w;
  int val[nmax + 1], weight[nmax + 1];
  while (cin >> w) {
    if (w == 0)
      break;
    cin >> n;
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= w; j++) {
        dp[i][j] = 0;
      }
    }
    for (int i = 1; i <= n; i++) {
      cin >> val[i] >> c >> weight[i];
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j <= w; j++) {
        if (j < weight[i]) {
          dp[i + 1][j] = dp[i][j];
        } else {
          dp[i + 1][j] = max(dp[i][j], dp[i][j - weight[i]] + val[i]);
        }
      }
    }
    cout << dp[n][w] << endl;
  }
  return 0;
}