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

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

#define req(i, r, k) for (int i = k; i < r; i++)
#define rep(i, r) req(i, r, 0)

const long long int INF = 100000000000000;

long long int dp[1002][1002];

int main() {

  for (int a = 1; 1; a++) {
    int w, n;
    scanf(" %d", &w);
    if (w == 0)
      break;
    scanf(" %d", &n);
    pair<int, int> t[n];
    char c;
    rep(i, n) { scanf(" %d%c%d", &t[i].first, &c, &t[i].second); }

    rep(i, w) {
      dp[0][i] = 0;
      dp[i][0] = 0;
    }

    dp[0][0] = 0;
    rep(i, n + 1) {
      rep(j, w + 1) {
        if (j >= t[i].second) {
          dp[i + 1][j] = max(dp[i][j], dp[i][j - t[i].second] + t[i].first);
        } else {
          dp[i + 1][j] = dp[i][j];
        }
      }
    }
    long long int maxvalue = 0, thenweight = 0;

    rep(i, w + 1) {
      // cout << dp[i] << " " << i << " " << thenweight << endl;
      if (maxvalue < dp[n][i]) {
        maxvalue = dp[n][i];
        thenweight = i;
      }
    }

    // printf("Case %d:\\n%lld\\n%lld\\n",a,maxvalue,thenweight);
    cout << "Case " << a << ':' << endl;
    cout << maxvalue << endl;
    cout << thenweight << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

#define req(i, r, k) for (int i = k; i < r; i++)
#define rep(i, r) req(i, r, 0)

const long long int INF = 100000000000000;

long long int dp[1001][1001];

int main() {

  for (int a = 1; 1; a++) {
    int w, n;
    scanf(" %d", &w);
    if (w == 0)
      break;
    scanf(" %d", &n);
    pair<int, int> t[n];
    char c;
    rep(i, n) { scanf(" %d%c%d", &t[i].first, &c, &t[i].second); }

    rep(i, w) {
      dp[0][i] = 0;
      dp[i][0] = 0;
    }

    dp[0][0] = 0;
    rep(i, n + 1) {
      rep(j, w + 1) {
        if (j >= t[i].second) {
          dp[i + 1][j] = max(dp[i][j], dp[i][j - t[i].second] + t[i].first);
        } else {
          dp[i + 1][j] = dp[i][j];
        }
      }
    }
    long long int maxvalue = 0, thenweight = 0;

    rep(i, w + 1) {
      // cout << dp[i] << " " << i << " " << thenweight << endl;
      if (maxvalue < dp[n][i]) {
        maxvalue = dp[n][i];
        thenweight = i;
      }
    }

    // printf("Case %d:\\n%lld\\n%lld\\n",a,maxvalue,thenweight);
    cout << "Case " << a << ':' << endl;
    cout << maxvalue << endl;
    cout << thenweight << endl;
  }
  return 0;
}
Problem p00042 (C++) — buggy submission, verdict: Time Limit Exceeded

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

constexpr int MAX = 1000;

int dp[MAX][MAX + 1];
int W, n;
int v[MAX], w[MAX];

int main() {
  int cnt = 0;
  while (1) {
    std::cin >> W;
    if (W == 0)
      break;
    ++cnt;
    std::cin >> n;
    char ch;
    for (int i = 0; i < n; ++i)
      std::cin >> v[i] >> ch >> w[i];
    std::memset(dp, 0, sizeof(dp));
    dp[0][w[0]] = v[0];
    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])
          dp[i][j] = std::max(dp[i][j], dp[i - 1][j - w[i]] + v[i]);
      }
    }
    int ans = 0, maxv;
    for (int i = W; i >= 0; --i) {
      if (ans <= dp[n - 1][i]) {
        ans = dp[n - 1][i];
        maxv = i;
      }
    }
    std::cout << "Case " << cnt << ':' << std::endl
              << ans << std::endl
              << maxv << std::endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

const int MAX_N = 1001;
const int MAX_W = 1001;

