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

#include <iostream>
using namespace std;

int N = 0;
int W = 0;
int max = 0;
int a[1000] = {0};
int w[1000] = {0};
int v[1000] = {0};
int dp[10000][1000] = {0};

int main(void) {

  cin >> N >> W;

  for (int i = 0; i < N; i++) {
    cin >> v[i] >> w[i];
  }
  for (int i = 1; i < W + 1; i++) {
    for (int j = 0; j < N; j++) {

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

  cout << dp[W][N - 1] << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int N = 0;
int W = 0;
int max = 0;
int a[1000] = {0};
int w[1000] = {0};
int v[1000] = {0};
int dp[1000][1000] = {0};

int main(void) {

  cin >> N >> W;

  for (int i = 0; i < N; i++) {
    cin >> v[i] >> w[i];
  }
  for (int i = 1; i < W + 1; i++) {
    for (int j = 0; j < N; j++) {

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

  cout << dp[W][N - 1] << endl;

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

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

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

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

Error: -11

Fixed submission:

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

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

int rec(int i, int j) {
  if (dp[i][j] >= 0) {
    return dp[i][j];
  }
  int res;
  if (i == n) {
    res = 0;
  } else if (j < w[i]) {
    res = rec(i + 1, j);
  } else {
    res = max(rec(i + 1, j), rec(i + 1, j - w[i]) + v[i]);
  }
  return dp[i][j] = res;
}
int main() {
  scanf("%d %d", &n, &W);
  for (int i = 0; i < n; i++) {
    scanf("%d %d", v[i], w[i]);
  }
  memset(dp, -1, sizeof(dp));
  printf("%d\\n", rec(0, W));
  return 0;
}
Problem p02315 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <math.h>
#include <string>
#include <vector>
using namespace std;
#define INF 10000000;

int partition(int A[], int p, int r) {
  int x = A[r];
  int i = p - 1;
  for (int j = p; j < r; j++) {
    if (x >= A[j]) {
      i = i + 1;
      swap(A[i], A[j]);
    }
  }
  swap(A[i + 1], A[r]);
  return i + 1;
}
void quicksort(int A[], int p, int r) {
  if (p < r) {
    int q = partition(A, p, r);
    quicksort(A, p, q - 1);
    quicksort(A, q + 1, r);
  }
}

int getNumberOfCoin(int C[], int n, int m) {
  int T[50000];
  for (int j = 0; j <= n; j++) {
    T[j] = INF;
  }
  T[0] = 0;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j <= n; j++) {
      if (j - C[i] >= 0) {
        T[j] = min(T[j], T[j - C[i]] + 1);
      }
    }
  }
  return T[n];
}
int knapsack(int w, int n, int v[], int ww[]) {
  int W[101][10001];
  for (int j = 0; j <= w; j++) {
    W[0][j] = 0;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= w; j++) {
      if (j - ww[i] < 0) {
        W[i + 1][j] = W[i][j];
      }
      if (j - ww[i] >= 0) {
        W[i + 1][j] = max(W[i][j], W[i][j - ww[i]] + v[i]);
      }
    }
  }
  return W[n][w];
}
int main() {
  int N, W;
  cin >> N >> W;
  int v[100];
  int w[100];
  for (int i = 0; i < N; i++) {
    cin >> v[i] >> w[i];
  }
  cout << knapsack(W, N, v, w) << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <string>
#include <vector>
using namespace std;
#define INF 10000000;

int partition(int A[], int p, int r) {
  int x = A[r];
  int i = p - 1;
  for (int j = p; j < r; j++) {
    if (x >= A[j]) {
      i = i + 1;
      swap(A[i], A[j]);
    }
  }
  swap(A[i + 1], A[r]);
  return i + 1;
}
void quicksort(int A[], int p, int r) {
  if (p < r) {
    int q = partition(A, p, r);
    quicksort(A, p, q - 1);
    quicksort(A, q + 1, r);
  }
}

int getNumberOfCoin(int C[], int n, int m) {
  int T[50000];
  for (int j = 0; j <= n; j++) {
    T[j] = INF;
  }
  T[0] = 0;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j <= n; j++) {
      if (j - C[i] >= 0) {
        T[j] = min(T[j], T[j - C[i]] + 1);
      }
    }
  }
  return T[n];
}
int knapsack(int w, int n, int v[], int ww[]) {
  int W[100][10000];
  for (int j = 0; j <= w; j++) {
    W[0][j] = 0;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= w; j++) {
      if (j - ww[i] < 0) {
        W[i + 1][j] = W[i][j];
      }
      if (j - ww[i] >= 0) {
        W[i + 1][j] = max(W[i][j], W[i][j - ww[i]] + v[i]);
      }
    }
  }
  return W[n][w];
}
int main() {
  int N, W;
  cin >> N >> W;
  int v[100];
  int w[100];
  for (int i = 0; i < N; i++) {
    cin >> v[i] >> w[i];
  }
  cout << knapsack(W, N, v, w) << endl;
}
Problem p02315 (C++) — buggy submission, verdict: Time Limit Exceeded

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

class Knapsack {
public:
  int *weight;
  int *value;
  int U;
  int N;
  int **memo;
  bool **done;

  Knapsack();
  ~Knapsack();

  int full_search(int i, int U);
  int Dynamic_Programming(int i, int U);
};

Knapsack::Knapsack() {
  cin >> N >> U;
  weight = new int[N];
  value = new int[N];
  for (int i = 0; i < N; i++) {
    cin >> value[i] >> weight[i];
  }
  memo = new int *[N];
  done = new bool *[N];
  for (int i = 0; i < N; i++) {
    memo[i] = new int[U + 1];
    done[i] = new bool[U + 1];
  }
}

Knapsack::~Knapsack() {
  delete[] weight;
  delete[] value;
  for (int i = 0; i < N; i++) {
    delete[] memo[i];
    delete[] done[i];
  }
  delete[] memo;
  delete[] done;
}

int Knapsack::full_search(int i, int U) {
  if (i == N)
    return 0;
  else if (U < weight[i])
    return full_search(i + 1, U);
  else {
    int res1 = full_search(i + 1, U);
    int res2 = full_search(i + 1, U - weight[i]) + value[i];
    return max(res1, res2);
  }
}

int Knapsack::Dynamic_Programming(int i, int U) {
  if (i == N)
    return 0;
  if (done[i][U])
    return memo[i][U];
  int res;
  if (U < weight[i])
    return Dynamic_Programming(i + 1, U);

  int res1 = Dynamic_Programming(i + 1, U);
  int res2 = Dynamic_Programming(i + 1, U - weight[i]) + value[i];
  res = max(res1, res2);

  done[i][U] = true;
  memo[i][U] = res;
  return res;
}

int main() {
  Knapsack *solve = new Knapsack;
  cout << solve->Dynamic_Programming(0, solve->U) << endl;
  delete solve;
}

Error: TLE

Fixed submission:

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

class Knapsack {
public:
  int *weight;
  int *value;
  int U;
  int N;
  int **memo;
  bool **done;

  Knapsack();
  ~Knapsack();

  int full_search(int i, int U);
  int Dynamic_Programming(int i, int U);
};

Knapsack::Knapsack() {
  cin >> N >> U;
  weight = new int[N];
  value = new int[N];
  for (int i = 0; i < N; i++) {
    cin >> value[i] >> weight[i];
  }
  memo = new int *[N];
  done = new bool *[N];
  for (int i; i < N; i++) {
    memo[i] = new int[U + 1];
    done[i] = new bool[U + 1];
  }
}

Knapsack::~Knapsack() {
  delete[] weight;
  delete[] value;
  for (int i; i < N; i++) {
    delete[] memo[i];
    delete[] done[i];
  }
  delete[] memo;
  delete[] done;
}

int Knapsack::full_search(int i, int U) {
  if (i == N)
    return 0;
  else if (U < weight[i])
    return full_search(i + 1, U);
  else {
    int res1 = full_search(i + 1, U);
    int res2 = full_search(i + 1, U - weight[i]) + value[i];
    return max(res1, res2);
  }
}

int Knapsack::Dynamic_Programming(int i, int U) {
  if (done[i][U])
    return memo[i][U];
  int res;

  if (i == N)
    return 0;
  else if (U < weight[i])
    return full_search(i + 1, U);
  else {
    int res1 = full_search(i + 1, U);
    int res2 = full_search(i + 1, U - weight[i]) + value[i];
    res = max(res1, res2);
  }

  done[i][U];
  memo[i][U] = res;
  return res;
}

int main() {
  Knapsack *solve = new Knapsack;
  cout << solve->Dynamic_Programming(0, solve->U) << endl;
  delete solve;
}
Problem p02315 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int n, W, a, b;
vector<int> v;
vector<int> w;

int memo[101][10001];
int fun(int i, int sw) {
  int temp;
  if (memo[i][sw] >= 0)
    return memo[i][sw];
  if (i == n) {
    temp = 0;
  } else if (w[i] > sw) {
    temp = fun(i + 1, sw);
  } else {
    temp = max(fun(i + 1, sw), fun(i + 1, sw - w[i]) + v[i]);
  }
  return memo[i][sw] = temp;
}

int main() {
  cin >> n >> W;
  for (int i = 0; i < n; ++i) {
    cin >> a >> b;
    w.push_back(b);
    v.push_back(a);
  }
  for (int i = 0; i < n + 1; ++i) {
    for (int j = 0; j < W + 1; ++j) {
      memo[i][j] = -1;
    }
  }
  cout << fun(0, W) << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int n, W, a, b;
vector<int> v;
vector<int> w;

int memo[100][10000];
int fun(int i, int sw) {
  int temp;
  if (memo[i][sw] >= 0)
    return memo[i][sw];
  if (i == n) {
    temp = 0;
  } else if (w[i] > sw) {
    temp = fun(i + 1, sw);
  } else {
    temp = max(fun(i + 1, sw), fun(i + 1, sw - w[i]) + v[i]);
  }
  return memo[i][sw] = temp;
}

int main() {
  cin >> n >> W;
  for (int i = 0; i < n; ++i) {
    cin >> a >> b;
    w.push_back(b);
    v.push_back(a);
  }
  for (int i = 0; i < n + 1; ++i) {
    for (int j = 0; j < W + 1; ++j) {
      memo[i][j] = -1;
    }
  }
  cout << fun(0, W) << endl;
  return 0;
}
Problem p02315 (C++) — buggy submission, verdict: Time Limit Exceeded

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

// ??\\???
int n, W;
int weight[110], value[110];

// DP????????????
int dp[110][10010];

int main() {
  cin >> n >> W;
  for (int i = 0; i < n; ++i)
    cin >> value[i] >> weight[i];

  // DP???????????¶: dp[0][w] = 0
  for (int w = 0; w <= W; ++w)
    dp[0][w] = 0;

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

  cout << dp[n][W] << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int dfs(int a, int j, int i, vector<int> &w, vector<int> &v) {
  if (i == a)
    return 0;
  else if (j < w[i])
    return dfs(a, j, i + 1, w, v);
  else
    return max(dfs(a, j, i + 1, w, v), dfs(a, j - w[i], i + 1, w, v) + v[i]);
}

int main(void) {
  int a, b;
  cin >> a >> b;
  vector<int> w(a, 0), v(a, 0);
  for (int i = 0; i < a; i++) {
    cin >> v[i] >> w[i];
  }

  int ans = dfs(a, b, 0, w, v);
  cout << ans << endl;
}
Problem p02315 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define all(a) a.begin(), a.end()
typedef long long ll;

using P = pair<ll, ll>;
#define v first
#define w second

// v高い順（vが同じならw軽い方優先）
bool comp(P a, P b) { return a.v == b.v ? a.w < b.w : a.v > b.v; }

int main(void) {
  ll N, W;
  cin >> N >> W;
  vector<P> a(N);
  rep(i, N) {
    ll v, w;
    cin >> v >> w;
    a[i] = {v, w};
  }
  vector<P> dp = {{0, 0}};

  rep(i, N) {
    vector<P> tmp;
    for (P s : dp) {
      tmp.push_back(s);
      tmp.push_back({s.v + a[i].v, s.w + a[i].w});
    }

    sort(all(tmp), comp);
    dp.clear();
    ll w_border = W + 1;
    for (P s : tmp) {
      if (w_border > s.w) {
        w_border = s.w;
        dp.push_back(s);
      }
    }
    assert((ll)dp.size() <= N * N * 2);
  }
  cout << dp[0].v << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define all(a) a.begin(), a.end()
typedef long long ll;

using P = pair<ll, ll>;
#define v first
#define w second

// v高い順（vが同じならw軽い方優先）
bool comp(P a, P b) { return a.v == b.v ? a.w < b.w : a.v > b.v; }

int main(void) {
  ll N, W;
  cin >> N >> W;
  vector<P> a(N);
  rep(i, N) {
    ll v, w;
    cin >> v >> w;
    a[i] = {v, w};
  }
  vector<P> dp = {{0, 0}};

  rep(i, N) {
    vector<P> tmp;
    for (P s : dp) {
      tmp.push_back(s);
      tmp.push_back({s.v + a[i].v, s.w + a[i].w});
    }

    sort(all(tmp), comp);
    dp.clear();
    ll w_border = W + 1;
    for (P s : tmp) {
      if (w_border > s.w) {
        w_border = s.w;
        dp.push_back(s);
      }
    }
    assert((ll)dp.size() <= N * N + 1);
  }
  cout << dp[0].v << endl;
  return 0;
}
Problem p02315 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define all(a) a.begin(), a.end()
typedef long long ll;

using P = pair<ll, ll>;
#define v first
#define w second

// v高い順（vが同じならw軽い方優先）
bool comp(P a, P b) { return a.v == b.v ? a.w < b.w : a.v > b.v; }

int main(void) {
  int N, W;
  cin >> N >> W;
  vector<P> a(N);
  rep(i, N) {
    ll v, w;
    cin >> v >> w;
    a[i] = {v, w};
  }
  vector<P> dp = {{0, 0}};

  rep(i, N) {
    vector<P> tmp;
    for (auto &s : dp) {
      tmp.push_back(s);
      tmp.push_back({s.v + a[i].v, s.w + a[i].w});
    }

    sort(all(tmp), comp);
    dp.clear();
    ll w_border = W + 1;
    for (auto &s : tmp) {
      if (w_border > s.w) {
        w_border = s.w;
        dp.push_back(s);
      }
    }
  }
  cout << dp[0].v << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define all(a) a.begin(), a.end()
typedef long long ll;

using P = pair<ll, ll>;
#define v first
#define w second

// v高い順（vが同じならw軽い方優先）
bool comp(P a, P b) { return a.v == b.v ? a.w < b.w : a.v > b.v; }

int main(void) {
  int N, W;
  cin >> N >> W;
  vector<P> a(N);
  rep(i, N) {
    ll v, w;
    cin >> v >> w;
    a[i] = {v, w};
  }
  vector<P> dp = {{0, 0}};

  rep(i, N) {
    vector<P> tmp;
    for (auto &s : dp) {
      tmp.push_back(s);
      tmp.push_back({s.v + a[i].v, s.w + a[i].w});
    }

    sort(all(tmp), comp);
    dp.clear();
    ll w_border = W + 1;
    for (auto &s : tmp) {
      if (w_border > s.w) {
        w_border = s.w;
        dp.push_back(s);
      }
    }
    assert(dp.size() <= N * N);
  }
  cout << dp[0].v << endl;
  return 0;
}
Problem p02315 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <climits>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define loop(i, n) for (int i = 0; i < (n); i++)
#define loops(i, f, n) for (int i = (f); i < (n); i++)
#define VEC vector<int>
#define INF INT_MAX
#define debug(a) cout << a
// vector<VEC> v (n, VEC(n))

int main() {
  int n, W;
  cin >> n >> W;
  VEC w(n), v(n);
  loop(i, n) cin >> v[i] >> w[i];
  int dp[101][10001];
  loop(i, 101) loop(j, 10001) dp[i][j] = -1;
  dp[0][0] = 0;
  loop(i, n) {
    loop(j, W + 1) {
      if (dp[i][j] >= 0)
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
      if (j + w[i] <= W)
        dp[i + 1][j + w[i]] = max(dp[i + 1][j + w[i]], dp[i][j] + v[i]);
    }
  }
  int ans = -1;
  loop(j, W + 1) ans = max(ans, dp[n][j]);
  printf("%d\\n", ans);
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <climits>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define loop(i, n) for (int i = 0; i < (n); i++)
#define loops(i, f, n) for (int i = (f); i < (n); i++)
#define VEC vector<int>
#define INF INT_MAX
#define debug(a) cout << a
// vector<VEC> v (n, VEC(n))

int main() {
  int n, W;
  cin >> n >> W;
  VEC w(n), v(n);
  loop(i, n) cin >> v[i] >> w[i];
  int dp[100][10000];
  loop(i, 101) loop(j, 10001) dp[i][j] = -1;
  dp[0][0] = 0;
  loop(i, n) {
    loop(j, W + 1) {
      if (dp[i][j] >= 0)
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
      if (j + w[i] <= W)
        dp[i + 1][j + w[i]] = max(dp[i + 1][j + w[i]], dp[i][j] + v[i]);
    }
  }
  int ans = -1;
  loop(j, W + 1) ans = max(ans, dp[n][j]);
  printf("%d", ans);
  return 0;
}
Problem p02315 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define int long long
#define INF 1 << 25
using namespace std;

int N, W;
int ans;
int v[1010], w[1010];
int mem[10100][10100];

int dfs(int n, int h) {
  if (n == 0) {
    ans = 0;
  } else if (mem[n][h] != 0)
    ans = mem[n][h];
  else if (w[n] > h)
    ans = dfs(n - 1, h);
  else {
    ans = max(dfs(n - 1, h), dfs(n - 1, h - w[n]) + v[n]);
  }
  return mem[n][h] = ans;
}

signed main() {
  cin >> N >> W;
  for (int i = 1; i <= N; i++) {
    cin >> v[i] >> w[i];
  }
  cout << dfs(N, W) << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long
static const int INF = 1 << 25;

using namespace std;

int N, W;
int ans;
int v[1010], w[1010];
int dfs(int n, int h) {
  if (n == 0)
    ans = 0;
  else if (h < w[n])
    ans = dfs(n - 1, h);
  else
    ans = max(dfs(n - 1, h), dfs(n - 1, h - w[n]) + v[n]);
  return ans;
}

signed main() {
  cin >> N >> W;
  for (int i = 1; i <= N; i++) {
    cin >> v[i] >> w[i];
  }
  cout << dfs(N, W) << endl;
  return 0;
}
Problem p02315 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repc(i, s, e) for (int i = (s); i < (e); i++)
#define pb(n) push_back((n))
#define mp(n, m) make_pair((n), (m))
#define all(r) r.begin(), r.end()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 1e9;

const int MAX_N = 110, MAX_W = 10010;
int dp[MAX_W];

int main() {
  int n, W;
  cin >> n >> W;
  vi v(n, 0), w(n, 0);
  rep(i, n) cin >> 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]);
    }
  }
  cout << dp[W] << endl;
}

Error: 0

Fixed submission:

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

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repc(i, s, e) for (int i = (s); i < (e); i++)
#define pb(n) push_back((n))
#define mp(n, m) make_pair((n), (m))
#define all(r) r.begin(), r.end()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 1e9;

const int MAX_N = 110, MAX_W = 1010;
int dp[MAX_W];

int main() {
  int n, W;
  cin >> n >> W;
  vi v(n, 0), w(n, 0);
  rep(i, n) cin >> 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]);
    }
  }
  cout << dp[W] << endl;
}
Problem p02315 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 1; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define VI vector<int>
#define VS vector<string>
#define all(a) (a).begin(), (a).end()
#define debug(x) cout << #x << ": " << x << endl
using namespace std;
typedef pair<int, int> P;

