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

#include <algorithm>
#include <iostream>
#include <string.h>
using namespace std;
const int max_n = 100;
const int max_w = 10000;

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

int main(void) {
  cin >> n >> W;
  for (int i = 0; i < n; i++) {
    cin >> v[i] >> w[i];
  }
  for (int i = n - 1; i >= 0; i--) {
    for (int j = 0; j <= W; j++) {
      if (j < w[i]) {
        dp[i][j] = dp[i + 1][j];
      } else {
        dp[i][j] = max(dp[i + 1][j], dp[i + 1][j - w[i]] + v[i]);
      }
    }
  }
  cout << dp[0][W] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
const int max_n = 100;

int n, W;
int w[max_n], v[max_n];

int rec(int i, int j) {
  int ans;
  if (i == n) {
    ans = 0;
  } else if (j < w[i]) {
    ans = rec(i + 1, j);
  } else {
    ans = max(rec(i + 1, j), rec(i + 1, j - w[i]) + v[i]);
  }
  return ans;
}

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

#include <bits/stdc++.h>
using namespace std;
int dp[111][11111], N, W, c[111], d[111], bnu;
int kei(int i, int j) {
  if (dp[i][j])
    return dp[i][j];
  if (i == N + 1) {
    return 0;
  }
  if (j < d[i]) {
    return kei(i + 1, j);
  }
  return dp[i][j] = bnu = max(kei(i + 1, j), kei(i + 1, j - d[i]) + c[i]);
}
int main() {
  cin >> N >> W;
  for (int i = 1; i < N + 1; i++)
    cin >> c[i] >> d[i];
  cout << kei(0, W) << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int dp[101][10000], N, W, kj, c[10000], d[10000], bnu;
int kei(int i, int j) {
  if (dp[i][j] != -1)
    return dp[i][j];
  if (i == N + 1) {
    bnu = 0;
  } else if (j < d[i]) {
    bnu = kei(i + 1, j);
  } else {
    bnu = max(kei(i + 1, j), kei(i + 1, j - d[i]) + c[i]);
  }
  return dp[i][j] = bnu;
}
int main() {
  cin >> N >> W;
  for (int i = 1; i < N + 1; i++)
    cin >> c[i] >> d[i];
  for (int i = 0; i < 101; i++)
    fill(dp[i], dp[i] + 10000, -1);
  cout << kei(0, W) << endl;
}
Problem p02315 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
using namespace std;
int item_list[100][2];
int solution_list[101][10001];
int select(int size, int weight_a) {
  if (solution_list[size][weight_a] == -1) {
    if (size < 0 || weight_a < 0) {
      return 0;
    }
    if (size == 0 || weight_a == 0) {
      solution_list[size][weight_a] = 0;
      return 0;
    }
    if (item_list[size - 1][1] > weight_a) {
      solution_list[size][weight_a] = select(size - 1, weight_a);
      return solution_list[size][weight_a];
    } else {
      int a, b;
      a = item_list[size - 1][0] +
          select(size - 1, weight_a - item_list[size - 1][1]);
      b = select(size - 1, weight_a);
      solution_list[size][weight_a] = (a > b) ? a : b;
      return solution_list[size][weight_a];
    }
  } else {
    return solution_list[size][weight_a];
  }
}

int main() {
  fill(&solution_list[0][0], &solution_list[100][10001], -1);
  int N, W;
  scanf("%d%d", &N, &W);
  for (int i = 0; i < N; i++) {
    scanf("%d%d", &item_list[i][0], &item_list[i][1]);
  }
  printf("%d\\n", select(N, W));
  return 0;
}


Error: TLE

Fixed submission:

#include <cstdio>
using namespace std;
int item_list[100][2];

int select(int size, int weight_a) {

  if (size <= 0 || weight_a <= 0)
    return 0;
  if (item_list[size - 1][1] > weight_a)
    return select(size - 1, weight_a);
  int a, b;
  a = item_list[size - 1][0] +
      select(size - 1, weight_a - item_list[size - 1][1]);
  b = select(size - 1, weight_a);
  return (a > b) ? a : b;
}

int main() {
  int N, W;
  scanf("%d%d", &N, &W);
  for (int i = 0; i < N; i++) {
    scanf("%d%d", &item_list[i][0], &item_list[i][1]);
  }
  printf("%d\\n", select(N, W));
  return 0;
}
Problem p02315 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

int DP[10001];

// first:value, second:weight
pair<int, int> goods[101];

int main() {

  int N, W, ans = 0;
  scanf("%d", &N);
  scanf("%d", &W);

  for (int i = 0; i < N; i++) {
    scanf("%d %d", &goods[i].first, &goods[i].second);
  }

  fill(DP, DP + 10001, -1);
  DP[0] = 0;
  /*
  for (int i = 0; i < N; i++) {
      for (int j = 0; j + goods[i].second <= W; j++) {
          if (DP[j] != -1){
              DP[j + goods[i].second] = max(DP[j + goods[i].second], DP[j] +
  goods[i].first);
          }
      }
  }*/

  for (int i = 0; i < N; i++) {
    for (int j = W; j >= 0; j--) {
      if (DP[j] != -1 && j + goods[i].second <= W) {
        DP[j + goods[i].second] =
            max(DP[j + goods[i].second], DP[j] + goods[i].first);
      }
    }
  }

  for (int i = 0; i <= W; i++) {
    ans = max(ans, DP[i]);
  }
  cout << ans << endl;

  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

int DP[10001];

// first:value, second:weight
pair<int, int> goods[101];

int main() {

  int N, W, ans = 0;
  scanf("%d", &N);
  scanf("%d", &W);

  for (int i = 0; i < N; i++) {
    scanf("%d %d", &goods[i].first, &goods[i].second);
  }

  fill(DP, DP + 50001, -1);
  DP[0] = 0;

  for (int i = 0; i < N; i++) {
    for (int j = 0; j + goods[i].second <= W; j++) {
      if (DP[j + goods[i].second] != -1) {
        DP[j + goods[i].second] =
            max(DP[j + goods[i].second], DP[j] + goods[i].first);
      }
    }
  }

  for (int i = 0; i <= W; i++) {
    ans = max(ans, DP[i]);
  }
  cout << ans << endl;

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

#include <bits/stdc++.h>
using namespace std;
int v[1001];
int w[1001];
int dp[101][10001];
int N, W;
int solve(int a, int b);
int main() {
  cin >> N >> W;
  for (int i = 0; i < N; i++) {
    cin >> v[i] >> w[i];
  }
  cout << solve(0, W) << endl;
}
int solve(int a, int b) {
  if (dp[a][b] != 0)
    return dp[a][b];
  if (a == N) {
    return dp[a][b] = 0;
  } else if (b - w[a] < 0) {
    return dp[a][b] = solve(a + 1, b);
  } else {
    return dp[a][b] = max(solve(a + 1, b), solve(a + 1, b - w[a]) + v[a]);
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int v[1001];
int w[1001];
int N, W;
int solve(int a, int b);
int main() {
  cin >> N >> W;
  for (int i = 0; i < N; i++) {
    cin >> v[i] >> w[i];
  }
  cout << solve(0, W) << endl;
}
int solve(int a, int b) {
  if (a == N) {
    return 0;
  } else if (b - w[a] < 0) {
    return solve(a + 1, b);
  } else {
    return max(solve(a + 1, b), solve(a + 1, b - w[a]) + v[a]);
  }
}
Problem p02315 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

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

  int dp[100000] = {};
  int dpd[100000] = {};
  for (int t = 0; t < n; t++) {
    for (int i = 1; i <= w; i++) {
      if (i - b[t] >= 0) {
        dp[i] = max(dpd[i], dpd[i - b[t]] + a[t]);
      }
    }
    for (int i = 0; i <= w; i++) {
      dpd[i] = dp[i];
    }
  }

  cout << dp[w] << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

int a[10001], b[10001];

int search(int n, int w) {
  int c = 0;
  if (w == 0) {
    return 0;
  }
  if (n == 0) {
    if (w - b[0] >= 0) {
      return a[0];
    } else {
      return 0;
    }
  }
  if (w - b[n] >= 0) {
    c = a[n] + search(n - 1, w - b[n]);
  }
  c = max(c, search(n - 1, w));
  return c;
}

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

  cout << search(n - 1, w) << endl;

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
#define int long long
const int inf = 999999999999999999;
typedef long long ll;
typedef long double ld;
struct A {
  int v, w;
};
A a[100];
int dp[10001];
signed main() {
  int n, w, ans = 0;
  cin >> n >> w;
  for (int i = 0; i < n; i++)
    cin >> a[i].v >> a[i].w;
  fill(dp, dp + 10001, -1);
  dp[0] = 0;
  for (int i = 0; i < n; i++)
    for (int j = 10000; j >= 0; j--)
      if (dp[j] != -1 && j + a[i].w <= 10000)
        dp[j + a[i].w] = max(dp[j + a[i].w], dp[j] + a[i].v);
  for (int i = 0; i <= w; i++)
    ans = max(ans, dp[i]);
  cout << ans << endl;
  getchar();
  getchar();
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
#define int long long
const int inf = 999999999999999999;
typedef long long ll;
typedef long double ld;
struct A {
  int v, w;
};
A a[100];
int dp[10001];
signed main() {
  int n, w, ans = 0;
  cin >> n >> w;
  for (int i = 0; i < n; i++)
    cin >> a[i].v >> a[i].w;
  fill(dp, dp + 10001, -1);
  dp[0] = 0;
  for (int i = 0; i < n; i++)
    for (int j = 10000; j >= 0; j--)
      if (dp[j] != -1)
        dp[j + a[i].w] = max(dp[j + a[i].w], dp[j] + a[i].v);
  for (int i = 0; i <= w; i++)
    ans = max(ans, dp[i]);
  cout << ans << endl;
  getchar();
  getchar();
}
Problem p02315 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

struct goods {
  int weit;
  int value;
};
goods g[100];

int n, w;
int dtp[101][10001];
int rec(int now_g, int capa) {
  if (dtp[now_g][capa] != -1) {
    return dtp[now_g][capa];
  }

  int total_val;
  if (now_g == n) {
    total_val = 0;
  } else if (capa < g[now_g].weit) {
    total_val = rec(now_g + 1, capa);
  } else {
    total_val = max(rec(now_g + 1, capa),
                    rec(now_g + 1, capa - g[now_g].weit) + g[now_g].value);
  }
  return dtp[now_g][capa] = total_val;
}

int main(void) {
  cin >> n >> w;
  for (int i = 0; i < n; i++) {
    cin >> g[i].value >> g[i].weit;
  }
  // int dp[n][w];
  for (int k = 0; k < 101; k++) {
    for (int i = 0; i < 10001; i++) {
      dtp[k][i] = -1;
    }
  }
  // for(int i=0;i<w;i++){
  //   dtp[i]=&(*dp)[i];
  // }
  cout << rec(0, w) << endl;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

struct goods {
  int weit;
  int value;
};
goods g[100];
int n, w;

int rec(int now_g, int capa) {
  int total_val;
  if (now_g == n) {
    total_val = 0;
  } else if (capa < g[now_g].weit) {
    total_val = rec(now_g + 1, capa);
  } else {
    total_val = max(rec(now_g + 1, capa),
                    rec(now_g + 1, capa - g[now_g].weit) + g[now_g].value);
  }
  return total_val;
}

int main(void) {
  cin >> n >> w;
  for (int i = 0; i < n; i++) {
    cin >> g[i].value >> g[i].weit;
  }
  cout << rec(0, w) << endl;
}
Problem p02315 (C++) — buggy submission, verdict: Runtime Error

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

#define rep(i, n) REP(i, 0, n)
#define REP(i, k, n) for (int i = k; i < n; i++)
#define mmax(start, end) *max_element(start, end)
#define mmin(start, end) *min_element(start, end)
#define argmax max_element
#define argmin min_element
// #define push push_back
// #define pop pop_back
#define ALL(x) (x).begin(), (x).end()

typedef long long ll;
typedef pair<int, int> prii;
// typedef string str;

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

template <class T = ll> T in() {
  T _in;
  cin >> _in;
  return _in;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, W;
  cin >> N >> W;

  int v[101], w[101];
  REP(i, 1, N + 1) { cin >> v[i] >> w[i]; }

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

  // rep(i, W+1) {
  //     rep(j, N+1) {
  //         cout << dp[i][j] << " ";
  //     } cout << endl;
  // } cout << endl;

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

  return 0;
}


Error: 0

Fixed submission:

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

#define rep(i, n) REP(i, 0, n)
#define REP(i, k, n) for (int i = k; i < n; i++)
#define mmax(start, end) *max_element(start, end)
#define mmin(start, end) *min_element(start, end)
#define argmax max_element
#define argmin min_element
// #define push push_back
// #define pop pop_back
#define ALL(x) (x).begin(), (x).end()

typedef long long ll;
typedef pair<int, int> prii;
// typedef string str;

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

template <class T = ll> T in() {
  T _in;
  cin >> _in;
  return _in;
}

int dp[1001][1001];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, W;
  cin >> N >> W;

  int v[101], w[101];
  REP(i, 1, N + 1) { cin >> v[i] >> w[i]; }

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

  // rep(i, W+1) {
  //     rep(j, N+1) {
  //         cout << dp[i][j] << " ";
  //     } cout << endl;
  // } cout << endl;

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

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

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

using ll = long long;
#define int ll

#define FOR(i, a, b) for (int i = int(a); i < int(b); i++)
#define REP(i, b) FOR(i, 0, b)

int N, W;
vector<int> v, w;
vector<vector<int>> dp; //?????¢???
int read() {
  int i;
  scanf("%lld", &i);
  return i;
}

int rec(int i, int j) {
  int retval;
  if (dp[i][j] != -1)
    return dp[i][j];
  if (i == N) {
    retval = 0;
  } else if (j < w[i]) {
    retval = rec(i + 1, j);
  } else {
    retval = max(v[i] + rec(i + 1, j - w[i]), rec(i + 1, j));
  }

  return dp[i][j] = retval;
}

signed main() {
  // your code goes here
  N = read(), W = read();
  v.resize(N);
  w.resize(N);
  dp.resize(N + 1);
  REP(i, N + 1) dp[i].resize(W + 1, -1);
  REP(i, N) {
    v[i] = read();
    w[i] = read();
  }

  cout << rec(0, W) << endl;
}

Error: TLE

Fixed submission:

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

using ll = long long;
#define int ll

#define FOR(i, a, b) for (int i = int(a); i < int(b); i++)
#define REP(i, b) FOR(i, 0, b)

int N, W;
vector<int> v, w;

int read() {
  int i;
  scanf("%lld", &i);
  return i;
}

int rec(int i, int j) {
  int retval;

  if (i == N) {
    retval = 0;
  } else if (j < w[i]) {
    retval = rec(i + 1, j);
  } else {
    retval = max(v[i] + rec(i + 1, j - w[i]), rec(i + 1, j));
  }

  return retval;
}

signed main() {
  // your code goes here
  N = read(), W = read();
  v.resize(N);
  w.resize(N);
  REP(i, N) {
    v[i] = read();
    w[i] = read();
  }

  cout << rec(0, W) << endl;
}
Problem p02315 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int V[101][10001];
int v[101], w[101];
int main() {
  int N, W;
  cin >> N >> W;

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

  for (int i = 0; i < 101; i++)
    V[i][0] = 0;
  for (int i = 0; i < 10001; i++)
    V[0][i] = 0;

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

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int v[101], w[101];
int value(int i, int c) {
  if (i <= 0 || c <= 0)
    return 0;
  else if ((c - w[i]) < 0)
    return value(i - 1, c);
  else
    return max(v[i] + value(i - 1, c - w[i]), value(i - 1, c));
};

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

  cout << value(N, W) << endl;
}
Problem p02315 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int N, W;
  cin >> N >> W;

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

  vector<vector<int>> DP(N);
  for (int i = 0; i < N; i++) {
    DP[i].resize(W + 1);
    fill(DP[i].begin(), DP[i].end(), 0);
  }

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

  int maxVal = 0;
  for (int i = 0; i <= W; i++) {
    maxVal = max(DP[N - 1][i], maxVal);
  }
  cout << maxVal << endl;

  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  int N, W;
  cin >> N >> W;

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

  vector<vector<int>> DP(N);
  for (int i = 0; i < N; i++) {
    DP[i].resize(W + 1);
    fill(DP[i].begin(), DP[i].end(), 0);
  }

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

  int maxVal = 0;
  for (int i = 0; i <= W; i++) {
    maxVal = max(DP[N - 1][i], maxVal);
  }
  cout << maxVal << endl;

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

//
//  DPL_1_B.cpp
//
//
//  Created by ?±±?´? ??? on 2015/08/29.
//
//

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int calc(int, int);

vector<int> v, w;
int N;
vector<vector<int>> memo;

int main() {
  int W;

  cin >> N >> W;

  v.resize(N);
  w.resize(N);
  memo.resize(N + 1, vector<int>(10001));

  for (int i = 0; i < N + 1; i++) {
    for (int j = 0; j < 10001; j++) {
      memo[i][j] = -1;
    }
  }

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

  cout << calc(0, W) << endl;

  return 0;
}

int calc(int i, int j) {
  if (memo[i][j] != -1) {
    return memo[i][j];
  }

  int res;

  if (i > N - 1) {
    res = 0;
  } else if (j < w[i]) {
    res = calc(i + 1, j);
  } else {
    res = (int)max(calc(i + 1, j), calc(i + 1, j - w[i]) + v[i]);
  }

  memo[i][j] = res;
  return res;
}

Error: TLE

Fixed submission:

//
//  DPL_1_B.cpp
//
//
//  Created by ?±±?´? ??? on 2015/08/29.
//
//

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int calc(int, int);

vector<int> v, w;
int N;

int main() {
  int W;

  cin >> N >> W;

  v.resize(N);
  w.resize(N);

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

  cout << calc(0, W) << endl;

  return 0;
}

int calc(int i, int j) {
  int res;

  if (i > N - 1) {
    res = 0;
  } else if (j < w[i]) {
    res = calc(i + 1, j);
  } else {
    res = (int)max(calc(i + 1, j), calc(i + 1, j - w[i]) + v[i]);
  }

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

#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

using ll = long long;

constexpr int INF = (1 << 30) - 1;
constexpr int MOD = 1000000000 + 7;

int N, W;
vector<int> v, w;

int dp[101][10001];

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

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

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


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

using ll = long long;

constexpr int INF = (1 << 30) - 1;
constexpr int MOD = 1000000000 + 7;

int N, W;
vector<int> v, w;

int dp[100][10001];

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

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

  cout << dp[N][W] << endl;
}
Problem p02315 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int N, W;
vector<int> value;
vector<int> weight;
int weight_all = 0;

int dp[101][10001];

int put(int now, int weight_now) {
  if (dp[now][weight_now] != -1) {
    return dp[now][weight_now];
  } else if (now == N) {
    return dp[now][weight_now] = 0;
  } else if (weight_now + weight[now] > W) {
    return dp[now][weight_now] = put(now + 1, weight_now);
  } else {
    return dp[now][weight_now] =
               max(put(now + 1, weight_now),
                   put(now + 1, weight_now + weight[now]) + value[now]);
  }
}

int main() {
  cin >> N >> W;
  for (int i = 0; i < N; i++) {
    int v, w;
    cin >> v >> w;
    value.push_back(v);
    weight.push_back(w);
  }
  for (int i = 0; i < 101; i++) {
    for (int j = 0; j < 10001; j++) {
      dp[i][j] = -1;
    }
  }
  cout << put(0, 0) << endl;
}

Error: TLE

Fixed submission:

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

int N, W;
vector<int> value;
vector<int> weight;

int put(int now, int weight_now) {
  if (now == N) {
    return 0;
  } else if (weight_now + weight[now] > W) {
    return put(now + 1, weight_now);
  } else {
    return max(put(now + 1, weight_now),
               put(now + 1, weight_now + weight[now]) + value[now]);
  }
}

int main() {
  cin >> N >> W;
  for (int i = 0; i < N; i++) {
    int v, w;
    cin >> v >> w;
    value.push_back(v);
    weight.push_back(w);
  }
  cout << put(0, 0) << endl;
}
Problem p02315 (C++) — buggy submission, verdict: Runtime Error

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

int dp[101][10001];

int main() {
  int n, W, ans = 0;
  cin >> n >> W;
  int v[n + 1], w[n + 1];

  for (int i = 0; i < n; i++) {
    cin >> v[i] >> w[i];
  }
  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]);
    }
    for (int j = 0; j <= W; j++) {
      if (j + w[i] <= W) {
        dp[i + 1][j + w[i]] = max(dp[i + 1][j + w[i]], dp[i][j] + v[i]);
      }
    }
  }

  for (int i = 0; i <= W; i++) {
    ans = max(ans, dp[n][i]);
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <iostream>

using namespace std;

int dp[101][1001];
int v[101], w[101];

int main() {
  int N, W;
  cin >> N >> W;

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

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