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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iostream>
#include <set>
#include <sstream>
#include <typeinfo>
#include <vector>

#define REP(i, n) for (int i = 0; i < n; i++)
#define pyfor(j, s, n, i) for (int j = s; j < n; j += i)

using namespace std;
typedef long long ll;
const int INF = INT_MAX;

const int max_n = 100;
const int max_w = 10000;
int pack_hist[max_n + 1][max_w + 1];
int v_s[max_n];
int w_s[max_n];
int N, W;

int pack(int ni, int wl) {
  if (ni >= N)
    return 0;
  if (pack_hist[ni][wl] != -1)
    return pack_hist[ni][wl];
  int ans;
  if (wl >= w_s[ni]) {
    ans = max(v_s[ni] + pack(ni + 1, wl - w_s[ni]), pack(ni + 1, wl));
  } else {
    ans = pack(ni + 1, wl);
  }
  pack_hist[ni][wl] = ans;
  // cerr << "pack(" << ni << " " << wl << "): " << ans << "\\n";
  return ans;
}

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

  cin >> N >> W;

  REP(i, N) { cin >> v_s[i] >> w_s[i]; }

  REP(i, N + 1) REP(j, W + 1) pack_hist[i][j] = -1;

  cout << pack(0, W) << "\\n";
  return 0;
}

Error: 0
pack(3 5): 8
pack(3 4): 8
pack(2 5): 10
pack(3 3): 8
pack(3 2): 0
pack(2 3): 8
pack(1 5): 13
pack(3 1): 0
pack(3 0): 0
pack(2 1): 2
pack(1 3): 8
pack(0 5): 13


Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iostream>
#include <set>
#include <sstream>
#include <typeinfo>
#include <vector>

#define REP(i, n) for (int i = 0; i < n; i++)
#define pyfor(j, s, n, i) for (int j = s; j < n; j += i)

using namespace std;
typedef long long ll;
const int INF = INT_MAX;

const int max_n = 100;
const int max_w = 10000;
int pack_hist[max_n + 1][max_w + 1];
int v_s[max_n];
int w_s[max_n];
int N, W;

int pack(int ni, int wl) {
  if (ni >= N)
    return 0;
  if (pack_hist[ni][wl] != -1)
    return pack_hist[ni][wl];
  int ans;
  if (wl >= w_s[ni]) {
    ans = max(v_s[ni] + pack(ni + 1, wl - w_s[ni]), pack(ni + 1, wl));
  } else {
    ans = pack(ni + 1, wl);
  }
  pack_hist[ni][wl] = ans;
  cerr << "pack(" << ni << " " << wl << "): " << ans << "\\n";
  return ans;
}

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

  cin >> N >> W;

  REP(i, N) { cin >> v_s[i] >> w_s[i]; }

  REP(i, N + 1) REP(j, W + 1) pack_hist[i][j] = -1;

  cout << pack(0, W) << "\\n";
  return 0;
}
Problem p02315 (C++) — buggy submission, verdict: Runtime Error

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

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

int dp(int i, int j) {
  if (i == 0)
    return memo[i][j] = 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]));
}

int main() {
  int N, W;
  for (int i = 0; i < 101; i++) {
    for (int j = 0; j < 10001; j++) {
      memo[i][j] = -1;
    }
  }
  scanf("%d %d", &N, &W);
  for (int k = 0; k < N; k++) {
    scanf("%d %d", &v[k], &w[k]);
  }
  printf("%d\\n", dp(N, W));
}


Error: -11

Fixed submission:

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

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

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

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

int main() {
  int N, W;
  for (int i = 0; i < 101; i++) {
    for (int j = 0; j < 10001; j++) {
      memo[i][j] = -1;
    }
  }
  scanf("%d %d", &N, &W);
  for (int k = 0; k < N; k++) {
    scanf("%d %d", &v[k], &w[k]);
  }
  printf("%d\\n", dp(N, W));
}
Problem p02315 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int main() {
  int N, W;
  int v[101], w[101];
  cin >> N >> W;
  for (int i = 0; i < N; i++) {
    cin >> v[i] >> w[i];
  }
  int DP[N + 1][W + 1];
  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: -11

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int N, W;
  int v[N], w[N];
  cin >> N >> W;
  for (int i = 0; i < N; i++) {
    cin >> v[i] >> w[i];
  }
  int DP[N + 1][W + 1];
  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;
}
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++)