int t;
int n, W;
int v[MAX_N], w[MAX_N];

int dp[MAX_N][MAX_W];

int main() {
  while (1) {
    std::cin >> W;
    if (w == 0)
      break;
    std::cin >> n;
    char ch;
    for (int i = 1; i <= n; ++i)
      std::cin >> v[i] >> ch >> w[i];
    for (int i = 1; i <= n; ++i) {
      for (int j = 0; j <= W; ++j)
        dp[i][j] = 0;
    }
    for (int i = 1; i <= n; ++i) {
      for (int j = 0; j <= W; ++j) {
        if (w[i] > j)
          continue;
        dp[i][j] = std::max(dp[i - 1][j - w[i]] + v[i], dp[i][j]);
      }
    }
    int max = 0, maxv;
    for (int i = 0; i <= W; ++i) {
      if (max < dp[n][i]) {
        maxv = i;
        max = dp[n][i];
      }
    }
    ++t;
    std::cout << "Case " << t << ':' << std::endl;
    std::cout << max << std::endl << maxv << std::endl;
  }
  return 0;
}
Problem p00042 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int N_MAX = 1000;
const int W_MAX = 1000;
int c[N_MAX + 1][W_MAX + 1];

int main() {
  int t = 1;
  int w;
  while (cin >> w, w) {
    int n;
    cin >> n;
    vector<int> vs(n + 1), ws(n + 1);
    for (int i = 1; i <= n; i++) {
      scanf("%d,%d", &vs[i], &ws[i]);
    }
    for (int i = 0; i <= n; i++) {
      fill_n(c[i], w + 1, 0);
    }
    int v_max = -1, w_min = 1 << 29;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= w; j++) {
        if (j - ws[i] >= 0) {
          c[i][j] = max(c[i - 1][j], c[i - 1][j - ws[i]] + vs[i]);
          if (v_max < c[i][j]) {
            v_max = c[i][j];
            w_min = j;
          } else if (v_max == c[i][j]) {
            w_min = min(w_min, j);
          }

        } else {
          c[i][j] = c[i - 1][j];
        }
      }
    }
    printf("Case %d:\\n%d\\n%d\\n", t++, v_max, w_min);
  }
}

Error: TLE

Fixed submission:

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

int n, w, maxValue, maxWeight;
const int MAX_N = 1000;
int treasures[MAX_N][2];

void dfs(int index, int value, int weight) {
  if (index == n) {
    if (maxValue < value) {
      maxValue = value;
      maxWeight = weight;
    }
    return;
  }
  if (weight + treasures[index][1] < w) {
    dfs(index + 1, value + treasures[index][0], weight + treasures[index][1]);
  }
  dfs(index + 1, value, weight);
}

int main() {
  int t = 0;
  while (true) {
    cin >> w >> n;
    if (w == 0) {
      break;
    }
    memset(treasures, 0, sizeof(treasures));
    for (int i = 0; i < n; i++) {
      scanf("%d,%d", treasures[i], treasures[i] + 1);
    }
    maxValue = 0;
    dfs(0, 0, 0);
    cout << "Case " << ++t << ":" << endl;
    cout << maxValue << endl << maxWeight << endl;
  }
}
Problem p00042 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, j, k) for (int i = j; i < (k); i++)
#define foreach(T, c, i) for (T::iterator i = c.begin(); i != c.end(); i++)
typedef vector<int> Array;
typedef vector<Array> Mat;
const int INF = 99999999;
int N, W, a, b, ans_num = 0;
const int MAX_N = 1000;
const int MAX_W = 1000;
int dp[MAX_N + 1][MAX_W + 1];
Array v, w;

void solve() {
  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]);
    }
  }
  cout << dp[N][W] << endl;
  int MIN = INF;
  rep(i, N + 1) rep(j, W + 1) {
    if (dp[i][j] == dp[N][W])
      MIN = min(MIN, j);
  }
  cout << MIN << endl;
}

