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

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
  for (int c = 1, w; cin >> w, w; c++) {
    int n, tr[1001][2] = {0};
    cin >> n;
    for (int i = 1; i <= n; i++) {
      scanf("%d,%d", &tr[i][0], &tr[i][1]);
    }
    int dp[1001][1001] = {0};
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j <= w; j++) {
        if (j >= tr[i][1]) {
          dp[j][i] = max(dp[j][i - 1], dp[j - tr[i][1]][i - 1] + tr[i][0]);
        } else {
          dp[j][i] = dp[j][i - 1];
        }
      }
    }
    int pr = dp[w][n], val;
    for (int j = 0; j <= w; j++) {
      if (pr == dp[j][n]) {
        val = j;
        break;
      }
    }
    cout << "Case " << c << ":" << endl;
    cout << pr << endl << val << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
  for (int c = 1, w; cin >> w, w; c++) {
    int n, tr[1000][2] = {0};
    cin >> n;
    for (int i = 0; i < n; i++) {
      scanf("%d,%d", &tr[i][0], &tr[i][1]);
    }
    int dp[100][100] = {0};
    for (int i = 0; i < n; i++) {
      for (int j = 0; j <= w; j++) {
        if (j >= tr[i][1]) {
          dp[j][i] = max(dp[j][i - 1], dp[j - tr[i][1]][i - 1] + tr[i][0]);
        }
      }
    }
    int pr = -1, val;
    for (int j = 0; j <= w; j++) {
      for (int i = 0; i < n; i++) {
        if (pr < dp[j][i]) {
          pr = dp[j][i];
          val = j;
        }
      }
    }
    cout << "Case " << c << ":" << endl;
    cout << pr << endl << val << endl;
  }
  return 0;
}
Problem p00042 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
  for (int c = 1, w; cin >> w, w; c++) {
    int n, t[1001][2], dp[1001][1001] = {0}, m = 0, ans;
    cin >> n;
    for (int i = 1; i <= n; i++)
      scanf("%d,%d", &t[i][0], &t[i][1]);
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j <= w; j++) {
        if (j >= t[i][1])
          dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - t[i][1]] + t[i][0]);
        else
          dp[i][j] = dp[i - 1][j];
      }
    }
    for (int i = 0; i <= w; i++) {
      if (m < dp[n][i]) {
        m = dp[n][i];
        ans = i;
      }
    }
    cout << "Case " << c << ":" << endl << m << endl << ans << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
  int w, c = 0;
  while (cin >> w, w) {
    c++;
    int n, t[1500][2] = {0}, dp[1500][1500] = {0};
    cin >> n;
    for (int i = 0; i < n; i++)
      scanf("%d,%d", &t[i][0], &t[i][1]);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j <= w; j++) {
        if (j + t[i][1] <= w)
          dp[i + 1][j + t[i][1]] = max(dp[i][j + t[i][1]], dp[i][j] + t[i][0]);
        dp[i + 1][j] = max(dp[i][j], dp[i + 1][j]);
      }
    }
    int m = 0, ans;
    for (int i = 0; i <= w; i++) {
      if (m < dp[n][i]) {
        m = dp[n][i];
        ans = i;
      }
    }
    cout << "Case " << c << endl << m << endl << ans << endl;
  }
  return 0;
}
Problem p00042 (C++) — buggy submission, verdict: Runtime Error

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

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

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

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

Error: 0

Fixed submission:

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

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

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

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(n) rep(i, n)
#define onen(i, n) for (int i = 1; i < n + 1; i++)

const int MAXW = 1001, MAXN = 1001;
int dp[MAXN][MAXW], w[MAXN], v[MAXN];
int n, maxw;

int main() {
  int setnum = 1;
  while (cin >> maxw && maxw) {
    memset(dp, 0, sizeof(dp));
    char t;

    cin >> n;
    REP(n) cin >> v[i] >> t >> w[i];

    rep(i, n) onen(j, maxw) {
      dp[i + 1][j] = max(dp[i][j], dp[i + 1][j - 1]);
      if (j >= w[i]) {
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j - w[i]] + v[i]);
      }
    }

    int maxv = -1, weight = 0;
    onen(j, maxw) if (dp[n][j] > maxv) {
      maxv = dp[n][j];
      weight = j;
    }

    cout << "Case " << setnum << ":" << endl;
    cout << maxv << endl;
    cout << weight << endl;
    setnum++;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(n) rep(i, n)
#define onen(i, n) for (int i = 1; i < n + 1; i++)

const int MAXW = 1001, MAXN = 10001;
int dp[MAXN][MAXW], w[MAXN], v[MAXN];
int n, maxw;

