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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;

typedef long long LL;
ostream &debug = cout;

/* AOJ DPL_1_A */

const int N = 25;
const int M = 60005;
const int INF = 0x3f3f3f3f;

int n, m, a[N], dp[M];

signed main(void) {
  /* input */
  {
    scanf("%d%d", &m, &n);
    for (int i = 1; i <= n; i++)
      scanf("%d", a + i);
  }
  /* dynamic programming */
  {
    memset(dp, 0x3f, sizeof(dp)), dp[0] = 0;
    for (int i = 1, sum = 0; i <= n; sum += a[i++])
      for (int j = 0; j <= m; j++)
        dp[a[i] + j] = min(dp[a[i] + j], dp[j] + 1);
  }
  /* output */
  { printf("%d\\n", dp[m]); }
  //	system("pause");
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;

typedef long long LL;
ostream &debug = cout;

/* AOJ DPL_1_A */

const int N = 25;
const int M = 50005;
const int INF = 0x3f3f3f3f;

int n, m, a[N], dp[M];

signed main(void) {
  /* input */
  {
    scanf("%d%d", &m, &n);
    for (int i = 1; i <= n; i++)
      scanf("%d", a + i);
  }
  /* dynamic programming */
  {
    memset(dp, 0x3f, sizeof(dp)), dp[0] = 0;
    for (int i = 1, sum = 0; i <= n; sum += a[i++])
      for (int j = 0; j <= m; j++)
        dp[a[i] + j] = min(dp[a[i] + j], dp[j] + 1);
  }
  /* output */
  { printf("%d\\n", dp[m]); }
  //	system("pause");
}
Problem p02314 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define irep(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define FOR(i, m, n) for (int i = int(m); i < (int)(n); i++)

using namespace std;

int main() {
  int N, W;
  int v[30];
  int dp[50005] = {0};
  cin >> N >> W;
  rep(i, W) { cin >> v[i]; }
  rep(i, N + 1) { dp[i] = i; }
  rep(i, W) {
    rep(j, N + 1) {
      if ((j - v[i]) > -1) {
        dp[j] = min(dp[j - v[i]] + 1, dp[j]);
      }
    }
  }
  cout << dp[N] << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define irep(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define FOR(i, m, n) for (int i = int(m); i < (int)(n); i++)

using namespace std;

int main() {
  int N, W;
  int v[1005];
  int dp[1005] = {0};
  cin >> N >> W;
  rep(i, W) { cin >> v[i]; }
  rep(i, N + 1) { dp[i] = i; }
  rep(i, W) {
    rep(j, N + 1) {
      if ((j - v[i]) > -1) {
        dp[j] = min(dp[j - v[i]] + 1, dp[j]);
      }
    }
  }
  cout << dp[N] << endl;
}
Problem p02314 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> c(m);
  vector<int> dp(n + 1);
  for (int i = 0; i < n + 1; i++) {
    dp[i] = i;
  }
  for (int i = 0; i < m; i++) {
    cin >> c[i];
    dp[c[i]] = 1;
  }
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= i; j++) {
      if (j > i - j) {
        break;
      }
      dp[i] = min(dp[i], dp[j] + dp[i - j]);
    }
  }
  cout << dp[n] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> c(m);
  vector<int> dp(n + 1, 0);
  for (int i = 0; i <= n; i++) {
    dp[i] = i;
  }
  for (int i = 0; i < m; i++) {
    cin >> c[i];
    dp[c[i]] = 1;
  }
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= i; j++) {
      dp[i] = min(dp[j] + dp[i - j], dp[i]);
    }
  }
  cout << dp[n] << endl;
  return 0;
}
Problem p02315 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define PII pair<int, int>