int main() {
  while (1) {
    cin >> W >> N;
    if (W == 0)
      return 0;
    ans_num++;
    memset(dp, 0, sizeof(dp));
    v.clear();
    w.clear();
    rep(i, N) {
      scanf("%d,%d", &a, &b);
      v.pb(a);
      w.pb(b);
    }
    cout << "Case " << ans_num << ":" << endl;
    solve();
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, j, k) for (int i = j; i < (k); i++)
#define foreach(T, c, i) for (T::iterator i = c.begin(); i != c.end(); i++)
typedef vector<int> Array;
typedef vector<Array> Mat;
const int INF = 99999999;
int N, W, a, b, ans_num = 0;
const int MAX_N = 1000;
const int MAX_W = 10000;
int dp[MAX_N + 1][MAX_W + 1];
Array v, w;

void solve() {
  rep(i, N) rep(j, W) {
    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]);
    }
  }
  cout << dp[N][W - 1] << endl;
  int MIN = INF;
  rep(i, N + 1) rep(j, W) {
    if (dp[i][j] == dp[N][W - 1])
      MIN = min(MIN, j);
  }
  cout << MIN << endl;
}

int main() {
  while (1) {
    cin >> W >> N;
    if (W == 0)
      return 0;
    ans_num++;
    memset(dp, 0, sizeof(dp));
    v.clear();
    w.clear();
    rep(i, N) {
      scanf("%d,%d", &a, &b);
      v.pb(a);
      w.pb(b);
    }

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

#include <cstdio>
#include <iostream>

using namespace std;

int main(void) {
  int W, n, v[1000], w[1000], dp[1001][1001], t = 1;

  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 < 1000; i++)
      for (int j = 0; j < 1000; j++)
        dp[i][j] = -1;

    for (int i = 0; i < 1000; i++) {
      dp[0][i] = 0;
    }

    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]);

    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: -11

Fixed submission:

#include <cstdio>
#include <iostream>

using namespace std;

int main(void) {
  int W, n, v[1000], w[1000], dp[10000][10000], t = 1;

  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 < 1000; i++)
      for (int j = 0; j < 1000; j++)
        dp[i][j] = -1;

    for (int i = 0; i < 1000; i++) {
      dp[0][i] = 0;
    }

    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]);

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

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ALL(g) (g).begin(), (g).end()
#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define F(i, j, k) fill(i[0], i[0] + j * j, k)
#define P(p) cout << (p) << endl;
#define INF 1 << 25

typedef long long ll;

int dy[8] = {1, 1, 1, 0, 0, -1, -1, -1};
int dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
struct S {
  int a, b, c;
};
bool asc(const S &left, const S &right) { return left.c > right.c; }

int W, N;
int v[1001], w[1001];
int dp[1001][1001];

int main() {
  int cnt = 1;
  char tmp;
  while (cin >> W) {
    if (W == 0)
      break;
    cin >> N;
    rep(i, N) cin >> v[i] >> tmp >> w[i];
    REP(i, 1, N + 1) {
      rep(j, W + 1) {
        if (w[i - 1] <= j)
          dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i - 1]] + v[i - 1]);
        else
          dp[i][j] = dp[i - 1][j];
      }
    }
    int ans_v = 0, ans_w = 0;
    rep(i, W + 1) {
      if (dp[N][i] > ans_v) {
        ans_v = dp[N][i];
        ans_w = i;
      }
    }
    cout << "Case " << cnt << ":" << endl;
    cout << ans_v << endl;
    cout << ans_w << endl;
    cnt++;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ALL(g) (g).begin(), (g).end()
#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define F(i, j, k) fill(i[0], i[0] + j * j, k)
#define P(p) cout << (p) << endl;
#define INF 1 << 25

typedef long long ll;

int dy[8] = {1, 1, 1, 0, 0, -1, -1, -1};
int dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
struct S {
  int a, b, c;
};
bool asc(const S &left, const S &right) { return left.c > right.c; }