int main() {
  int setnum = 1;
  while (cin >> maxw && maxw) {
    memset(dp, 0, sizeof(dp));
    char t;

    cin >> n;
    REP(n) cin >> v[i] >> t >> w[i];

    rep(i, n) onen(j, maxw) {
      dp[i + 1][j] = max(dp[i + 1][j], dp[i][j - 1]);
      if (j >= w[i])
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j - w[i]] + v[i]);
    }

    int maxv = -1, weight = 0;
    onen(j, maxw) if (dp[n][j] > maxv) {
      maxv = dp[n][j];
      weight = j;
    }

    cout << "Case " << setnum << ":" << endl;
    cout << maxv << endl;
    cout << weight << endl;
    setnum++;
  }

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

#include <bits/stdc++.h>
using namespace std;
int dp[1001][1001], wait[1001], value[1001];
int power(int n) {
  int num = 1;
  for (int i = 1; i <= n; i++)
    num *= 10;
  return num;
}
int main() {
  int n, w, point, num = 1, wa = 0;
  char c;
  while (cin >> w && w != 0) {
    cin >> n;
    for (int i = 0; i < n; i++)
      cin >> value[i] >> c >> wait[i];
    memset(dp, 0, sizeof(dp));
    for (int i = n - 1; i >= 0; i--) {
      for (int j = 0; j <= w; j++) {
        if (j < wait[i])
          dp[i][j] = dp[i + 1][j];
        else
          dp[i][j] = max(dp[i + 1][j], dp[i + 1][j - wait[i]] + value[i]);
      }
    }
    int wa = 0, point = 0;
    for (int i = 0; i <= w; i++)
      if (dp[0][wa] < dp[0][i])
        wa = i;
    cout << "Case " << num << ":\\n" << dp[0][w] << endl << wa << endl;
    num++;
  }
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int dp[1001][1001], wait[1001], value[1001], dpp[1001][20000];
void Initialization() {
  for (int i = 0; i < 1001; i++)
    for (int j = 0; j < 1001; j++)
      dp[i][j] = -1;
  for (int i = 0; i < 1001; i++)
    for (int j = 0; j < 20000; j++)
      dpp[i][j] = -1;
}
int power(int n) {
  int num = 1;
  for (int i = 1; i <= n; i++)
    num *= 10;
  return num;
}
int serch(int i, int j, int n) {
  int num;
  if (dp[i][j] > -1)
    return dp[i][j];
  else if (i == n)
    num = 0;
  else if (j < wait[i])
    num = serch(i + 1, j, n);
  else
    num = max(serch(i + 1, j, n), serch(i + 1, j - wait[i], n) + value[i]);
  return dp[i][j] = num;
}
int wserch(int i, int va, int ser, int n, int w) {
  int num;
  if (dpp[i][va] > -1)
    return dpp[i][va];
  else if (i == n)
    num = w;
  else if (ser < va + value[i])
    num = wserch(i + 1, va, ser, n, w);
  else if (ser == va + value[i])
    num = wait[i];
  else
    num = min(wserch(i + 1, va, ser, n, w),
              wserch(i + 1, va + value[i], ser, n, w) + wait[i]);
  return dpp[i][va] = num;
}
int main() {
  int n, w, point, num = 1, wai, wa;
  string str;
  while (cin >> w && w != 0) {
    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> str;
      value[i] = 0;
      wait[i] = 0;
      for (int j = 0;; j++) {
        if (str[j] == ',') {
          for (int k = 0; k < j; k++)
            value[i] += power(j - k - 1) * (str[k] - '0');
          point = j;
        }
        if (str[j] == '\\0') {
          for (int k = point + 1; k < j; k++)
            wait[i] += power(j - k - 1) * (str[k] - '0');
          break;
        }
      }
    }
    Initialization();
    point = serch(0, w, n);
    wai = w;
    for (int i = 0; i < n; i++) {
      if (value[i] < point) {
        wa = wserch(i + 1, value[i], point, n, w) + wait[i];
        if (wa < wai)
          wai = wa;
      } else if (value[i] == point && wai > wait[i])
        wai = wait[i];
    }
    cout << "Case " << num << ":\\n" << point << endl << wai << endl;
    num++;
  }
}
Problem p00042 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

using namespace std;

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

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

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

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

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

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

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

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

  return 0;
}

Error: TLE

Fixed submission:

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

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

using namespace std;

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

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

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

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

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

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

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

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

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

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

Error: TLE

Fixed submission:

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

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

Error: MLE

Fixed submission:

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