int main() {
  int n, m;
  cin >> n >> m;
  int item_v[100], item_w[100];
  rep(i, n) { cin >> item_v[i] >> item_w[i]; }

  int dp[101][10100] = {};
  for (int i = 1; i <= n; i++) {
    rep(j, 10001) {
      dp[i][j] = dp[i - 1][j];

      if (j - item_w[i - 1] >= 0) {
        dp[i][j] = max(dp[i][j], dp[i - 1][j - item_w[i - 1]] + item_v[i - 1]);
      }
    }
  }
  int maximum = -1;
  rep(j, m + 1) { maximum = max(maximum, dp[n][j]); }
  cout << maximum << endl;
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  int n, m;
  cin >> n >> m;
  int item_v[100], item_w[100];
  rep(i, n) { cin >> item_v[i] >> item_w[i]; }

  int dp[101][10100] = {};
  for (int i = 1; i <= n; i++) {
    rep(j, 100001) {
      dp[i][j] = dp[i - 1][j];

      if (j - item_w[i - 1] >= 0) {
        dp[i][j] = max(dp[i][j], dp[i - 1][j - item_w[i - 1]] + item_v[i - 1]);
      }
    }
  }
  int maximum = -1;
  rep(j, m + 1) { maximum = max(maximum, dp[n][j]); }
  cout << maximum << endl;
}
Problem p02315 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <list>
#include <string>
#include <vector>

using namespace std;

int n, w;
vector<vector<int>> dp(101, vector<int>(10001, -1));
vector<pair<int, int>> c(0);

int search(int i, int j) {
  if (dp[i][j] > -1)
    return dp[i][j];
  int ans;
  if (i == n) {
    ans = 0;
  } else if (c[i].second > j) {
    ans = search(i + 1, j);
  } else {
    ans = max(search(i + 1, j), search(i + 1, j - c[i].second) + c[i].first);
  }
  dp[i][j] = ans;
  return ans;
}

int main(void) {

  cin >> n;
  cin >> w;

  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x;
    cin >> y;
    c.push_back(make_pair(x, y));
  }

  cout << search(0, w) << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <list>
#include <string>
#include <vector>

using namespace std;

int n, w;
vector<vector<int>> dp(100, vector<int>(10001, -1));
vector<pair<int, int>> c(0);

int search(int i, int j) {
  if (dp[i][j] > -1)
    return dp[i][j];
  int ans;
  if (i == n) {
    return 0;
  } else if (c[i].second > j) {
    ans = search(i + 1, j);
  } else {
    ans = max(search(i + 1, j), search(i + 1, j - c[i].second) + c[i].first);
  }
  dp[i][j] = ans;
  return ans;
}

int main(void) {

  cin >> n;
  cin >> w;

  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x;
    cin >> y;
    c.push_back(make_pair(x, y));
  }

  cout << search(0, w) << endl;

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

#include <iostream>
using namespace std;

int main() {
  int n = 0;
  int w = 0;
  int v[1000];
  int m[1000];

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

  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int n = 0;
  int w = 0;
  int v[1000];
  int m[1000];
  int DP[100000][100000];
  cin >> n >> w;
  for (int i = 0; i < n; i++) {
    cin >> v[i] >> m[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 < m[i - 1]) {
        DP[i][j] = DP[i - 1][j];
      } else {
        if (DP[i - 1][j] > DP[i - 1][j - m[i - 1]] + v[i - 1]) {
          DP[i][j] = DP[i - 1][j];
        } else {
          DP[i][j] = DP[i - 1][j - m[i - 1]] + v[i - 1];
        }
      }
    }
  }
  cout << DP[n][w] << endl;

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

#include <iostream>
using namespace std;
int w[1001];
int v[1001];
int c;
int arr[10001][10001];
int max1(int a, int b) { return (a > b) ? a : b; }
void print(int n1, int n2) {
  for (int i = 1; i <= n1; i++) {
    for (int j = 1; j <= n2; j++) {

      cout << arr[i][j] << " ";
    }
    cout << endl;
  }
}
int main() {

  int n;
  cin >> n;
  cin >> c;
  for (int i = 1; i <= n; i++) {
    cin >> v[i];
    cin >> w[i];
  }
  /*for(int i = 0; i < n; i++)
  {
      cin >> v[i];
  }*/
  //  cin >> c;
  for (int i = 0; i <= c; i++) {
    arr[0][i] = 0;
  }
  for (int i = 0; i <= n; i++) {
    arr[i][0] = 0;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= c; j++) {
      if (j >= w[i]) {
        arr[i][j] = max1(v[i] + arr[i - 1][j - w[i]], arr[i - 1][j]);
      } else {
        arr[i][j] = arr[i - 1][j];
      }
    }
  }
  //  print(n,c);
  cout << arr[n][c] << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
