alexjercan/bugnet huggingface dataset:
Problem p00042 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

int MAX = 1010;

void solve(int n, int W, vector<int> v, vector<int> w) {
  int **dp = new int *[n];
  for (int i = 0; i < n; i++) {
    dp[i] = new int[MAX + 1];
    for (int j = 0; j < MAX + 1; j++)
      dp[i][j] = 0;
  }

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

  int val = -1, weight = 0;
  for (int j = 0; j <= W; j++) {
    if (val < dp[n - 1][j]) {
      val = dp[n - 1][j];
      weight = j;
    }
  }

  cout << val << endl << weight << endl;

  for (int i = 0; i < n; i++)
    delete[] dp[i];
  delete[] dp;
}

int main() {

  int n, W;
  int ver = 1;

  cin >> W;
  while (W != 0) {
    cin >> n;

    vector<int> v(n), w(n);
    for (int i = 0; i < n; i++)
      scanf("%d,%d", &v[i], &w[i]);

    cout << "Case " << ver << ":" << endl;
    solve(n, W, v, w);

    cin >> W;
    ver++;
  }

  return 0;
}

Error: 0

Fixed submission:

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

using namespace std;

int MAX = 1000 * 1000;

void solve(int n, int W, vector<int> v, vector<int> w) {
  int **dp = new int *[n];
  for (int i = 0; i < n; i++) {
    dp[i] = new int[MAX + 2];
    for (int j = 0; j < MAX + 2; j++)
      dp[i][j] = 0;
  }

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

  int val = -1, weight = 0;
  for (int j = 0; j <= W; j++) {
    if (val < dp[n - 1][j]) {
      val = dp[n - 1][j];
      weight = j;
    }
  }

  cout << val << endl << weight << endl;

  for (int i = 0; i < n; i++)
    delete[] dp[i];
  delete[] dp;
}

