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

#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;
}

Error: MLE

Fixed submission:

#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;
}
Problem p00042 (C++) — buggy submission, verdict: 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;
}

Error: -11

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, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
#define MAX 1001

int dp[MAX][MAX], t, w, m;
int Val[MAX], Wei[MAX];

void judge() {

  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= w; j++) {
      int k = j - Wei[i];
      if (k < 0) {
        dp[i][j] = dp[i - 1][j];
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i - 1][k] + Val[i]);
      }
    }
  }
  int ans_W = 0, ans_C;
  for (int i = 1; i <= w; i++) {
    ans_W = max(ans_W, dp[m][i]);
  }
  for (int i = w; i >= 1; i--) {
    if (ans_W == dp[m][i])
      ans_C = i;
  }

  cout << ans_W << endl;
  cout << ans_C << endl;
}

void print() { cout << "Case " << t++ << ":" << endl; }

int main() {
  char c;

  t = 1;
  while (cin >> w, w) {
    cin >> m;

    fill(dp[0], dp[MAX - 1], 0);
    Wei[0] = Val[0] = 0;

    print();
    for (int j = 1; j <= m; j++)
      cin >> Val[j] >> c >> Wei[j];

    judge();
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define MAX 1001
typedef pair<int, int> P;

int dp[MAX][MAX], t, w, m;

void judge(vector<P> v) {

  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= w; j++) {
      int k = j - v[i - 1].first;
      if (k < 0) {
        dp[i][j] = dp[i - 1][j];
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i - 1][k] + v[i - 1].second);
      }
    }
  }
  int ans_W = 0, ans_C;
  for (int i = 0; i <= w; i++) {
    ans_W = max(ans_W, dp[m][i]);
  }
  for (int i = w; i >= 0; i--) {
    if (ans_W == dp[m][i])
      ans_C = i;
  }

  cout << ans_W << endl;
  cout << ans_C << endl;
}

void print() { cout << "Case " << t++ << ":" << endl; }