int W, N;
int v[1001], w[1001];
int dp[1001][1001];

int main() {
  int cnt = 1;
  while (1) {
    cin >> W;
    if (W == 0)
      break;
    cin >> N;
    rep(i, N) cin >> v[i] >> w[i];
    REP(i, 1, N + 1) {
      rep(j, W + 1) {
        if (w[i - 1] <= j)
          dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i - 1]] + v[i - 1]);
        else
          dp[i][j] = dp[i - 1][j];
      }
    }
    int ans_v = 0, ans_w = 0;
    rep(i, W + 1) {
      if (dp[N][i] > ans_v) {
        ans_v = dp[N][i];
        ans_w = i;
      }
    }
    cout << "Case " << cnt << endl;
    cout << ans_v << endl;
    cout << ans_w << endl;
    cnt++;
  }
  return 0;
}
Problem p00042 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <stdio.h>
#include <string.h>
int w[1000], v[1000], W, N, dp[2][10000], dpw[2][10000];

bool solve(int num) {
  memset(dp, 0, sizeof(dp));
  memset(dpw, 0, sizeof(dpw));
  scanf("%d", &W);
  if (W == 0)
    return false;
  scanf("%d", &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) & 1][j] = dp[i & 1][j];
        dpw[(i + 1) & 1][j] = dpw[i & 1][j];
      } else {
        int t1 = dp[i & 1][j], t2 = dp[i & 1][j - w[i]] + v[i],
            w1 = dpw[i & 1][j], w2 = dpw[i & 1][j - w[i]] + w[i];
        if (t1 > t2 || (t1 == t2 && w1 < w2)) {
          dp[(i + 1) & 1][j] = t1;
          dpw[(i + 1) & 1][j] = w1;
        } else {
          dp[(i + 1) & 1][j] = t2;
          dpw[(i + 1) & 1][j] = w2;
        }
      }
    }
  printf("Case %d:\\n%d\\n%d\\n", num, dp[N & 1][W], dpw[N & 1][W]);
  return true;
}

int main() {
  for (int i = 0;; i++)
    if (!solve(i + 1))
      return 0;
}

Error: MLE

Fixed submission:

#include <stdio.h>
#include <string.h>
int w[1000], v[1000], W, N, dp[10000][1000], dpw[10000][1000];

bool solve(int num) {
  memset(dp, 0, sizeof(dp));
  memset(dpw, 0, sizeof(dpw));
  scanf("%d", &W);
  if (W == 0)
    return false;
  scanf("%d", &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];
        dpw[i + 1][j] = dpw[i][j];
      } else {
        int t1 = dp[i][j], t2 = dp[i][j - w[i]] + v[i], w1 = dpw[i][j],
            w2 = dpw[i][j - w[i]] + w[i];
        if (t1 > t2 || (t1 == t2 && w1 < w2)) {
          dp[i + 1][j] = t1;
          dpw[i + 1][j] = w1;
        } else {
          dp[i + 1][j] = t2;
          dpw[i + 1][j] = w2;
        }
      }
    }
  printf("Case %d:\\n%d\\n%d\\n", num, dp[N][W], dpw[N][W]);
  return true;
}

int main() {
  for (int i = 0;; i++)
    if (!solve(i + 1))
      return 0;
}
Problem p00042 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cstring>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
const int MAX_N = 1000, MAX_M = 1000;
int n;
typedef pair<int, int> P;
P ilist[MAX_N];
P memo[MAX_N + 1][MAX_M + 1];

P comp(const P &a, const P &b) {
  if (a.first < b.first)
    return b;
  return a;
}

P dp(int now, int lim) {
  if (memo[now][lim].first > -1) {
    return memo[now][lim];
  }
  P res;
  if (now == n)
    return make_pair(0, 0);
  else if (lim < ilist[now].second) {
    res = dp(now + 1, lim);
  } else {
    res = dp(now + 1, lim - ilist[now].second);
    res.second += ilist[now].second;
    res.first += ilist[now].first;
    res = comp(dp(now + 1, lim), res);
  }
  return memo[now][lim] = res;
}