int dp[101][10001];
PII p[101];
int main() {
  int N, W;
  cin >> N >> W;
  REP(i, N) cin >> p[i].first >> p[i].second;
  REP(i, N + 1) REP(j, W + 1) dp[i][j] = -1;
  REP(i, N) dp[i][0] = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= W; j++) {
      if (dp[i][j] != -1) {
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
        int temp = j + p[i].second;
        if (temp <= W) {
          dp[i + 1][temp] = max(dp[i + 1][temp], dp[i][j] + p[i].first);
        }
      }
    }
  }
  int ans = 0;
  for (int i = W; i >= 0; i--) {
    if (ans < dp[N][i]) {
      ans = dp[N][i];
    }
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define PII pair<int, int>

int dp[100][10001];
PII p[101];
int main() {
  int N, W;
  cin >> N >> W;
  REP(i, N) cin >> p[i].first >> p[i].second;
  REP(i, N + 1) REP(j, W + 1) dp[i][j] = -1;
  REP(i, N) dp[i][0] = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= W; j++) {
      if (dp[i][j] != -1) {
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
        int temp = j + p[i].second;
        if (temp <= W) {
          dp[i + 1][temp] = max(dp[i + 1][temp], dp[i][j] + p[i].first);
        }
      }
    }
  }
  int ans = 0;
  for (int i = W; i >= 0; i--) {
    if (ans < dp[N][i]) {
      ans = dp[N][i];
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02315 (C++) — buggy submission, verdict: Runtime Error


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

int dp(int, int);

int w[1001];
int v[101];
int memo[101][10001];

int main() {
  int N, W;
  scanf("%d %d", &N, &W);

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

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

  printf("%d\\n", dp(N, W));

  return 0;
}

int dp(int i, int j) {
  if (i == 0) {
    return 0;
  }
  if (memo[i][j] >= 0) {
    return memo[i][j];
  }
  if (j - w[i - 1] < 0) {
    return memo[i][j] = dp(i - 1, j);
  }

  return memo[i][j] = max(dp(i - 1, j), dp(i - 1, j - w[i - 1]) + v[i - 1]);
}


Error: -11

Fixed submission:


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

int dp(int, int);

int w[101];
int v[101];
int memo[101][10001];

int main() {
  int N, W;
  scanf("%d %d", &N, &W);

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

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

  printf("%d\\n", dp(N, W));

  return 0;
}

int dp(int i, int j) {
  if (j == 0) {
    return 0;
  }
  if (memo[i][j] >= 0) {
    return memo[i][j];
  }
  if (j - w[i - 1] < 0) {
    return memo[i][j] = dp(i - 1, j);
  }

  return memo[i][j] = max(dp(i - 1, j), dp(i - 1, j - w[i - 1]) + v[i - 1]);
}
Problem p02315 (C++) — buggy submission, verdict: Time Limit Exceeded

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

struct knapsack {
  int value = 0;
  int weight = 0;
};

int N, W;
vector<knapsack> vk;
int memo[101][10001];

int dp(int i, int j) {
  if (memo[i][j] > 0) {
    return memo[i][j];
  }

  int result;

  if (i == N) {
    result = 0;
  } else if (vk[i].weight > j) {
    result = dp(i + 1, j);
  } else {
    result = max(dp(i + 1, j), dp(i + 1, j - vk[i].weight) + vk[i].value);
  }

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

int main() {
  int v, w;

  scanf("%d %d", &N, &W);

  for (int i = 0; i < N; i++) {
    scanf("%d %d", &v, &w);
    vk.push_back({v, w});
  }

  memset(memo, 0, sizeof(memo));
  printf("%d\\n", dp(0, W));

  return 0;
}

Error: TLE

Fixed submission:

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

struct knapsack {
  int value = 0;
  int weight = 0;
};

int N, W;
vector<knapsack> vk;

int dp(int i, int j) {
  int result = 0;

  if (i == N) {
    result = 0;
  } else if (vk[i].weight > j) {
    result = dp(i + 1, j);
  } else {
    result = max(dp(i + 1, j), dp(i + 1, j - vk[i].weight) + vk[i].value);
  }

  return result;
}

int main() {
  int v, w;

  scanf("%d %d", &N, &W);

  for (int i = 0; i < N; i++) {
    scanf("%d %d", &v, &w);
    vk.push_back({v, w});
  }

  printf("%d\\n", dp(0, W));

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

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <list>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string>
#include <typeinfo>
#include <vector>

using namespace std;

const int MAX_N = 101;
const int MAX_W = 10001;

int main() {
  int n, w;
  scanf("%d %d", &n, &w);
  vector<int> values(MAX_N);
  vector<int> weights(MAX_N);
  for (int i = 0; i < n; i++) {
    scanf("%d %d", &values[i], &weights[i]);
  }

  vector<vector<int>> dp(MAX_N, vector<int>(MAX_W, 0));

  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= w; j++) {
      if (weights[i] > j) {
        dp[i + 1][j] = dp[i][j];
      } else {
        dp[i + 1][j] = max(dp[i][j], dp[i][j - weights[i]] + values[i]);
      }
    }
  }
  printf("%d\\n", dp[n][w]);
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <list>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string>
#include <typeinfo>
#include <vector>

using namespace std;

const int MAX_N = 101;
const int MAX_W = 10001;

int main() {
  int n, w;
  scanf("%d %d", &n, &w);
  vector<int> values;
  vector<int> weights;
  for (int i = 1; i <= n; i++) {
    int value, weight;
    scanf("%d %d", &value, &weight);
    values.push_back(value);
    weights.push_back(weight);
  }

  vector<vector<int>> dp(MAX_N, vector<int>(MAX_W, 0));

  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= w; j++) {
      if (weights[i] > j) {
        dp[i + 1][j] = dp[i][j];
      } else {
        dp[i + 1][j] = max(dp[i][j], dp[i][j - weights[i]] + values[i]);
      }
    }
  }
  printf("%d\\n", dp[n][w]);
  return 0;
}
Problem p02315 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;
int main(void) {
  int dp[105][10005] = {};
  int n;
  cin >> n;
  int W;
  cin >> W;
  int w[105], v[105];
  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++) {
      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;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int main(void) {
  int dp[105][10005] = {};
  int n;
  cin >> n;
  int W;
  cin >> W;
  int w[n], v[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++) {
      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;
}
Problem p02315 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;
int main(void) {
  int dp[105][10005] = {};
  int n;
  cin >> n;
  int W;
  cin >> W;
  int v[105], w[105];
  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++) {
      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;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int main(void) {
  int dp[105][10005] = {};
  int n;
  cin >> n;
  int W;
  cin >> W;
  int w[n], v[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++) {
      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;
}
Problem p02315 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

int main() {
  int N, W;
  cin >> N >> W;
  vector<pair<int, int>> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i].first;
    cin >> A[i].second;
  }

  vector<vector<int>> dp(N, vector<int>(W + 1));
  for (int i = 0; i < N; i++) {
    for (int c = 0; c <= W; c++) {
      int k = c - A[i].second;
      if (i == 0) {
        if (k >= 0)
          dp[0][c] = A[0].first;
        else
          dp[0][c] = 0;
      } else if (k < 0) {
        dp[i][c] = dp[i - 1][c];
      } else {
        if (A[i].first + dp[i - 1][k] < dp[i - 1][c])
          dp[i][c] = dp[i - 1][c];
        else
          dp[i][c] = A[i].first + dp[i - 1][k];
      }
    }
  }

  cout << dp[N - 1][W] << endl;
  return 0;
}

Error: -11

Fixed submission:

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

using namespace std;

int main() {
  int N, W;
  cin >> N >> W;
  vector<pair<int, int>> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i].first;
    cin >> A[i].second;
  }

  vector<vector<int>> dp(N, vector<int>(W + 1));

  for (int i = 0; i < N; i++) {
    for (int c = 0; c <= W; c++) {
      int k = c - A[i].second;
      if (k < 0) {
        if (i = 0) {
          if (c - A[0].second >= 0) {
            dp[0][c] = A[0].first;
          } else
            dp[0][c] = 0;
        } else {
          dp[i][c] = dp[i - 1][c];
        }
      } else {
        if (A[i].first + dp[i - 1][k] < dp[i - 1][c])
          dp[i][c] = dp[i - 1][c];
        else
          dp[i][c] = A[i].first + dp[i - 1][k];
      }
    }
  }

  cout << dp[N - 1][W] << endl;
  return 0;
}
Problem p02315 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <cstdio>

#define REP(i, n) for (int i = 0; (i) < (n); (i)++)
#define FOR_IN(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define IN(x, a, b) ((x) >= (a) && (x) <= (b))

using namespace std;
typedef long long int ll;
/*
N W
vi wi

N 100
W 10000

 */

int N, W;
int w[100], v[100];
ll dp[101][10001];

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

  REP(i, N) { cin >> v[i] >> w[i]; }

  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;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <cstdio>

#define REP(i, n) for (int i = 0; (i) < (n); (i)++)
#define FOR_IN(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define IN(x, a, b) ((x) >= (a) && (x) <= (b))

using namespace std;
typedef long long int ll;
/*
N W
vi wi

N 100
W 10000

 */

int N, W;
int w[100], v[100];
ll dp[100][10000];

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

  REP(i, N) { cin >> v[i] >> w[i]; }

  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;
  return 0;
}
Problem p02315 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

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