int main() {
  int val, wei;
  string ch;

  t = 1;
  while (cin >> w, w) {
    cin >> m;
    vector<P> v;

    fill(dp[0], dp[MAX - 1], 0);

    print();
    for (int i = 1; i <= m; i++) {
      cin >> val >> ch >> wei;
      v.push_back(P(wei, val));
    }
    sort(v.begin(), v.end());
    judge(v);
  }

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

#include <cstdio>
#include <cstring>
#include <iostream>
#include <utility>

using namespace std;

pair<int, int> max(const pair<int, int> &a, const pair<int, int> &b) {
  if (a.first == b.first ? a.second < b.second : a.first > b.first) {
    return a;
  } else {
    return b;
  }
}

#define MAX_W 1001

int main() {
  int W;
  int case_n = 1;
  while (cin >> W, W != 0) {
    int N;
    cin >> N;
    pair<int, int> dp[N][W + 1];
    for (int i = 0; i < N; i++)
      for (int j = 0; j <= W; j++)
        dp[i][j] = pair<int, int>(0, 0); // 価値、 重さ
    int value[N];
    int weight[N];
    for (int i = 0; i < N; i++) {
      scanf("%d,%d", value + i, weight + i);
    }

    for (int j = weight[0]; j <= W; j++) {
      dp[0][j] = make_pair(value[0], weight[0]);
    }
    for (int i = 1; i < N; i++) {
      for (int j = 0; j <= W; j++) {
        if (j < weight[i]) {
          dp[i][j] = dp[i - 1][j];
        } else {
          dp[i][j] =
              max(dp[i - 1][j],
                  make_pair(dp[i - 1][j - weight[i]].first + value[i],
                            dp[i - 1][j - weight[i]].second + weight[i]));
        }
      }
    }

    cout << "Case " << case_n << ":" << endl;
    cout << dp[N - 1][W].first << endl;
    cout << dp[N - 1][W].second << endl;
    case_n++;
  }
  return 0;
}


Error: TLE

Fixed submission:

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

int W, N;

struct T {
  int v, w;
  T(int value, int weight) { v = value, w = weight; }
  bool operator<(const T &o) const { return (w > o.w); }
};

vector<T> ts;

void init() {
  ts.clear();
  W = N = 0;
}

bool get_input() {
  init();
  cin >> W;
  if (W == 0)
    return false;
  cin >> N;
  int value, weight;
  char dummy;
  for (int i = 0; i < N; i++) {
    cin >> value >> dummy >> weight;
    ts.push_back(T(value, weight));
  }
  sort(ts.begin(), ts.end());
  return true;
}

T vmax(T t1, T t2) {
  if (t1.v == t2.v) {
    if (t1.w > t2.w) {
      return t2;
    } else {
      return t1;
    }
  } else if (t1.v > t2.v) {
    return t1;
  } else {
    return t2;
  }
}

T solve(T t, int ptr) {
  if (ptr == ts.size())
    return t;
  if (ts[ptr].w + t.w > 50)
    return solve(t, ptr + 1);
  else
    return vmax(solve(t, ptr + 1),
                solve(T(t.v + ts[ptr].v, t.w + ts[ptr].w), ptr + 1));
}

void format(T t, int data_set_n) {
  cout << "Case " << data_set_n << ':' << endl;
  cout << t.v << endl;
  cout << t.w << endl;
}

int main() {
  int dn = 1;
  while (get_input()) {
    format(solve(T(0, 0), 0), dn++);
  }
  return 0;
}
Problem p00042 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <stdio.h>
using namespace std;
int W, N, v[1000], w[1000], dp[1001][1001], c, r;
int main() {
  while (~scanf("%d", &W)) {
    if (!W)
      break;
    scanf("%d", &N);
    for (int i = 0; i < N; i++)
      scanf("%d,%d", &v[i], &w[i]);
    for (int i = 1; i <= N; i++) {
      for (int j = 0; j <= W; j++) {
        dp[i][j] = dp[i - 1][j];
        if (j >= w[i - 1]) {
          dp[i][j] = max(dp[i][j], dp[i - 1][j - w[i - 1]] + v[i - 1]);
        }
      }
    }
    printf("Case %d:\\n", ++c);
    printf("%d\\n", dp[N][W]);
    printf("%d\\n", lower_bound(dp[N], dp[N] + W + 1, dp[N][W]) - dp[N]);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <stdio.h>
using namespace std;
int W, N, v[1000], w[1000], dp[1001][1001], c, r;
int main() {
  while (~scanf("%d", &W)) {
    if (!W)
      break;
    scanf("%d", &N);
    for (int i = 0; i < N; i++)
      scanf("%d%d", &w[i], &v[i]);
    for (int i = 1; i <= N; i++) {
      for (int j = 0; j <= W; j++) {
        dp[i][j] = dp[i - 1][j];
        if (j >= w[i - 1]) {
          dp[i][j] = max(dp[i][j], dp[i - 1][j - w[i - 1]] + v[i - 1]);
        }
      }
    }
    printf("Case %d:\\n", ++c);
    printf("%d\\n", dp[N][W]);
    printf("%d\\n", lower_bound(dp[N], dp[N] + W + 1, dp[N][W]) - dp[N]);
  }
  return 0;
}
Problem p00042 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <stdio.h>
using namespace std;
int dp[1005][1005];
int main() {
  int W, N, v[1000], w[1000];
  int n = 1;
  int lastj = 0;
  char jyama;
  while (cin >> W && W) {
    cin >> N;
    for (int i = 0; i < N; i++) {
      scanf("%d,%d", &v[i], &w[i]);
    }
    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]);
          if (j > 0 && dp[i][j] != dp[i][j - 1])
            lastj = j;
        }
      }
    }
    cout << "Case " << n << ":" << endl;
    cout << dp[N][W] << endl;
    cout << lastj << endl;
    ++n;
  }
}

Error: WA

Fixed submission:

#include <iostream>
#include <stdio.h>
using namespace std;
int dp[1001][1001];
int main() {
  int W, N, v[1000], w[1000];
  int n = 1;
  int lastj = 0;
  char jyama;
  while (cin >> W && W) {
    cin >> N;
    for (int i = 0; i < N; i++) {
      scanf("%d,%d", &v[i], &w[i]);
    }
    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]);
          if (j > 0 && dp[i][j] != dp[i][j - 1])
            lastj = j;
        }
      }
    }
    cout << "Case " << n << ":" << endl;
    cout << dp[N][W] << endl;
    cout << lastj << endl;
    ++n;
  }
}
Problem p00042 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
int sum_w[1001][1001];
int sum_v[1001][1001];
void vmax(int i, int j, int v[], int w[]) {
  if (i == 0) {
    if (w[i] <= j) {
      sum_v[i][j] += v[i];
      sum_w[i][j] += w[i];
    }
  } else if (j >= w[i]) {
    if (sum_v[i - 1][j] < sum_v[i - 1][j - w[i]] + v[i]) {
      sum_v[i][j] = sum_v[i - 1][j - w[i]] + v[i];
      sum_w[i][j] = sum_w[i - 1][j - w[i]] + w[i];
    } else {
      sum_v[i][j] = sum_v[i - 1][j];
      sum_w[i][j] = sum_w[i - 1][j];
    }
  } else {
    sum_v[i][j] = sum_v[i - 1][j];
    sum_w[i][j] = sum_w[i - 1][j];
  }
}
int main() {
  int count = 0;
  while (1) {
    int i, j, lim = 0, N = 0, v[1001], w[1001], k = 0, l = 0;
    count++;
    for (i = 0; i < 1001; i++) {
      for (j = 0; j < 1001; j++) {
        sum_v[i][j] = 0;
        sum_w[i][j] = 0;
      }
    }
    cin >> lim;
    if (lim == 0)
      break;
    cin >> N;
    for (i = 0; i < N; i++) {
      scanf("%d,%d", &v[i], &w[i]);
    }
    for (i = 0; i < N; i++) {
      for (j = 0; j <= lim; j++) {
        vmax(i, j, v, w);
      }
    }
    for (i = 0; i <= lim; i++) {
      if (k < sum_v[N - 1][i]) {
        k = sum_v[N - 1][i];
        l = i;
      }
    }
    cout << "Case " << count << ":" << endl
         << sum_v[N - 1][l] << endl
         << sum_w[N - 1][l] << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
int sum_w[1001][1001];
int sum_v[1001][1001];
void vmax(int, int, int[], int[]);
int main() {
  int count = 0;
  while (1) {
    int i, j, lim = 0, N = 0, v[1001], w[1001], k = 0, l = 0;
    for (i = 0; i < 1001; i++) {
      for (j = 0; j < 1001; j++) {
        sum_v[i][j] = 0;
        sum_w[i][j] = 0;
      }
    }
    count++;
    cin >> lim;
    if (lim == 0)
      break;
    cin >> N;
    for (i = 0; i < N; i++) {
      scanf("%d,%d", &v[i], &w[i]);
    }
    for (i = 0; i < N; i++) {
      for (j = 0; j <= lim; j++) {
        vmax(i, j, v, w);
      }
    }
    for (i = 0; i < N; i++) {
      if (k < sum_v[N - 1][i]) {
        k = sum_v[N - 1][i];
        l = i;
      }
    }
    cout << "Case" << count << endl;
    cout << sum_v[N - 1][l] << endl;
    cout << sum_w[N - 1][l] << endl;
  }

  return 0;
}
void vmax(int i, int j, int v[], int w[]) {
  if (sum_v[i - 1][j] < sum_v[i - 1][j - w[i]] + v[i]) {
    sum_v[i][j] = sum_v[i - 1][j - w[i]] + v[i];
    sum_w[i][j] = sum_w[i - 1][j - w[i]] + w[i];
  }
}
Problem p00042 (C++) — buggy submission, verdict: Runtime Error

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

#define MAX_N 1001
#define MAX_W 10001

int dp[MAX_W][MAX_N], W, N, value[MAX_N], w[MAX_N];

int main() {
  int Case = 0;

  while (true) {
    for (int i = 0; i < MAX_N; ++i)
      memset(dp[i], 0, MAX_W);
    // initialize

    ++Case;
    scanf("%d%d", &W, &N);
    if (!W)
      break;
    for (int i = 0; i < N; ++i)
      scanf("%d,%d", &value[i], &w[i]);
    // input

    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 - w[i]] + value[i], dp[i][j]);
      }
    }
    // solve

    int ans = dp[N][W];

    printf("Case %d:\\n%d\\n", Case, ans);

    int i = 0;
    while (true) {
      if (ans != dp[N][W - i]) {
        printf("%d\\n", W - i + 1);
        break;
      }
      ++i;
    }
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>