using namespace std;

int max1(int a, int b) { return (a > b) ? a : b; }
/*void print(int n1, int n2)
{
    for(int i = 0; i < n1; i++)
    {
        for(int j = 0; j < n2; j++)
        {

            cout << arr[i][j] << " ";
        }
        cout << endl;
    }
}*/
int main() {
  int w[1001];
  int v[1001];
  int c;
  int arr[10001][10001];
  int n;
  cin >> n;
  cin >> c;
  for (int i = 0; i < n; i++) {
    cin >> v[i];
    cin >> w[i];
  }
  /*for(int i = 0; i < n; i++)
  {
      cin >> v[i];
  }*/
  //  cin >> c;
  arr[0][0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= c; j++) {
      if (j >= w[i]) {
        arr[i][j] = max1(v[i] + arr[i - 1][j - w[i]], arr[i - 1][j]);
      } else {
        arr[i][j] = arr[i - 1][j];
      }
    }
  }
  // print(n,c+1);
  cout << arr[n - 1][c] << endl;
  return 0;
}
Problem p02315 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

using namespace std;

int N, W;
vector<int> values;
vector<int> weights;
vector<vector<int>> *dp;

// i 番目以降の品物から重さの総和が w 以下となるように選んだときの価値の最大値
int napsack(const int i, const int w) {
  if ((*dp)[i][w] >= 0) {
    return (*dp)[i][w];
  }
  int res;
  if (i == N) {
    // もう品物は残っていない
    res = 0;
  } else if (w < weights[i]) {
    int wi = weights[i];
    // i 番目の品物は入らない
    res = napsack(i + 1, w);
  } else {
    int wi = weights[i];
    // 入れる場合と入れない場合を両方試す
    res = max(napsack(i + 1, w), napsack(i + 1, w - wi) + values[i]);
  }
  (*dp)[i][w] = res;
  return res;
}

int main() {

  cin >> N >> W;

  values = vector<int>(N, 0);
  weights = vector<int>(N, 0);
  for (int i = 0; i < N; ++i) {
    cin >> values[i];
    cin >> weights[i];
  }
  dp = new vector<vector<int>>(N + 1, vector<int>(W + 1, -1));

  // vector<vector<int>> dp(N, vector<int>(W, 0));
  cout << napsack(0, W) << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int N, W;
vector<int> values;
vector<int> weights;

// i 番目以降の品物から重さの総和が w 以下となるように選んだときの価値の最大値
int napsack(const int i, const int w) {
  if (i == N) {
    // もう品物は残っていない
    return 0;
  }
  int wi = weights[i];
  if (w < wi) {
    // i 番目の品物は入らない
    return napsack(i + 1, w);
  }
  // 入れる場合と入れない場合を両方試す
  return max(napsack(i + 1, w), napsack(i + 1, w - wi) + values[i]);
}

int main() {

  cin >> N >> W;

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

  // vector<vector<int>> dp(N, vector<int>(W, 0));
  cout << napsack(0, W) << endl;

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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = (0); (i) < (int)(n); ++(i))
using ll = long long;
using P = pair<int, int>;
using namespace std;

template <class T> void vin(vector<T> &v, int n) {
  v.resize(n);
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
  }
}

int N, W;
int w[1010], v[1010];
int dp[101][101010];

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() {
  cin >> N >> W;
  rep(i, N) { cin >> v[i] >> w[i]; }
  fill(dp[0], dp[N], -1);
  cout << rec(0, W) << endl;
  return 0;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = (0); (i) < (int)(n); ++(i))
using ll = long long;
using P = pair<int, int>;
using namespace std;

template <class T> void vin(vector<T> &v, int n) {
  v.resize(n);
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
  }
}

int N, W;
int w[1010], v[1010];
int dp[101][1010101];

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

#include <cstring>
#include <iostream>

using namespace std;

