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

#include <algorithm>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <stack>
#include <vector>
using namespace std;
const int maxn = 10500;
int dp[maxn], v[maxn], w[maxn];
int main() {
  memset(dp, 0, sizeof(dp));
  int n, W;
  cin >> n >> W;
  for (int i = 1; i <= n; i++)
    cin >> v[i] >> w[i];
  for (int i = 1; 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 <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <stack>
#include <vector>
using namespace std;
const int maxn = 1010;
int dp[maxn], v[maxn], w[maxn];
int main() {
  memset(dp, 0, sizeof(dp));
  int n, W;
  cin >> n >> W;
  for (int i = 1; i <= n; i++)
    cin >> v[i] >> w[i];
  for (int i = 1; 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: Runtime Error

#include <iostream>
using namespace std;

int main() {

  int N = 0;
  int W = 0;
  cin >> N >> W;

  int *weights = new int[N + 1];
  int *values = new int[N + 1];
  int **C = new int *[N + 1];
  for (int i = 0; i < N + 1; i++) {
    C[i] = new int[W + 1];
  }

  for (int n = 1; n <= N; n++) {
    cin >> values[n] >> weights[n];
  }

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

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

  for (int i = 1; i < N + 1; i++) {
    for (int w = 0; w < W + 1; w++) {
      if (weights[i] <= w) {
        if (values[i] + C[i - 1][w - weights[i]] > C[i - 1][w]) {
          C[i][w] = values[i] + C[i - 1][w - weights[i]];
        } else {
          C[i][w] = C[i - 1][w];
        }
      } else {
        C[i][w] = C[i - 1][w];
      }
    }
  }

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

Error: -11

Fixed submission:

#include <iostream>
using namespace std;

int main() {

  int N = 0;
  int W = 0;

  int *weights = new int[N + 1];
  int *values = new int[N + 1];
  int **C = new int *[N + 1];
  for (int i = 0; i < N + 1; i++) {
    C[i] = new int[W + 1];
  }
  cin >> N >> W;
  for (int n = 1; n <= N; n++) {
    cin >> values[n] >> weights[n];
  }

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

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

  for (int i = 1; i < N + 1; i++) {
    for (int w = 0; w < W + 1; w++) {
      if (weights[i] <= w) {
        if (values[i] + C[i - 1][w - weights[i]] > C[i - 1][w]) {
          C[i][w] = values[i] + C[i - 1][w - weights[i]];
        } else {
          C[i][w] = C[i - 1][w];
        }
      } else {
        C[i][w] = C[i - 1][w];
      }
    }
  }

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

/* ????????????????????????????????¢????????°??§?§£??? */
#include <bits/stdc++.h>
#define dump(a) cout << #a << " " << a << endl
#define N_MAX 100
#define W_MAX 10000
using namespace std;

int N, W;
vector<int> weights;
vector<int> values;
int dp[N_MAX + 1][W_MAX + 1];

int dfs(int i, int w) {
  if (dp[i][w] != -1) {
    return dp[i][w];
  }

  int res = 0;
  if (i == N) {
    res = 0;
  } else if (w < weights[i]) {
    //??????w????´??????????????????¨???
    res = dfs(i + 1, w); //?????????????????????????????\\?????????
  } else {
    //?????????????????????????????????????????¨?????¨??\\????????¨??????value?????????????????????????????????return
    res = max(dfs(i + 1, w), dfs(i + 1, w - weights[i]) + values[i]);
  }
  dp[i][w] = res;
  return res;
}

int main() {
  memset(dp, -1, sizeof(dp));

  cin >> N >> W;
  int v, w;
  for (int i = 0; i < N; i++) {
    cin >> v >> w;
    values.push_back(v);
    weights.push_back(w);
  }
  cout << dfs(0, W) << endl; // 0?????????????¨????,W??\\???

  return EXIT_SUCCESS;
}

Error: TLE

Fixed submission:

/* ????????????????????????????????¢????????°??§?§£??? */
#include <bits/stdc++.h>
#define dump(a) cout << #a << " " << a << endl
#define MAX_SIZE 25
using namespace std;

int N, W;
vector<int> weights;
vector<int> values;
int dp[MAX_SIZE + 1][MAX_SIZE + 1];

/*
 * w = ??????, dollar = ???????????????
 */
int dfs(int i, int w) {
  if (i == N) {
    return 0;
  } else if (w < weights[i]) {
    //??????w????´??????????????????¨???
    return dfs(i + 1, w); //?????????????????????????????\\?????????
  } else {
    //?????????????????????????????????????????¨?????¨??\\????????¨??????value?????????????????????????????????return
    return max(dfs(i + 1, w), dfs(i + 1, w - weights[i]) + values[i]);
  }
}

int main() {
  memset(dp, -1, sizeof(dp));

  cin >> N >> W;
  int v, w;
  for (int i = 0; i < N; i++) {
    cin >> v >> w;
    weights.push_back(w);
    values.push_back(v);
  }
  cout << dfs(0, W) << endl; // 0?????????????¨????,W??\\???

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

#include <algorithm>
#include <stdio.h>
#include <string.h>
int N, W, dp[105][20005], v[105], w[105], i;

int dfs(int d, int nw) {
  int r;
  if (dp[d][nw] >= 0)
    return dp[d][nw];
  if (d == N)
    r = 0;
  else if (w[d] > nw)
    r = dfs(d + 1, nw);
  else
    r = std::max(dfs(d + 1, nw), dfs(d + 1, nw - w[d]) + v[d]);
  return dp[d][nw] = r;
}

int main() {
  memset(dp, -1, sizeof(dp));
  scanf("%d%d", &N, &W);
  for (i = 0; i < N; i++)
    scanf("%d%d", &v[i], &w[i]);
  printf("%d\\n", dfs(0, W));
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <stdio.h>
#include <string.h>
int N, W, dp[100][10005], v[105], w[105], i;

int dfs(int d, int nw) {
  int r;
  if (dp[d][nw] >= 0)
    return dp[d][nw];
  if (d == N)
    r = 0;
  else if (w[d] > nw)
    r = dfs(d + 1, nw);
  else
    r = std::max(dfs(d + 1, nw), dfs(d + 1, nw - w[d]) + v[d]);
  return dp[d][nw] = r;
}

int main() {
  memset(dp, -1, sizeof(dp));
  scanf("%d%d", &N, &W);
  for (i = 0; i < N; i++)
    scanf("%d%d", &v[i], &w[i]);
  printf("%d\\n", dfs(0, W));
  return 0;
}
Problem p02315 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define srep(i, a, n) for (int i = a; i < n; i++)
#define REP(i, n) for (int i = 0; i <= n; i++)
#define SREP(i, a, n) for (int i = a; i <= n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define RREP(i, n) for (int i = n; i >= 0; i--)
using namespace std;
typedef long long ll;
int N, W, V_[100], W_[100];
int memo[100][10001];
int rec(int id, int w) {
  if (id == N)
    return 0;
  if (w < 0)
    return 0;
  if (memo[id][w] != -1)
    return memo[id][w];
  int coe = w < W_[id] ? 0 : 1;
  return memo[id][w] =
             max(rec(id + 1, w), (rec(id + 1, w - W_[id]) + V_[id]) * coe);
}
int main(void) {
  cin >> N >> W;
  rep(i, N) cin >> V_[i] >> W_[i];
  rep(i, N) REP(j, W) memo[i][j] = -1;
  cout << rec(0, W) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define srep(i, a, n) for (int i = a; i < n; i++)
#define REP(i, n) for (int i = 0; i <= n; i++)
#define SREP(i, a, n) for (int i = a; i <= n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define RREP(i, n) for (int i = n; i >= 0; i--)
using namespace std;
typedef long long ll;
int N, W, V_[100], W_[100];
int rec(int id, int w) {
  if (id == N)
    return 0;
  int coe = w < W_[id] ? 0 : 1;
  return max(rec(id + 1, w), (rec(id + 1, w - W_[id]) + V_[id]) * coe);
}
int main(void) {
  cin >> N >> W;
  rep(i, N) cin >> V_[i] >> W_[i];
  cout << rec(0, W) << endl;
  return 0;
}
Problem p02315 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;
int dp[101][10001];
int main() {
  int N, W, v[101], w[101];
  cin >> N >> W;
  for (int i = 1; i <= N; i++)
    cin >> v[i] >> w[i];
  for (int i = 0; i <= N; i++)
    dp[i][0] = 0;
  for (int i = 0; i <= W; i++)
    dp[0][i] = 0;
  for (int i = 1; i <= N; i++)
    for (int j = 1; j <= W; j++) {
      if (j >= w[i])
        dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]);
      else
        dp[i][j] = dp[i - 1][j];
    }
  cout << dp[N][W] << "\\n";
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  int N, W, v[101], w[101], dp[101][101];
  cin >> N >> W;
  for (int i = 1; i <= N; i++)
    cin >> v[i] >> w[i];
  for (int i = 0; i <= max(N, W); i++) {
    dp[i][0] = 0;
    dp[0][i] = 0;
  }
  for (int i = 1; i <= N; i++)
    for (int j = 1; j <= W; j++) {
      if (j >= w[i])
        dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]);
      else
        dp[i][j] = dp[i - 1][j];
    }
  cout << dp[N][W] << "\\n";
  return 0;
}
Problem p02315 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int n, m, v[1100], w[1100];
bool done[110][11000];
int memo[110][11000];

int search(int i, int h) {

  if (done[i][h])
    return memo[i][h];

  int res;
  if (i == n)
    res = 0;
  else if (w[i] > h)
    res = search(i + 1, h);
  else if (w[i] <= h) {
    int res1 = search(i + 1, h);
    int res2 = search(i + 1, h - w[i]) + v[i];
    res = max(res1, res2);
  }
  done[i][h] = true;
  return memo[i][h] = res;
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> v[i] >> w[i];

  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= m; j++) {
      done[i][j] = false;
    }
  }
  cout << search(0, m) << endl;
}


Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int n, m, v[1100], w[1100];
bool done[100][11000];
int memo[100][11000];

int search(int i, int h) {

  if (done[i][h])
    return memo[i][h];

  int res;
  if (i == n)
    res = 0;
  else if (w[i] > h)
    res = search(i + 1, h);
  else if (w[i] <= h) {
    int res1 = search(i + 1, h);
    int res2 = search(i + 1, h - w[i]) + v[i];
    res = max(res1, res2);
  }
  done[i][h] = true;
  return memo[i][h] = res;
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> v[i] >> w[i];

  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= m; j++) {
      done[i][j] = false;
    }
  }
  cout << search(0, m) << endl;
}
Problem p02315 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

#define ll long long
#define LL long long
#define ULL unsigned long long
#define ull unsigned long long
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < n; i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
const ll INF = -10000000000;
using namespace std;
ll dp[10001][101] = {};
int N, W;
int v[1000], w[1000];

int main() {
  cin >> N >> W;
  REP(i, N) cin >> v[i] >> w[i];

  for (int i = 0; i <= W; i++) {  //??\\??????????????§??????
    for (int j = 0; j < N; j++) { // j????????\\????????????
      if (i == 0)
        dp[i][j] = 0;
      else if (i >= w[j])
        dp[i][j] = max(dp[i][j - 1], dp[i - w[j]][j - 1] + v[j]);
      else
        dp[i][j] = dp[i][j - 1];
    }
  }
  /*
  REP(i, W+1) {
          REP(j, N) {
                  cout << dp[i][j] << " ";
          }
          cout << endl;
  }
  */
  cout << dp[W][N - 1] << endl;

  return 0;
}

Error: MLE

Fixed submission:

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

#define ll long long
#define LL long long
#define ULL unsigned long long
#define ull unsigned long long
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < n; i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
const ll INF = -10000000000;
using namespace std;
ll dp[10001][10001] = {};
int N, W;
int v[1000], w[1000];

int main() {
  cin >> N >> W;
  REP(i, N) cin >> v[i] >> w[i];

  for (int i = 0; i <= W; i++) {  //??\\??????????????§??????
    for (int j = 0; j < N; j++) { // j????????\\????????????
      if (i == 0)
        dp[i][j] = 0;
      else if (i >= w[j])
        dp[i][j] = max(dp[i][j - 1], dp[i - w[j]][j - 1] + v[j]);
      else
        dp[i][j] = dp[i][j - 1];
    }
  }
  /*
  REP(i, W+1) {
          REP(j, N) {
                  cout << dp[i][j] << " ";
          }
          cout << endl;
  }
  */
  cout << dp[W][N - 1] << endl;

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

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

#define INF INT_MAX / 3
#define LINF LLONG_MAX / 3

/*
 * ???????????§????????????????????????
 */

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

  dp[0][0] = 0;

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

  int ans = 0;
  for (int i = 0; i <= sumv; i++)
    if (dp[N][i] <= W)
      ans = i;
  cout << ans << endl;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;

#define INF INT_MAX / 3
#define LINF LLONG_MAX / 3

/*
 * ???????????§????????????????????????
 */

int main() {
  ll N, W;
  cin >> N >> W;
  vector<ll> v(N), w(N);
  for (int i = 0; i < N; i++)
    cin >> v[i] >> w[i];
  ll sumv = 0;
  for (int i = 0; i < N; i++)
    sumv += v[i];
  vector<vector<ll>> dp(N + 1, vector<ll>(sumv + 1, INF));

  dp[0][0] = 0;

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

  int ans = 0;
  for (int i = 0; i <= sumv; i++)
    if (dp[N][i] <= W)
      ans = i;
  cout << ans << endl;
}
Problem p02315 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int N_MAX = 100;
const int W_MAX = 10000;

int N;
int W;

int *value;
int *weight;

int dp[N_MAX + 1][W_MAX + 1];

int rec(int index, int maxWeight) {
  if (dp[index][maxWeight] != -1) {
    return dp[index][maxWeight];
  }
  int res;
  if (index == N) {
    res = 0;
  } else if (maxWeight < weight[index]) {
    res = rec(index + 1, maxWeight);
  } else {
    res = max(rec(index + 1, maxWeight),
              rec(index + 1, maxWeight - weight[index]) + value[index]);
  }
  dp[index][maxWeight] = res;

  return res;
}

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

  for (int i = 0; i < N_MAX + 1; i++) {
    for (int j = 0; j < W_MAX + 1; j++) {
      dp[i][j] = -1;
    }
  }

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

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

  cout << rec(0, W) << '\\n';
}

Error: TLE

Fixed submission:

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

int N;
int W;

int maxValue = 0;

void rec(const vector<int> &value, const vector<int> &weight, int nowWeight,
         int nowValue, int index) {
  if (nowWeight > W)
    return;
  if (maxValue < nowValue) {
    maxValue = nowValue;
  }
  if (index == N)
    return;
  rec(value, weight, nowWeight + weight[index], nowValue + value[index],
      index + 1);
  rec(value, weight, nowWeight, nowValue, index + 1);
  return;
}

int main() {

  cin >> N;
  cin >> W;

  vector<int> value(N);
  vector<int> weight(N);

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

  rec(value, weight, 0, 0, 0);

  cout << maxValue << endl;

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

#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <string>

using namespace std;

int larger(int x, int y) {
  if (x > y) {
    return x;
  } else {
    return y;
  }
}

int main() {
  int N, W;
  cin >> N >> W;
  int values[N];
  int weight[N];
  for (int i = 0; i < N; i++) {
    cin >> values[i] >> weight[i];
  }
  int dp[N + 1][W + 1];
  for (int i = 0; i < N + 1; i++) {
    for (int j = 0; j < W + 1; j++) {
      dp[i][j] = 0;
    }
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < W + 1; j++) {
      if (j < weight[i]) {
        dp[i + 1][j] = dp[i][j];
      } else {
        dp[i + 1][j] = larger(dp[i][j - weight[i]] + values[i], dp[i][j]);
      }
    }
  }
  cout << dp[N][W] << "\\n";
}


Error: 0

Fixed submission:

#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <string>

using namespace std;

int larger(int x, int y) {
  if (x > y) {
    return x;
  } else {
    return y;
  }
}

int main() {
  int N, W;
  cin >> N >> W;
  int values[N];
  int weight[N];
  for (int i = 0; i < N; i++) {
    cin >> values[i] >> weight[i];
  }
  int dp[N + 1][W + 1];
  for (int i = 0; i < N + 1; i++) {
    for (int j = 0; j < W + 1; j++) {
      dp[i][j] = 0;
    }
  }
  for (int i = 0; i < N + 1; i++) {
    for (int j = 0; j < W + 1; j++) {
      if (j < weight[i]) {
        dp[i + 1][j] = dp[i][j];
      } else {
        dp[i + 1][j] = larger(dp[i][j - weight[i]] + values[i], dp[i][j]);
      }
    }
  }
  cout << dp[N][W] << "\\n";
}
Problem p02315 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>

#define MAX_N 100
#define MAX_W 10000

int N, W, v[MAX_N], w[MAX_N];
int dp[MAX_N + 1][MAX_W + 1];

void solve();

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

  solve();

  return 0;
}