int dfs(int i, int j) {
  int res;
  if (dp[i][j] >= 0)
    return res = dp[i][j];
  if (i == N)
    res = 0;
  else if (j < w[i])
    res = dfs(i + 1, j); // ここ代入し忘れてた
  else
    res = max(dfs(i + 1, j), dfs(i + 1, j - w[i]) + v[i]);
  // dp[i][j] = res;
  return dp[i][j] = res;
}

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

  // dp_tableの初期化
  for (int i = 0; i < 101; i++)
    for (int j = 0; j < 10001; j++)
      dp[i][j] = -1;

  cout << dfs(0, W) << endl;
  return 0;
}


Error: MLE

Fixed submission:

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

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

int dfs(int i, int j) {
  int res;
  if (dp[i][j] >= 0)
    return res = dp[i][j];
  if (i == N)
    res = 0;
  else if (j < w[i])
    res = dfs(i + 1, j); // ここ代入し忘れてた
  else
    res = max(dfs(i + 1, j), dfs(i + 1, j - w[i]) + v[i]);
  // dp[i][j] = res;
  return dp[i][j] = res;
}

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

  // dp_tableの初期化
  for (int i = 0; i < 8000; i++)
    for (int j = 0; j < 8000; j++)
      dp[i][j] = -1;

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