const int INF = -1;

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

// i???????????§???????????§???j??\\????????????
int solve(int i, int j) {
  if (dp[i][j] != INF)
    return dp[i][j];
  int res;
  if (i == N)
    res = 0;
  else if (j < w[i])
    res = solve(i + 1, j);
  else
    res = max(solve(i + 1, j), solve(i + 1, j - w[i]) + v[i]);
  return dp[i][j] = res;
}

int main() {
  memset(dp, -1, sizeof(dp));
  cin >> N >> W;
  for (int i = 0; i < N; ++i)
    cin >> v[i] >> w[i];
  cout << solve(0, W) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int N, W;
int v[101], w[101];

// i???????????§???????????§???j??\\????????????
int solve(int i, int j) {
  int res;
  if (i == N)
    res = 0;
  else if (j < w[i])
    res = solve(i + 1, j);
  else
    res = max(solve(i + 1, j), solve(i + 1, j - w[i]) + v[i]);
  return res;
}

int main() {
  cin >> N >> W;
  for (int i = 0; i < N; ++i)
    cin >> v[i] >> w[i];
  cout << solve(0, W) << endl;
  return 0;
}
Problem p02315 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;

// n:品物の数, W:入れられる重さの総和の限界
int N, W;
// w[i]: i個目の荷物の重さ
vector<int> w;
// v[i]: i個目の荷物の価値
vector<int> v;
vector<vector<int>> dp;

// //
// i番目以降の品物から重さの総和がj以下となるように選んだときの最大の価値が返ってくる
// int rec(int i, int j) {
//   if(dp[i][j] >= 0) { return dp[i][j]; }
//   int res;
//   if(i == N) {
//     res = 0;
//   }
//   // w[j]が重さオーバーで入れられないとき
//   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]);
//   }
//   dp[i][j] = res;
//   return res;
// }

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N >> W;
  w.assign(N, 0);
  v.assign(N, 0);
  dp.assign(N + 1, vector<int>(W + 1, -1));
  REP(i, N) cin >> v[i] >> w[i];
  for (int j = 0; j < W + 1; j++) {
    dp[N][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 {
        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 <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
using namespace std;
using ll = long long;

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N >> W;
  v.resize(N);
  w.resize(N);
  REP(i, N) cin >> v[i] >> w[i];
  ll ans = dp(0, W);
  cout << ans << endl;
  return 0;
}
Problem p02315 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#define INF 10000000
#define MIN(a, b) ((a > b) ? (b) : (a))
#define MAX(a, b) ((a > b) ? (a) : (b))

int main(void) {
  int N, W;
  int v[1001], w[1001];
  int dp[10001];
  int i, j, k;

  scanf("%d%d", &N, &W);
  for (i = 1; i <= N; i++)
    scanf("%d%d", v + i, w + i);

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

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

  printf("%d\\n", dp[W]);
  return 0;
}

Error: WA

Fixed submission:

#include <stdio.h>
#define INF -10000000
#define MIN(a, b) (((a) > (b)) ? (b) : (a))
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define DEBUG
int dp[101][1001];

int main(void) {
  int N, W;
  int v[101], w[101];

  scanf("%d%d", &N, &W);
  for (int i = 1; i <= N; i++)
    scanf("%d%d", v + i, w + i);

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

  for (int i = 0; i <= N; i++) {
    for (int j = W; j > 0; j--) {
      //			if(j-w[i]>=0){
      //			if(!(j-w[i]>1)) continue;
      if (j - w[i] < 0) {
        continue;
      }
      dp[i][j] = MAX(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]);
      //			}elsedp[i][j]=dp[i-1][j];
    }
  }
  // aa
  //		for(int i=0;i<=N;i++){
  //			for(int j=0;j<=W;j++){
  //				printf("%d ",dp[i][j]);
  //			}
  //			printf("\\n");
  //		}
  //	bb
  printf("%d\\n", dp[N][W]);
  return 0;
}
Problem p02315 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

const int MOD = 1000000007;
const int INF = int(1e9);
const int MAX = 2000;
int dp[10007][300], n, capacity, value[MAX], wt[MAX];

int solve(int w, int n) {
  if (dp[w][n] != -1) {
    return dp[w][n];
  }
  if (w < 0 || n < 0) {
    return dp[w][n] = 0;
  }
  if (wt[n] > w) {
    return dp[w][n] = solve(w, n - 1);
  } else {
    return dp[w][n] = max(solve(w - wt[n], n - 1) + value[n], solve(w, n - 1));
  }
  return dp[w][n];
}

int main() {
  ios_base::sync_with_stdio(false);
  for (int i = 0; i < 10007; i++) {
    for (int j = 0; j < 300; j++) {
      dp[i][j] = -1;
    }
  }
  cin >> n >> capacity;
  for (int i = 1; i <= n; i++) {
    cin >> value[i] >> wt[i];
  }
  cout << solve(capacity, n) << "\\n";

  return 0;
}

Error: MLE

Fixed submission:

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

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

const int MOD = 1000000007;
const int INF = int(1e9);
const int MAX = 2000;
int dp[10007][MAX], n, capacity, value[MAX], wt[MAX];

int solve(int w, int n) {
  if (dp[w][n] != -1) {
    return dp[w][n];
  }
  if (w < 0 || n < 0) {
    return dp[w][n] = 0;
  }
  if (wt[n] > w) {
    return dp[w][n] = solve(w, n - 1);
  } else {
    return dp[w][n] = max(solve(w - wt[n], n - 1) + value[n], solve(w, n - 1));
  }
  return dp[w][n];
}

int main() {
  ios_base::sync_with_stdio(false);
  for (int i = 0; i < 10007; i++) {
    for (int j = 0; j < MAX; j++) {
      dp[i][j] = -1;
    }
  }
  cin >> n >> capacity;
  for (int i = 1; i <= n; i++) {
    cin >> value[i] >> wt[i];
  }
  cout << solve(capacity, n) << "\\n";

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

#include <iostream>
using namespace std;
int dp[101][10001];

int main(void) {
  // Your code here!
  int n, W, w[101], v[101];
  cin >> n >> W;
  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 - w[i]] + v[i], dp[i][j]);
    }
  }
  cout << dp[n][W] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