void solve() {
  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] = std::max(dp[i + 1][j], dp[i + 1][j - w[i]] + v[i]);
    }
  }

  std::cout << dp[0][W] << std::endl;
  return;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>

#define MAX_N 100
#define MAX_W 1000

int N, W, v[MAX_N], w[MAX_N];
int dp[MAX_N + 1][MAX_W + 1];

void solve();

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

  solve();

  return 0;
}

void solve() {
  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] = std::max(dp[i + 1][j], dp[i + 1][j - w[i]] + v[i]);
    }
  }

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

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

int N, W;
int v[100], w[100];
int dp[1000][10004];
int dfs(int i, int omosa) {
  // int a1=0,a2=0;
  // cout<<i<<" "<<omosa<<endl;
  if (dp[i][omosa] >= 0)
    return dp[i][omosa];
  if (i == N) {
    dp[i][omosa] = 0;
    return 0; // すべての商品を試したとき.
  }

  if (omosa < w[i])
    return dp[i][omosa] = dfs(i + 1, omosa);

  int a1 = dfs(i + 1, omosa);
  int a2 = dfs(i + 1, omosa - w[i]) + v[i];
  // cout<<a1<<"#"<<a2<<endl;
  dp[i][omosa] = max(a1, a2);
  return max(a1, a2);
}