#include <iostream>
#include <vector>

using namespace std;

template <typename T>
vector<vector<T>> make_2d_vector(size_t rows, size_t cols, T init) {
  return vector<vector<T>>(rows, vector<T>(cols, init));
}

#define debug(x) cout << #x << "==" << x << endl;

const int inf = 100000000;

typedef long long ll;

int main() {
  ios::sync_with_stdio(false);

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

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

  // // dp
  // dp[i][j] := i?????? ?????§?????????????????£??? ??????j
  // ??\\?????§????????§??????
  vector<vector<int>> dp = make_2d_vector(N + 1, W + 1, 0);
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= W; j++) {
      if (j >= w[i - 1]) {
        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];
      }
    }
  }

  cout << dp[N][W] << "\\n";

  // dp[i][j] := i???????????§?????????????????£??? ??????j??\\???
  // ??§????????§?????? vector< vector<int> > dp = make_2d_vector(N+1, W+1, 0);
  // for(int i = 1; i <= N; i++) {
  //   for(int j = 0; j <= W; j++) {
  //     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];
  //   }
  // }
  // cout << dp[N][W] << "\\n";

  // // dp[i][j] := i???????????§?????????????????£????????? j
  // ??§????????§?????? auto dp = make_2d_vector(N + 1, W + 1, 0); for(int i =
  // 1; i <= N; i++) {
  //   for(int j = 0; j <= W; j++) {
  //     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];
  //   }
  // }

  // cout << dp[N][W] << "\\n";

  return 0;
}

Error: TLE

Fixed submission:

#include <bitset>
#include <iostream>
#include <vector>

using namespace std;

#define debug(x) cout << #x << "==" << x << endl;

// 2^20 = 1048576 = 10^6
const int MAX_N = 20;