#define MAX_N 1000
#define MAX_W 10000

int dp[MAX_W][MAX_N], W, N, value[MAX_N], w[MAX_N];

int main() {
  int count = 0;

  while (true) {
    ++count;

    if (w == 0)
      break;

    scanf("%d%d", &W, &N);

    for (int i = 0; i < N; ++i) {
      for (int j = 0; j <= W; ++j)
        dp[j][i] = 0;
    } // initialize

    for (int i = 0; i < N; ++i)
      scanf("%d,%d", &value[i], &w[i]);
    // input

    for (int i = N - 1; i >= 0; ++i) {
      for (int j = 0; j < W; ++j) {
        if (j < w[i])
          dp[i][j] = dp[i + 1][j];
        else
          std::max(dp[i + 1][j], dp[i + 1][j - w[i]] - value[i]);
      }
    }
    printf("count %d:%d?\\n", count, dp[0][W]);
  }
  return 0;
}
Problem p00042 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

const int MAX_N = 1000;
const int MAX_W = 1000;
int W, N;
int v[MAX_N];
int w[MAX_N];
int dp[MAX_W + 1];

void solve() {
  fill(dp, dp + W + 1, 0);
  for (int i = 0; i < N; i++) {
    for (int j = W; j >= 0; j--) {
      if (j + w[i] <= W) {
        dp[j + w[i]] = max(dp[j + w[i]], dp[j] + v[i]);
      }
    }
  }
  printf("%d\\n", dp[W]);
  for (int i = W; i >= 0; i--) {
    if (dp[W] != dp[i]) {
      printf("%d\\n", i + 1);
      break;
    }
  }
}

int main() {
  int i = 0;
  while (scanf("%d", &W), W) {
    i++;
    printf("Case %d:\\n", i);
    scanf("%d", &N);
    for (int i = 0; i < N; i++) {
      scanf("%d,%d", &v[i], &w[i]);
    }
    solve();
  }
  return 0;
}

Error: MLE

Fixed submission:

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

const int MAX_N = 1000;
const int MAX_W = 10000;
int W, N;
int v[MAX_N];
int w[MAX_N];
int dp[MAX_N + 1][MAX_W + 1];

void printtable() {
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j <= W; j++) {
      printf("%4d", dp[i][j]);
    }
    printf("\\n");
  }
}

void solve() {
  memset(dp, 0, sizeof(dp));
  for (int i = 1; i <= N; i++) {
    for (int j = 0; j <= W; j++) {
      if (j - w[i] >= 0) {
        dp[i][j] = max(dp[i - 1][j - w[i]] + v[i], dp[i - 1][j]);
      } else {
        dp[i][j] = dp[i - 1][j];
      }
    }
  }
  printf("%d\\n", dp[N][W]);
  for (int i = W; i >= 0; i--) {
    if (dp[N][W] != dp[N][i]) {
      printf("%d\\n", i + 1);
      break;
    }
  }
}

int main() {
  int i = 0;
  while (scanf("%d", &W), W) {
    i++;
    printf("Case %d:\\n", i);
    scanf("%d", &N);
    for (int i = 0; i < N; i++) {
      scanf("%d,%d", &v[i], &w[i]);
    }
    solve();
  }
  return 0;
}
Problem p00042 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int item_w[1001];
int item_p[1001];
int n;
int w;
int dp[1002][1001];
int main() {
  int casenum = 1;
  for (; cin >> w, w;) {
    cin >> n;
    memset(dp, -1, sizeof(dp));
    for (int i = 0; i < n; i++) {
      char tmp;
      cin >> item_p[i] >> tmp >> item_w[i];
    }
    int wa = 0;
    int ans = 0;
    dp[0][0] = 0;
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= w; j++) {
        if (dp[i][j] != -1) {
          dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
          if (j + item_w[i] <= w)
            dp[i + 1][j + item_w[i]] =
                max(dp[i + 1][j + item_w[i]], dp[i][j] + item_p[i]);
        }
      }
    }
    for (int j = 0; j <= w; j++) {
      if (ans < dp[n][j]) {
        ans = dp[n][j];
        wa = j;
      }
    }
    cout << "Case " << casenum++ << ":" << endl << ans << endl << wa << endl;
  }
}