int main() {

  int n, W;
  int ver = 1;

  cin >> W;
  while (W != 0) {
    cin >> n;

    vector<int> v(n), w(n);
    for (int i = 0; i < n; i++)
      scanf("%d,%d", &v[i], &w[i]);

    cout << "Case " << ver << ":" << endl;
    solve(n, W, v, w);

    cin >> W;
    ver++;
  }

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define it ::iterator
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double ESP = 1e-10;
using namespace std;
int main() {
  int m;
  int co = 0;
  while (cin >> m, m) {
    co++;
    int n;
    cin >> n;
    int in[1001][1001] = {0};
    rep(q, n) {
      int i = q + 1;
      int a, b;
      char d;
      cin >> a >> d >> b;
      rep(j, m + 1) {
        if (j >= b)
          in[i][j] = max(in[i][j], in[i - 1][j - b] + a);
        in[i][j] = max(in[i][j], in[i - 1][j]);
      }
    }
    int max1 = 0;
    rep(i, m + 1) max1 = max(max1, in[n][i]);
    rep(i, m + 1) if (in[n][i] == max1) {
      cout << "Case " << co << ":" << endl;
      cout << max1 << endl << i << endl;
      break;
    }
  }
}

/*int main(){
  int m;
  int co=0;
  while(cin>>m,m){
    co++;
    int n;
    char d;
    cin>>n;
    vector<int>w(n),v(n);
    vector<vector<int> >dp(n+1);
    rep(i,m+1)dp[0].pb(0);
    rep(q,n){
      int i=q+1;
      int a,b;char d;
      cin>>a>>d>>b;
      dp[i].pb(0);


}
*/

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define it ::iterator
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double ESP = 1e-10;
using namespace std;
int main() {
  int m;
  int co = 0;
  while (cin >> m, m) {
    co++;
    int n;
    cin >> n;
    vector<int> in(50, 0);
    rep(i, n) {
      int a, b;
      char d;
      cin >> a >> d >> b;
      for (int i = m; i > 0; i--) {
        if (i - b < 0)
          continue;
        if (i != b && !in[i - b])
          continue;
        in[i] = max(in[i - b] + a, in[i]);
      }
      // rep(i,m+1)cout<<in[i]<<" "<<i<<endl;
      // cout<<" "<<endl;
    }
    int ans1 = 0, ans2 = 0;
    for (int i = m; i > 0; i--) {
      if (ans1 <= in[i]) {
        ans1 = in[i];
        ans2 = i;
      }
    }
    cout << "Case " << co << ":" << endl;
    cout << ans1 << endl << ans2 << endl;
  }
}
Problem p00042 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <utility>
using namespace std;
typedef pair<int, int> P;

const int W_MAX = 1000;
const int N_MAX = 1000;

int v[N_MAX + 1], w[N_MAX + 1];
int N, W;
P memo[N_MAX + 1][W_MAX + 1];

P bfs(int i, int j) {
  if (i == N)
    return P(0, 0);
  if (memo[i][j] >= P(0, 0)) {
    return memo[i][j];
  }
  P p;
  if (w[i] > j) {
    p = bfs(i + 1, j);
  } else {
    P p1, p2;
    p1 = bfs(i + 1, j);
    p2 = bfs(i + 1, j - w[i]);
    p2.first += v[i];
    p2.second += w[i];
    if (p1.first == p2.first) {
      if (p1.second < p2.second) {
        p = p1;
      } else {
        p = p2;
      }
    } else {
      p = max(p1, p2);
    }
  }
  return memo[i][j] = p;
}

int main() {
  int c = 1;
  while (cin >> W, W) {
    cin >> N;
    fill((P *)memo, (P *)(memo + N_MAX + 1), P(-1, -1));
    for (int i = 0; i < N; ++i) {
      cin >> v[i];
      cin.ignore(1, ',');
      cin >> w[i];
    }
    cout << "Case " << c++ << ':' << endl;
    P p = bfs(0, W);
    cout << p.first << endl << p.second << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <utility>
using namespace std;

const int W_MAX = 1000;
const int N_MAX = 1000;

int v[N_MAX + 1], w[N_MAX + 1];
int N, W;

typedef pair<int, int> P;

P bfs(int i, int j) {
  if (i == N)
    return P(0, 0);
  if (w[i] > j) {
    return bfs(i + 1, j);
  } else {
    P p1, p2;
    p1 = bfs(i + 1, j);
    p2 = bfs(i + 1, j - w[i]);
    p2.first += v[i];
    p2.second += w[i];
    if (p1.first == p2.first) {
      if (p1.second < p2.second) {
        return p1;
      } else {
        return p2;
      }
    } else {
      return max(p1, p2);
    }
  }
}

int main() {
  int c = 1;
  while (cin >> W, W) {
    cin >> N;
    for (int i = 0; i < N; ++i) {
      cin >> v[i];
      cin.ignore(1, ',');
      cin >> w[i];
    }
    cout << "Case " << c++ << ':' << endl;
    P p = bfs(0, W);
    cout << p.first << endl << p.second << endl;
  }
}
Problem p00042 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <stdio.h>
#include <vector>

#define TEST 0
typedef std::pair<int, int> pi; // first: value,second: weight

struct Pred {
  bool operator()(const pi &f, const pi &s) //<
  {
    return f.second < s.second;
  }
};

int wmax[1001], cnt[1000];
void maxassign(int index, int value) {
  if (wmax[index] < value)
    wmax[index] = value;
}
pi rec_check(const std::vector<pi> &data, int start, int remw) {}
int main(void) {
  int ccount = 1;
  int w, n;
  using namespace std;
  vector<pair<int, int>> data;
  while (scanf("%d%d", &w, &n) == 2 && w != 0) {
    data.assign(n, std::pair<int, int>(0, 0));
    for (int i = 0; i < n; i++) {
      scanf("%d%*[,]%d", &data[i].first, &data[i].second);
    }
    Pred pred;
    sort(data.begin(), data.end(), pred);
    printf("Case %d:\\n", ccount);
    for (int i = 0; i < 1001; i++) {
      wmax[i] = -1;
    }
    wmax[0] = 0;
#if TEST
    for (int i = 0; i < n; i++) {
      printf("val=%d,wei=%d\\n", data[i].first, data[i].second);
    }
#endif
    for (int i = 0; i < n; i++) {
      // Now we think about treasure[i]
      for (int j = w - data[i].second; j >= 0; j--) {
        if (wmax[j] != -1)
          maxassign(j + data[i].second, wmax[j] + data[i].first);
      }
    }
    int max = -1;
    int ind = -1;
    for (int i = 0; i <= w; i++) {
      if (max < wmax[i]) {
        max = wmax[i];
        ind = i;
      }
    }
#if TEST
    for (int i = 0; i <= w; i++) {
      if (wmax[i] != -1)
        printf("\\nwmax[%d]=%d", i, wmax[i]);
    }
#endif
    printf("%d\\n%d\\n", max, ind);
    data.clear();
    ccount++;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <stdio.h>
#include <vector>

#define TEST 1
typedef std::pair<int, int> pi;

struct Pred {
  bool operator()(const pi &f, const pi &s) //<
  {
    return f.first < s.first;
  }
};
pi rec_check(const std::vector<pi> &data, int start, int remw) {
  if (start >= data.size())
    return pi(0, 0);
  if (data[start].second > remw)
    return rec_check(data, start + 1, remw);
  pi div0 = rec_check(data, start + 1, remw - data[start].second);
  div0.first += data[start].first;
  div0.second += data[start].second;
  pi div1 = rec_check(data, start + 1, remw);
  if (div0.first > div1.first)
    return div0;
  if (div0.first < div1.first)
    return div1;
  if (div0.second < div1.second)
    return div0;
  return div1;
}
int main(void) {
  int ccount = 1;
  int w, n;
  using namespace std;
  vector<pair<int, int>> data;
  while (scanf("%d%d", &w, &n) == 2 && w != 0) {
    data.assign(n, std::pair<int, int>(0, 0));
    for (int i = 0; i < n; i++) {
      scanf("%d%*[,]%d", &data[i].first, &data[i].second);
    }
    Pred pred;
    sort(data.begin(), data.end(), pred);
    printf("Case %d:", ccount);
#if TEST
    for (int i = 0; i < n; i++) {
      printf("val=%d,wei=%d\\n", data[i].first, data[i].second);
    }
#endif
    pi result = rec_check(data, 0, w);
    printf("%d\\n%d\\n", result.first, result.second);
    data.clear();
    ccount++;
  }
  return 0;
}
Problem p00042 (C++) — buggy submission, verdict: Time Limit Exceeded


// dpの練習
// dpでなく、愚直に組んだプログラミング
// ナップサック問題とほぼ同じ

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

using namespace std;

int wei;            // 持てる最大の重さ
int n;              // お宝の数
int v[1001];        // お宝の価値
int w[1001];        // お宝の重さ
                    // グローバル配列は0で初期化されている
int dp[1001][1001]; // メモテーブル

int max_value(int i, int j) {
  int value;

  if (dp[i][j] >= 0) {
    return dp[i][j];
    // すでに１回は調べている
  }

  if (i == n) {
    // 品物がもう残っていないときは、価値の和の最大値は0で確定
    // もうお宝がないつまり、価値は0
    value = 0;
  } else if (j < w[i]) {
    // 風呂敷が持てる重さを超えてしまう
    //  残りの容量が足りず品物iを入れられないので、入れないパターンだけ処理
    //  i+1 以降の品物のみを使ったときの最大値をそのままこの場合の最大値にする
    value = max_value(i + 1, j);
  } else {
    // 左がお宝を入れない、右がお宝を入れた場合
    //  品物iを入れるか入れないか選べるので、両方試して価値の和が大きい方を選ぶ
    value = max(max_value(i + 1, j), max_value(i + 1, j - w[i]) + v[i]);
  }
  return dp[i][j] = value;
  // メモに記憶させる
}

void max_weight(int ans) {

  int w_min;

  for (int i = wei - 1; i >= 0; i--) {
    if (ans != max_value(0, i)) {
      w_min = i + 1;
      break;
    }
  }
  cout << w_min << endl;
}

int main(void) {

  int cnt = 1;
  while (cin >> wei) {
    memset(dp, -1, sizeof(dp));
    // まだ調べてないところを-1で初期化
    if (wei == 0)
      break;
    cin >> n;
    char ten;
    for (int i = 0; i < n; i++) {
      cin >> v[i] >> ten >> w[i];
    }
    cout << "Case " << cnt << ":" << endl;
    int ans = max_value(0, wei);
    cout << ans << endl;
    max_weight(ans);
    cnt++;
  }
  return 0;
}

Error: TLE

Fixed submission:


// dpの練習
// dpでなく、愚直に組んだプログラミング
// ナップサック問題とほぼ同じ

#include <algorithm>
#include <iostream>

using namespace std;

int wei;     // 持てる最大の重さ
int n;       // お宝の数
int v[1001]; // お宝の価値
int w[1001]; // お宝の重さ
             // グローバル配列は0で初期化されている

int max_value(int i, int j) {
  int value;

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

void max_weight(int ans) {

  int w_min;

  for (int i = wei - 1; i >= 0; i--) {
    if (ans != max_value(0, i)) {
      w_min = i + 1;
      break;
    }
  }
  cout << w_min << endl;
}

int main(void) {

  int cnt = 1;
  while (cin >> wei) {
    if (wei == 0)
      break;
    cin >> n;
    char ten;
    for (int i = 0; i < n; i++) {
      cin >> v[i] >> ten >> w[i];
    }
    cout << "Case " << cnt << ":" << endl;
    int ans = max_value(0, wei);
    cout << ans << endl;
    max_weight(ans);
    cnt++;
  }
  return 0;
}
Problem p00042 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

using namespace std;

class item {
public:
  int value;
  int weight;
};

bool input(int *c, int *n, item it[1000]) {
  cin >> *c;
  if (*c == 0) {
    return false;
  }
  cin >> *n;
  for (int i = 0; i < *n; i++) {
    char t;
    cin >> it[i].value >> t >> it[i].weight;
  }
  return true;
}

item max(item a, item b) {
  if (a.value > b.value) {
    return a;
  } else {
    return b;
  }
}

item add(item a, item b) {
  a.value += b.value;
  a.weight += b.weight;
  return a;
}

item solve(int c, int n, item it[1000]) {
  vector<vector<item>> table(n + 1, vector<item>(c + 1));
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= c; j++) {
      if (j == 0 || i == 0) {
        table[i][j].value = 0;
        table[i][j].weight = 0;
      } else if (j < it[i - 1].weight) {
        table[i][j] = table[i - 1][j];
      } else {
        table[i][j] = max(table[i - 1][j],
                          add(table[i - 1][j - it[i - 1].weight], it[i - 1]));
      }
    }
  }
  return table[n][c];
}

int main(void) {
  int c, n;
  item it[1000];
  for (int i = 1; input(&c, &n, it); i++) {
    item ans = solve(c, n, it);
    cout << "Case " << i << ':' << endl
         << ans.value << endl
         << ans.weight << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>

using namespace std;

class item {
public:
  int value;
  int weight;
  bool flag;
};

bool input(int *c, int *n, item it[1000]) {
  cin >> *c;
  if (*c == 0) {
    return false;
  }
  cin >> *n;
  for (int i = 0; i < *n; i++) {
    char t;
    cin >> it[i].value >> t >> it[i].weight;
  }
  return true;
}

item max(item a, item b) {
  if (a.value > b.value) {
    return a;
  } else {
    return b;
  }
}

item add(item a, item b) {
  a.value += b.value;
  a.weight += b.weight;
  return a;
}

item table[1000][1000];

item solve(int c, int n, item it[1000]) {
  if (table[c][n].flag != true) {
    if (c == 0 || n == 0) {
      table[c][n].value = 0;
      table[c][n].weight = 0;
    } else if (c < it[n - 1].weight) {
      table[c][n] = solve(c, n - 1, it);
    } else {
      table[c][n] = max(solve(c, n - 1, it),
                        add(solve(c - it[n - 1].weight, n - 1, it), it[n - 1]));
    }
    table[c][n].flag = true;
  }
  return table[c][n];
}

int main(void) {
  for (int i = 1;; i++) {
    int c, n;
    item it[1000];
    if (input(&c, &n, it) == false) {
      break;
    }
    for (int k = 0; k < 1000; k++) {
      for (int j = 0; j < 1000; j++) {
        table[k][j].flag = false;
        table[k][j].value = 0;
        table[k][j].weight = 0;
      }
    }
    item ans = solve(c, n, it);
    cout << "Case " << i << ':' << endl
         << ans.value << endl
         << ans.weight << endl;
  }
  return 0;
}
Problem p00042 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

int main() {
  string str = "";
  int count = 0;

  while (getline(cin, str)) {
    int max_wight;
    stringstream ssw(str);
    ssw >> max_wight;

    if (max_wight == 0) {
      break;
    } // end if
    ++count;

    int n;
    getline(cin, str);
    stringstream ssn(str);
    ssn >> n;

    vector<int> W(n, 0);
    vector<int> V(n, 0);
    vector<int> DP(max_wight + 1, 0);
    int i, j;

    for (i = 0; i < n; ++i) {
      getline(cin, str);
      while (str.find(',') != string::npos) {
        str = str.replace(str.find(','), 1, " ");
      } // end while
      stringstream ss(str);
      ss >> V[i] >> W[i];
    } // end for

    for (i = 0; i < n; ++i) {
      vector<int> dp = DP;
      for (j = W[i]; j <= max_wight; ++j) {
        int p = j - W[i];
        if (dp[p] + V[i] > DP[j]) {
          DP[j] = dp[p] + V[i];
        } // end if
      }   // end for
    }     // end for

    int maxvalue = DP[max_wight];
    int minwight = max_wight;

    for (j = max_wight - 1; j >= 0; --j) {
      if (DP[j] >= maxvalue) {
        minwight = j;
        maxvalue = DP[j];
      } // end if
    }   // end for
    cout << "Case " << count << ':' << endl;
    cout << maxvalue << endl;
    cout << minwight << endl;

  } // end while

  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>

int main() {
  for (int c = 1;; c++) {
    int w, n, kati[100], weit[100], weitk = 0, katik = 0;
    while (scanf("%d", &w)) {
      if (w == 0) {
        return 0;
      } else if (w > 1000) {
      } else {
        break;
      }
    }

    scanf("%d", &n);

    int katisum[100][100] = {0}, weitsum[100][100] = {0};
    for (int x = 0; x < n; x++) {
      scanf("%d,%d", &kati[x], &weit[x]);
    }
    for (int y = 0; y < n; y++) {
      for (int z = y; z < n; z++) {
        for (int r = y; r <= z; r++) {
          katisum[y][z] += kati[r];
          weitsum[y][z] += weit[r];
        }
      }
    }

    for (int y = 0; y < n; y++) {
      for (int z = 0; z < n; z++) {
        if (w >= weitsum[y][z] && katik < katisum[y][z]) {
          weitk = weitsum[y][z];
          katik = katisum[y][z];
        }
      }
    }
    for (int y = 0; y < n; y++) {
      for (int z = 0; z < n; z++) {
        if (katik == katisum[y][z] && weitk > weitsum[y][z]) {
          weitk = weitsum[y][z];
        }
      }
    }
    printf("Case %d:\\n%d\\n%d\\n", c, katik, weitk);
  }
}
Problem p00042 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int w, n;
  int count = 0;
  while (cin >> w, w) {
    vector<pair<int, int>> v;
    long long dp[1010][1010];
    memset(dp, 0, sizeof(dp));
    cin >> n;
    for (int i = 0; i < n; i++) {
      int s, t;
      scanf("%d,%d", &s, &t);
      v.push_back(make_pair(s, t));
    }
    for (int i = n - 1; 0 <= i; i--) {
      for (int j = 0; j <= w; j++) {
        if (j < v[i].second)
          dp[i][j] = dp[i + 1][j];
        else
          dp[i][j] = max(dp[i + 1][j], dp[i + 1][j - v[i].second] + v[i].first);
      }
    }
    cout << "Case " << ++count << ":" << endl;
    cout << dp[0][w] << endl;
    for (int i = w;; i--) {
      if (dp[0][i] != dp[0][w]) {
        cout << i + 1 << endl;
        break;
      }
    }
  }
}

Error: 0

Fixed submission:

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

int main() {
  int w, n;
  int count = 0;
  while (cin >> w, w) {
    vector<pair<int, int>> v;
    long long dp[1000][1000];
    memset(dp, 0, sizeof(dp));
    cin >> n;
    for (int i = 0; i < n; i++) {
      int s, t;
      scanf("%d,%d", &s, &t);
      v.push_back(make_pair(s, t));
    }
    for (int i = n - 1; 0 <= i; i--) {
      for (int j = 0; j <= w; j++) {
        if (j < v[i].second)
          dp[i][j] = dp[i + 1][j];
        else
          dp[i][j] = max(dp[i + 1][j], dp[i + 1][j - v[i].second] + v[i].first);
      }
    }
    cout << "Case " << ++count << ":" << endl;
    cout << dp[0][w] << endl;
    for (int i = w;; i--) {
      if (dp[0][i] != dp[0][w]) {
        cout << i + 1 << endl;
        break;
      }
    }
  }
}
Problem p00042 (C++) — buggy submission, verdict: 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;
}

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

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int dp[1001][1001] = {{0}};
int main() {
  int w, n;
  char ch;
  int P[1001] = {0}, W[1001] = {0}, t = 1;
  while (cin >> w, w) {
    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> P[i] >> ch >> W[i];
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j <= w; j++) {
        if (j - W[i] >= 0) {
          dp[i + 1][j] = max(dp[i][j], dp[i][j - W[i]] + P[i]);
        } else
          dp[i + 1][j] = dp[i][j];
      }
    }
    cout << "Case " << t << ":" << endl;
    for (int i = w; i >= 0; i--) {
      if (dp[n][i] != dp[n][w]) {
        cout << dp[n][w] << endl << i + 1 << endl;
        break;
      }
    }
    t++;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int dp[1000][1000] = {{0}};
int main() {
  int w, n;
  char ch;
  int P[1000] = {0}, W[1000] = {0}, t = 1;
  while (cin >> w, w) {
    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> P[i] >> ch >> W[i];
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j <= w; j++) {
        if (j - W[i] >= 0) {
          dp[i + 1][j] = max(dp[i][j], dp[i][j - W[i]] + P[i]);
        } else
          dp[i + 1][j] = dp[i][j];
      }
    }
    cout << "Case " << t << ":" << endl;
    for (int i = w; i >= 0; i--) {
      if (dp[n][i] != dp[n][w]) {
        cout << dp[n][w] << endl << i + 1 << endl;
        break;
      }
    }
    t++;
  }
  return 0;
}