int main() {
  ios::sync_with_stdio(false);

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

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

  int v_max = 0;
  for (int b = 0; b < (1 << N); b++) {
    int w_sum = 0;
    int v_sum = 0;
    for (int i = 0; i < N; i++) {
      if ((b & (1 << i)) != 0) {
        w_sum += w[i];
        v_sum += v[i];
      }
    }
    if (w_sum <= W && v_max < v_sum)
      v_max = v_sum;
  }
  cout << v_max << "\\n";

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

#include <algorithm>
#include <cstring>
#include <iostream>
#define MAX_INT 100005
#define MAX_N 100
#define MAX_W 10000
using namespace std;

//??\\???
int N, W;
int w[MAX_N], v[MAX_N];

// dp table
int dp[MAX_N + 1][MAX_W + 1];

// i: i????????\\?????? item ?????????
// rw: ?????????????????? w ??\\???
// return: ???????????????????????§???
int rec(int i, int rw) {
  // dp lookup
  if (dp[i][rw] > 0) {
    return dp[i][rw];
  }

  int res;
  if (i == N) {
    // ?????????????????????
    res = 0;
  } else if (rw < w[i]) {
    // ??\\?????????
    res = rec(i + 1, rw);
  } else {
    // ??\\???????????¨????????????
    // ??\\????????´?????¨??\\???????????´??????????????????
    res = max(rec(i + 1, rw), rec(i + 1, rw - w[i]) + v[i]);
  }

  dp[i][rw] = res;
  return res;
}

int main() {

  memset(dp, -1, sizeof(dp));

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

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#define MAX_INT 100005
#define MAX_N 100
using namespace std;

//??\\???
int N, W;
int w[MAX_N], v[MAX_N];

// i: i????????\\?????? item ?????????
// rw: ?????????????????? w ??\\???
// return: ???????????????????????§???
int rec(int i, int rw) {

  if (i == N) {
    return 0;
  }

  if (rw < w[i]) {
    // ??\\?????????
    return rec(i + 1, rw);
  } else {
    // ??\\???????????¨????????????
    // ??\\????????´?????¨??\\???????????´??????????????????
    return max(rec(i + 1, rw), rec(i + 1, rw - w[i]) + v[i]);
  }
}

int main() {

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

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

#include <iostream>

using namespace std;
typedef long long ll;

#define max_i 100
#define max_j 10000
int dp[max_i + 1][max_j + 1];

void solve() {
  int n, m;
  int v[max_i + 1], w[max_i + 1];

  std::cin >> n >> m;
  for (int i = 0; i < n; i++) {
    /* code */
    std::cin >> v[i] >> w[i];
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= m; j++) {
      if (j < w[i]) {
        dp[i + 1][j] = dp[i][j];
      } else {
        dp[i + 1][j] = max(dp[i][j - w[i]] + v[i], dp[i][j]);
      }
    }
  }

  std::cout << dp[n][m] << '\\n';
}

int main() {
  solve();
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>

using namespace std;
typedef long long ll;

#define max_i 100
#define max_j 10000
int dp[max_i][max_j];

void solve() {
  int n, m;
  int v[max_i + 1], w[max_i + 1];

  std::cin >> n >> m;
  for (int i = 0; i < n; i++) {
    /* code */
    std::cin >> v[i] >> w[i];
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= m; j++) {
      if (j < w[i]) {
        dp[i + 1][j] = dp[i][j];
      } else {
        dp[i + 1][j] = max(dp[i][j - w[i]] + v[i], dp[i][j]);
      }
    }
  }

  std::cout << dp[n][m] << '\\n';
}

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

#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
int main() {
  const int MAX_N = 101, MAX_W = 10001, inf = -1;
  int dp[MAX_N][MAX_W], N, W, v[MAX_W], w[MAX_W];
  memset(dp, inf, sizeof(dp));
  cin >> N >> W;
  for (int i = 1; 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] >= 0 && j - w[i] <= W) {
        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] << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#define inf -1
using namespace std;
int main() {
  int dp[1001][10001], N, W, v[10001], w[10001];
  memset(dp, inf, sizeof(dp));
  cin >> N >> W;
  for (int i = 1; i <= N; i++) {
    cin >> v[i] >> w[i];
  }
  dp[0][0] = 0;
  for (int i = 1; i <= N; i++) {
    for (int j = 0; j <= W; j++) {
      if (j - w[i] >= 0 && j - w[i] <= W) {
        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] << endl;
  return 0;
}