int dp[101][10001];

int main(void) {
  // Your code here!
  int n, W, w[10001], v[101];
  cin >> n >> W;
  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 - w[i]] + v[i], dp[i][j]);
    }
  }
  cout << dp[n][W] << endl;
  return 0;
}
Problem p02315 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stack>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

int dp[101][10001]; // i番目で、残りの容量がjの時の価値
int n, weight;
int v[101], w[101];

int rec(int i, int j) // i番目で、残りの容量がj
{
  if (dp[i][j] > 0) {
    return dp[i][j];
  }

  if (i == n) {
    return 0;
  } else if (j <
             w[i]) // 次に入れる物の重さが容量を超えている時は「入れない」一択
  {
    dp[i][j] = rec(i + 1, j); // iは次に進み、残り容量はそのまま
  } else // 入りそうな時は、「入れる」「入れない」を計算して、大きい方を選択
  {
    dp[i][j] = max(rec(i + 1, j), rec(i + 1, j - w[i]) + v[i]);
    // 上のやつと、iは次に進み、残り容量を減らしたものに価値を足したものを比較
  }

  return dp[i][j];
}

int main() {
  cin >> n >> weight;
  for (int i = 0; i < n; i++) {
    cin >> v[i] >> w[i];
  }
  memset(dp, -1, sizeof(dp));

  cout << rec(0, weight) << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stack>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

int dp[100][10000]; // i番目で、残りの容量がjの時の価値
int n, weight;
int v[100], w[100];

int rec(int i, int j) // i番目で、残りの容量がj
{
  if (dp[i][j] > 0) {
    return dp[i][j];
  }

  if (i == n) {
    return 0;
  } else if (j <
             w[i]) // 次に入れる物の重さが容量を超えている時は「入れない」一択
  {
    dp[i][j] = rec(i + 1, j); // iは次に進み、残り容量はそのまま
  } else // 入りそうな時は、「入れる」「入れない」を計算して、大きい方を選択
  {
    dp[i][j] = max(rec(i + 1, j), rec(i + 1, j - w[i]) + v[i]);
    // 上のやつと、iは次に進み、残り容量を減らしたものに価値を足したものを比較
  }

  return dp[i][j];
}

int main() {
  cin >> n >> weight;
  for (int i = 0; i < n; i++) {
    cin >> v[i] >> w[i];
  }
  memset(dp, -1, sizeof(dp));

  cout << rec(0, weight) << endl;
  return 0;
}