int main() {
  int lim, cnt = 0, w, v;
  P item;
  char buf;
  while (cin >> lim, lim) {
    for (int i = 0; i <= MAX_N; ++i) {
      for (int j = 0; j <= MAX_M; ++j) {
        memo[i][j] = make_pair(-1, -1);
      }
    }
    ++cnt;
    cin >> n;
    for (int i = 0; i < n; ++i) {
      cin >> v >> buf >> w;
      ilist[i] = make_pair(v, w);
    }
    item = dp(0, lim);
    cout << "Case " << cnt << ":" << endl;
    cout << item.first << endl << item.second << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
const int MAX_N = 1000, MAX_M = 10000;
int n;
typedef pair<int, int> P;
P ilist[MAX_N];
P memo[MAX_N + 1][MAX_M + 1];

P comp(const P &a, const P &b) {
  if (a.first < b.first)
    return b;
  return a;
}

P dp(int now, int lim) {
  if (memo[now][lim].first > -1) {
    return memo[now][lim];
  }
  P res;
  if (now == n)
    return make_pair(0, 0);
  else if (lim < ilist[now].second) {
    res = dp(now + 1, lim);
  } else {
    res = dp(now + 1, lim - ilist[now].second);
    res.second += ilist[now].second;
    res.first += ilist[now].first;
    res = comp(dp(now + 1, lim), res);
  }
  return memo[now][lim] = res;
}

int main() {
  int lim, cnt = 0, w, v;
  P item;
  char buf;
  while (cin >> lim, lim) {
    for (int i = 0; i <= MAX_N; ++i) {
      for (int j = 0; j <= MAX_M; ++j) {
        memo[i][j] = make_pair(-1, -1);
      }
    }
    ++cnt;
    cin >> n;
    for (int i = 0; i < n; ++i) {
      cin >> v >> buf >> w;
      ilist[i] = make_pair(v, w);
    }
    item = dp(0, lim);
    cout << "Case " << cnt << ":" << endl;
    cout << item.first << endl << item.second << endl;
  }
  return 0;
}
Problem p00042 (C++) — buggy submission, verdict: Runtime Error

//-AOJ0042 -----------------------------------------------------------//
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>

#define rep(x, to) for (int x = 0; x < to; x++)
#define rep2(x, from, to) for (int x = from; x < to; x++)

struct stk {
  int kt, wg, ct;
  stk(int kt, int wg, int ct) : kt(kt), wg(wg), ct(ct) {}
};
using namespace std;

int main(void) {

  int w, n, cnt = 0;
  char k;

  while (!cin.eof()) {
    cin >> w;
    if (cin.eof() || w == 0)
      break;
    cin.ignore();
    cin >> n;
    vector<vector<int>> dp(n + 1, vector<int>(w + 1, 0));
    vector<int> tk(n, 0);
    vector<int> tw(n, 0);

    cnt++;
    rep(i, n) cin >> tk[i] >> k >> tw[i];

    rep(i, n) rep(j, w + 1) {
      if (tw[i] > j) {
        dp[i + 1][j] = dp[i][j];
      } else {
        dp[i + 1][j] = max(dp[i][j], dp[i][j - tw[i]] + tk[i]);
      }
    }
    //		rep(i,n+1) {rep2(j,1,w+1){
    //			cout << dp[i][j] << "\\t";
    //		} cout <<endl;}

    while (dp[n][w] == dp[n][w - 1]) {
      w--;
    }
    printf("Case %d:\\n%d\\n%d\\n", cnt, dp[n][w], w);
    dp.clear();
    tk.clear();
    tw.clear();
  }

  return 0;
}

Error: 0

Fixed submission:

//-AOJ0042 -----------------------------------------------------------//
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>

#define rep(x, to) for (int x = 0; x < to; x++)
#define rep2(x, from, to) for (int x = from; x < to; x++)

struct stk {
  int kt, wg, ct;
  stk(int kt, int wg, int ct) : kt(kt), wg(wg), ct(ct) {}
};
using namespace std;

int main(void) {

  int w, n, cnt = 0;
  char k;
  vector<vector<int>> dp;
  vector<int> tk;
  vector<int> tw;

  while (!cin.eof()) {
    cin >> w;
    if (cin.eof() || w == 0)
      break;
    cin.ignore();
    cin >> n;
    dp.resize(n + 1, vector<int>(w + 1, 0));
    tk.resize(n, 0);
    tw.resize(n, 0);
    cnt++;
    rep(i, n) cin >> tk[i] >> k >> tw[i];

    rep(i, n) rep(j, w + 1) {
      if (tw[i] > j) {
        dp[i + 1][j] = dp[i][j];
      } else {
        dp[i + 1][j] = max(dp[i][j], dp[i][j - tw[i]] + tk[i]);
      }
    }
    //		rep(i,n+1) {rep2(j,1,w+1){
    //			cout << dp[i][j] << "\\t";
    //		} cout <<endl;}

    while (dp[n][w] == dp[n][w - 1]) {
      w--;
    }

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

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

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define all(v) begin(v), end(v)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, s, n) for (int i = (int)(s); i < (int)(n); i++)

const int inf = 1LL << 55;
const int mod = 1e9 + 7;

signed main() {
  int W, N;
  int v[1001], w[1001];
  for (int t = 1; scanf("%lld", &W), W; t++) {
    scanf("%lld", &N);
    rep(i, N) scanf("%lld,%lld", &v[i], &w[i]);

    int dp[1001] = {0};
    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]);
      }
    }

    int aw = 0;
    while (dp[aw] != dp[W])
      aw++;

    printf("Case %d:\\n%d\\n%d\\n", t, dp[aw], aw);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define all(v) begin(v), end(v)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, s, n) for (int i = (int)(s); i < (int)(n); i++)