const int INF = 1000000;
char field[101][101];
int d[101][101];
int N, M;
int sx, sy;
int gx, gy;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

const int MAX_N = 1000;

int main() {

  int n, w;

  cin >> n >> w;

  vector<int> c(n);
  vector<int> we(n);
  vector<vector<int>> dp(n + 1, vector<int>(w + 1));

  rep(i, n) cin >> c[i] >> we[i];

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

  cout << dp[0][w] << endl;
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int main() {

  int W, N;
  int k = 1;
  while (cin >> W, W != 0) {
    cin >> N;
    char comma;
    vector<int> c(N);
    vector<int> weight(N);
    vector<vector<int>> dp(N + 1, vector<int>(W + 1));
    vector<vector<int>> m(N + 1, vector<int>(W + 1));
    for (int i = 0; i < N; i++) {
      cin >> c[i] >> comma >> weight[i];
    }

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

    for (int i = N - 1; i >= 0; 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], dp[i + 1][j - weight[i]] + c[i]);
        }
      }
    }
    cout << "Case " << k++ << ":" << endl;
    cout << dp[0][W] << endl;
    for (int i = W; i >= 0; i--) {
      if (dp[0][W] != dp[0][i]) {
        cout << i + 1 << endl;
        break;
      }
    }
  }
}
Problem p02315 (C++) — buggy submission, verdict: Runtime Error

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