Error: TLE

Fixed submission:

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

int item_w[1001];
int item_p[1001];
int n;
int w;
int dp[1001][1001]; // kati,omosa

pair<int, int> check(int i, int ww, int pp) {
  //  cout<<i<<","<<ww<<","<<pp<<endl;
  pair<int, int> tmp;
  pair<int, int> tmp2;
  if (ww > w)
    return make_pair(0, 0);
  if (i > n)
    return make_pair(ww, pp);
  if (dp[i][ww] > pp)
    return make_pair(ww, dp[i][ww]);
  dp[i][ww] = pp;
  tmp = check(i + 1, ww, pp);
  tmp2 = check(i + 1, ww + item_w[i], pp + item_p[i]);
  if (tmp.second > tmp2.second ||
      (tmp.second == tmp2.second && tmp.first < tmp2.first)) {
    return tmp;
  } else {
    return tmp2;
  }
}

int main() {
  int casenum = 1;
  for (; cin >> w, w;) {
    cin >> n;
    memset(dp, -1, sizeof(dp));
    for (int i = 0; i < n; i++) {
      char tmp;
      cin >> item_p[i] >> tmp >> item_w[i];
    }
    int wa = 0;
    int ans = 0;
    pair<int, int> ans2 = check(0, 0, 0);
    /*
    for(int i=0;i<=w;i++)
      {
        //	  cout<<"test: i,dp[i]="<<i<<","<<dp[i]<<endl;
        if(ans<dp[i])
          {
            ans=dp[i];
            wa=i;
          }
      }
    */
    cout << "Case " << casenum++ << ":" << endl
         << ans2.second << endl
         << ans2.first << endl;
    // cout<<"Case "<<casenum++<<":"<<endl<<ans<<endl<<wa<<endl;
  }
}
Problem p00042 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int dataset = 1, w;
  while (cin >> w, w) {
    int dp[1001], n, treas_val[1000], treas_w[1000], ans_val = 0, ans_w;
    for (int i = 0; i <= w; i++) {
      dp[i] = -1;
    }
    cin >> n;
    for (int i = 0; i < n; i++) {
      scanf("%d,%d", &treas_val[i], &treas_w[i]);
    }
    dp[0] = 0;
    for (int i = 0; i < n; i++) {
      for (int j = w; j >= 0; j--) {
        if (dp[j] == -1)
          continue;
        if (j + treas_w[i] <= w && dp[j + treas_w[i]] < dp[j] + treas_val[i])
          dp[j + treas_w[i]] = dp[j] + treas_val[i];
      }
    }
    for (int i = 0; i <= w; i++) {
      if (ans_val < dp[i]) {
        ans_val = dp[i];
        ans_w = i;
      }
    }
    printf("Case %d:\\n%d\\n%d\\n", dataset, ans_val, ans_w);
    dataset++;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int dp[1000];
int main() {
  int w, n, ans_w = 0, ans_val = 0;
  for (int dataset = 1; cin >> w, w; dataset++) {
    int t_w, t_val;
    cin >> n;
    for (int i = 0; i < w; i++)
      dp[i] = -1;
    dp[0] = 0;
    for (int i = 0; i < n; i++) {
      scanf("%d,%d", &t_val, &t_w);
      if (i == n - 1)
        for (int i = 0; i < 5000000000; i++) {
        }
      for (int j = w - 1; j >= 0; j--) {
        // printf("%d ", dp[j]);
        if (dp[j] == -1 || j + t_w >= w || dp[j + t_w] > dp[j] + t_val)
          continue;
        dp[j + t_w] = dp[j] + t_val;
      }
      // puts("");
    }
    for (int i = 0; i < w; i++) {
      // printf("%d ", dp[i]);
      if (ans_val < dp[i]) {
        ans_val = dp[i];
        ans_w = i;
      }
    }
    // puts("");
    printf("Case %d:\\n%d\\n%d\\n", dataset, ans_val, ans_w);
  }
  return 0;
}