#include <algorithm>
#include <iostream>
#include <stdio.h>
using namespace std;

int N, W;
int m[1010][2];
int dp[1010][1010];
int cnt = 0;

int main() {

  while (cin >> W, W) {
    cin >> N;
    cnt++;
    for (int i = 0; i < 1010; i++)
      for (int j = 0; j < 1010; j++)
        dp[i][j] = 0;

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

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

    int best[2] = {0};
    for (int j = 0; j <= W; j++) {
      if (best[0] < dp[N][j]) {
        best[0] = dp[N][j];
        best[1] = j;
      }
    }

    cout << "Case " << cnt << ':' << endl;
    cout << best[0] << endl;
    cout << best[1] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

int N, W;
int v[1001], w[1001];
int dp[1002][1002];
int ans1, ans2;

void solve() {
  memset(dp, -1, sizeof(dp));
  dp[0][0] = 0;
  ans1 = 0;
  ans2 = 0;

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

  for (int i = 0; i < W; i++) {
    if (ans1 < dp[N][i]) {
      ans1 = dp[N][i];
      ans2 = i;
    }
  }
}

int main() {

  int t = 0;

  while (cin >> W >> N, W) {
    for (int i = 1; i < N; i++) {
      scanf("%d,%d", &v[i], &w[i]);
    }
    t++;
    solve();

    cout << "Case " << t << ':' << endl;
    cout << ans1 << endl;
    cout << ans2 << endl;
  }

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

#include <stdio.h>
int a, b, c[1001][1001], d[1001], e[1001], m, mm, i, j, g = 1;
int main(void) {
  scanf("%d", &a);
  while (a != 0) {
    m = 0;
    mm = 0;
    scanf("%d", &b);
    for (i = 0; i <= b; i++) {
      for (j = 0; j <= a; j++) {
        c[i][j] = -1;
      }
    }
    for (i = 0; i < b; i++) {
      scanf("%d,%d", &d[i], &e[i]);
    }
    c[0][0] = 0;
    for (i = 0; i < b; i++) {
      for (j = 0; j <= a; j++) {
        if (c[i][j] >= 0 && c[i][j] + d[i] > c[i + 1][j + e[i]] &&
            j + e[i] <= a) {
          c[i + 1][j + e[i]] = c[i][j] + d[i];
        }
        if (c[i][j] >= 0 && c[i + 1][j] < c[i][j]) {
          c[i + 1][j] = c[i][j];
        }
      }
    }
    for (j = 0; j <= a; j++) {
      if (c[b][j] > m) {
        m = c[b][j];
        mm = j;
        // printf("c=%d j=%d\\n",c[b][j],j);
      }
    }
    /*for(i=0;i<=b;i++){
            for(j=0;j<=a;j++){
                    printf("%4d",c[i][j]);
            }
            printf("\\n");
    }
    printf("%d\\n",c[b][49]);*/
    printf("Case %d:\\n%d\\n%d\\n", g, m, mm);
    g++;
    scanf("%d", &a);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>
int a, b, c[1001][1001], d[1001], e[1001], m, mm, i, j, g = 1;
int main(void) {
  scanf("%d", &a);
  while (a != 0) {
    m = 0;
    mm = 0;
    scanf("%d", &b);
    for (i = 0; i <= b; i++) {
      for (j = 0; j <= a; j++) {
        c[i][j] = -1;
        d[i] = 0;
        e[i] = 0;
      }
    }
    for (i = 0; i < b; i++) {
      scanf("%d,%d", &d[i], &e[i]);
    }
    c[0][0] = 0;
    for (i = 0; i <= b; i++) {
      for (j = 0; j <= a; j++) {
        if (c[i][j] >= 0 && c[i][j] + d[i] > c[i + 1][j + e[i]] &&
            j + e[i] <= a && i + 1 <= b) {
          c[i + 1][j + e[i]] = c[i][j] + d[i];
        }
        if (c[i][j] >= 0 && c[i + 1][j] < c[i][j] && i + 1 <= b) {
          c[i + 1][j] = c[i][j];
        }
      }
    }
    for (j = 0; j <= a; j++) {
      if (c[b][j] > m) {
        m = c[b][j];
        mm = j;
        // printf("c=%d j=%d\\n",c[b][j],j);
      }
    }
    /*for(i=0;i<=b;i++){
            for(j=0;j<=a;j++){
                    printf("%4d",c[i][j]);
            }
            printf("\\n");
    }
    printf("%d\\n",c[b][49]);*/
    printf("Case %d:\\n%d\\n%d\\n", g, m, mm);
    g++;
    scanf("%d", &a);
  }
  return 0;
}