const int inf = 1LL << 55;
const int mod = 1e9 + 7;

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  int W, t = 0;
  while (cin >> W, W) {
    t++;
    int N;
    cin >> N;
    int v[1001], w[1001];
    rep(i, N) scanf("%d,%d", &v[i], &w[i]);

    int dp[1001] = {0};
    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]);
      }
    }

    int aw = 0;
    while (dp[aw] != dp[W])
      aw++;

    printf("Case %d:\\n%d\\n%d\\n", t, dp[W], aw);
  }

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

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

int main() {
  int w, n, cnt = 1;
  char dum;
  vector<vector<int>> dp;
  vector<int> weight, value;

  while (cin >> w, w) {
    cin >> n;

    weight.resize(n);
    value.resize(n);

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

    dp.resize(n + 1);
    for (int i = 0; i <= n; ++i) {
      dp[i].resize(w + 1);
    }

    for (int i = 0; i <= w; ++i)
      dp[0][i] = 0;

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

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

    ++cnt;
  }
}

Error: TLE

Fixed submission:

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

int MinW, MaxV;

void Func(int *w, int *v, int n, int i, int limw, int sumw, int sumv) {

  if (sumw > limw)
    return;

  if (sumv > MaxV) {
    MaxV = sumv;
    MinW = sumw;
  }
  if (sumv == MaxV && sumw < MinW) {
    MinW = sumw;
  }
  for (int j = i; j < n; ++j) {
    Func(w, v, n, j + 1, limw, sumw + w[j], sumv + v[j]);
  }
}

int main() {
  int W, N;
  int i, c = 0;
  int *weight, *value;

  while (cin >> W, W) {
    ++c;
    cin >> N;

    MaxV = 0;
    MinW = W;

    weight = new int[N];
    value = new int[N];

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

    Func(weight, value, N, 0, W, 0, 0);

    printf("Case %d:\\n%d\\n%d\\n", c, MaxV, MinW);
  }
}