int main() {
  cin >> N >> W;
  for (int i = 0; i < N; i++)
    cin >> v[i] >> w[i];
  //	for(int i=0;i<N;i++)cout<<v[i]<<" "<<w[i]<<endl;
  for (int i = 0; i < 1000; i++) {
    for (int j = 0; j < 10004; j++) {
      dp[i][j] = -1;
    }
  }
  cout << dfs(0, W) << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
int N, W, v[1000], w[1000]; // 価値がv,重さがw
int ans;

void dfs(int h, int omosa, int now) { // hは深さ、nowは今のところの勝ちの合計
  //	cout<<h<<" "<<omosa<<" "<<now<<endl;

  if (h == N) {
    ans = max(ans, now);
    return;
  }
  if (W >= omosa + w[h]) {
    dfs(h + 1, omosa + w[h], now + v[h]);
  }
  dfs(h + 1, omosa, now);
  return;
}

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

#include <iostream>
using namespace std;

int main() {
  unsigned N, W;
  cin >> N >> W;
  unsigned DP[101][10001];
  unsigned v[1000], w[1000];
  for (int i = 0; i < N; i++)
    cin >> v[i] >> w[i];
  for (int j = 0; j <= W; j++)
    DP[0][j] = 0;

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


Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  unsigned N, W;
  cin >> N >> W;
  unsigned DP[N][W];
  unsigned v[N], w[N];
  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][j] = 0;

  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 {
        if (DP[i + 1][j] > DP[i + 1][j - w[i]] + v[i])
          DP[i][j] = DP[i + 1][j];
        else
          DP[i][j] = DP[i + 1][j - w[i]] + v[i];
      }
  cout << DP[0][W] << endl;
  return 0;
}