int C[105][10005];
struct items {
  int value, weight;
};

int main() {
  int N, W;
  int a, b;
  items item[105];
  cin >> N >> W;

  for (int i = 1; i <= N; i++) {
    cin >> a >> b;
    item[i].value = a;
    item[i].weight = b;
  }

  for (int i = 0; i <= N; i++) {
    C[i][0] = 0;
  }

  for (int i = 0; i <= W; i++) {
    C[0][i] = 0;
  }

  for (int i = 1; i <= N; i++) {
    for (int w = 1; w <= W; w++) {
      C[i][w] = C[i - 1][w];
      if (item[i].weight > w)
        continue;
      C[i][w] = max(C[i - 1][w - item[i].weight] + item[i].value, C[i - 1][w]);
    }
  }

  cout << C[N][W] << endl;
}

Error: -11

Fixed submission:

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

int C[105][10005];
struct items {
  int value, weight;
};

int main() {
  int N, W;
  int a, b;
  vector<items> item;
  cin >> N >> W;

  for (int i = 0; i <= N; i++) {
    C[i][0] = 0;
  }

  for (int i = 0; i <= W; i++) {
    C[0][i] = 0;
  }

  for (int i = 0; i <= N; i++) {
    cin >> a >> b;
    item[i].value = a;
    item[i].weight = b;
  }

  for (int i = 1; i <= N; i++) {
    for (int w = 1; w <= W; w++) {
      C[i][w] = C[i - 1][w];
      if (item[i].value > w)
        continue;
      C[i][w] = max(C[i - 1][w - item[i].weight] + item[i].value, C[i - 1][w]);
    }
  }

  cout << C[N][W] << endl;
}