prithivMLmods/Coder-Stat huggingface dataset:
Problem p02315 (C++)

Buggy submission (status: Runtime Error):
#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;
}

Fixed submission:
#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;
}
Problem p02315 (C++)

Buggy submission (status: Runtime Error):
// ------------------------------------
// Date:2018/ 3/ 2
// Problem:AOJ DPL 0-1 Knapsack Problem 1_b.cpp
//
// ------------------------------------

#include <bits/stdc++.h>

using namespace std;

#define EACH(i, a) for (auto &&i : a)
#define FOR(i, a, b) for (int i = (int)a; i < (int)b; ++i)
#define RFOR(i, a, b) for (int i = (int)b - 1; i >= (int)a; --i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
#define debug(x) cerr << #x << ":" << x << endl

typedef long long ll;

// static const int MOD = 1000000007;

// dp[今見てる品物の個数][まだ詰め込める量]
int dp[101][10101];

// v:value / w:weight
int v[1010];
int w[1010];

// N:品物の個数 / W:ナップサックの容量
int N, W;

int main() {
  // 初期化
  memset(dp, 0, sizeof(dp));
  memset(v, 0, sizeof(v));
  memset(w, 0, sizeof(w));
  scanf("%d %d", &N, &W);
  REP(i, N) { scanf("%d %d", &v[i], &w[i]); }

  RREP(num, N + 1) {
    REP(weight, W + 1) {
      if (weight < w[num]) {
        // 見ている品物の重さが、まだ詰め込める量より多いとき
        dp[num][weight] = dp[num + 1][weight];
      } else {
        // 詰め込むことが可能な状態で、max(詰め込まない or 詰め込む
        dp[num][weight] =
            max(dp[num + 1][weight], dp[num + 1][weight - w[num]] + v[num]);
      }
    }
  }

  int ans = dp[0][W];
  printf("%d\n", ans);

  return 0;
}


Fixed submission:
// ------------------------------------
// Date:2018/ 3/ 2
// Problem:AOJ DPL 0-1 Knapsack Problem 1_b.cpp
//
// ------------------------------------

#include <bits/stdc++.h>

using namespace std;

#define EACH(i, a) for (auto &&i : a)
#define FOR(i, a, b) for (int i = (int)a; i < (int)b; ++i)
#define RFOR(i, a, b) for (int i = (int)b - 1; i >= (int)a; --i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
#define debug(x) cerr << #x << ":" << x << endl

typedef long long ll;

// static const int MOD = 1000000007;

// dp[今見てる品物の個数][まだ詰め込める量]
int dp[101][10101];

// v:value / w:weight
int v[1010];
int w[1010];

// N:品物の個数 / W:ナップサックの容量
int N, W;

int main() {
  // 初期化
  memset(dp, 0, sizeof(dp));
  memset(v, 0, sizeof(v));
  memset(w, 0, sizeof(w));
  scanf("%d %d", &N, &W);
  REP(i, N) { scanf("%d %d", &v[i], &w[i]); }

  RREP(num, N) {
    REP(weight, W + 1) {
      if (weight < w[num]) {
        // 見ている品物の重さが、まだ詰め込める量より多いとき
        dp[num][weight] = dp[num + 1][weight];
      } else {
        // 詰め込むことが可能な状態で、max(詰め込まない or 詰め込む
        dp[num][weight] =
            max(dp[num + 1][weight], dp[num + 1][weight - w[num]] + v[num]);
      }
    }
  }

  int ans = dp[0][W];
  printf("%d\n", ans);

  return 0;
}
Problem p02315 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <vector>

using namespace std;

vector<int> dp(100000);

int main() {

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

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <vector>

using namespace std;

vector<int> dp(100000);

int main() {

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

  return 0;
}
Problem p02315 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#define REP(i, n) for (int i = 0; i < n; i++)
using namespace std;
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, W;
  cin >> N >> W;
  int value[N];
  int weight[N];
  REP(n, N) {
    cin >> value[n];
    cin >> weight[n];
  }

  int dp[100][10000] = {{}};

  REP(n, N) {
    REP(w, W + 1) {
      weight[n] > w
          ? dp[n + 1][w] = dp[n][w]
          : dp[n + 1][w] = max(dp[n][w], dp[n][w - weight[n]] + value[n]);
    }
  }

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

  return 0;
}


Fixed submission:
#include <iostream>
#define REP(i, n) for (int i = 0; i < n; i++)
using namespace std;
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, W;
  cin >> N >> W;
  int value[N];
  int weight[N];
  REP(n, N) {
    cin >> value[n];
    cin >> weight[n];
  }

  int dp[110][11000] = {{}};

  REP(n, N) {
    REP(w, W + 1) {
      weight[n] > w
          ? dp[n + 1][w] = dp[n][w]
          : dp[n + 1][w] = max(dp[n][w], dp[n][w - weight[n]] + value[n]);
    }
  }

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

  return 0;
}
Problem p02315 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
int i, j, a, b, c[101], d[101], e[101][10001], f = -1;
int main(void) {
  scanf("%d %d", &a, &b);
  for (i = 0; i <= a; i++) {
    for (j = 0; j <= b; j++) {
      e[i][j] = -1;
    }
  }
  for (i = 0; i < a; i++) {
    scanf("%d %d", &c[i], &d[i]);
  }
  e[0][0] = 0;
  for (i = 0; i < a; i++) {
    for (j = 0; j <= b; j++) {
      if (e[i][j] != -1) {
        if (e[i][j] + c[i] > e[i + 1][j + d[i]] && j + d[i] <= b) {
          e[i + 1][j + d[i]] = e[i][j] + c[i];
        }
        if (e[i][j] > e[i + 1][j]) {
          e[i + 1][j] = e[i][j];
        }
      }
    }
  }
  for (i = 0; i <= a; i++) {
    for (j = 0; j <= b; j++) {
      if (f < e[i][j]) {
        f = e[i][j];
      }
    }
  }
  printf("%d\n", f);
  return 0;
}

Fixed submission:
#include <stdio.h>
int i, j, a, b, c[101], d[101], e[102][10001], f = -1;
int main(void) {
  scanf("%d %d", &a, &b);
  for (i = 0; i <= a; i++) {
    for (j = 0; j <= b; j++) {
      e[i][j] = -1;
    }
  }
  for (i = 0; i < a; i++) {
    scanf("%d %d", &c[i], &d[i]);
  }
  e[0][0] = 0;
  for (i = 0; i < a; i++) {
    for (j = 0; j <= b; j++) {
      if (e[i][j] != -1) {
        if (e[i][j] + c[i] > e[i + 1][j + d[i]] && j + d[i] <= b) {
          e[i + 1][j + d[i]] = e[i][j] + c[i];
        }
        if (e[i][j] > e[i + 1][j]) {
          e[i + 1][j] = e[i][j];
        }
      }
    }
  }
  for (i = 0; i <= a; i++) {
    for (j = 0; j <= b; j++) {
      if (f < e[i][j]) {
        f = e[i][j];
      }
    }
  }
  printf("%d\n", f);
  return 0;
}
Problem p02315 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <iostream>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

#define MP make_pair
typedef unsigned long long ULL;

// dp[i+][j]
// i番目まで使って重さj以下になるように選んだ時の最大価値
long dp[100][10001];

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];

  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;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <iostream>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

#define MP make_pair
typedef unsigned long long ULL;

// dp[i+][j]
// i番目まで使って重さj以下になるように選んだ時の最大価値
long dp[101][10001];

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];

  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;

  return 0;
}
Problem p02315 (C++)

Buggy submission (status: 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[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;
}


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[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;
}
Problem p02315 (C++)

Buggy submission (status: 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[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;
}


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 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;
}
Problem p02315 (C++)

Buggy submission (status: 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[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;
}


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[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;
}
Problem p02315 (C++)

Buggy submission (status: Runtime Error):
#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;
}


Fixed submission:
#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;
}
Problem p02315 (C++)

Buggy submission (status: 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)
        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();
}

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 && 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();
}
Problem p02315 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int N, W;
int dp[10010][10010];
vector<int> v(1100);
vector<int> w(1100);
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() {
  memset(dp, -1, sizeof(dp));
  cin >> N >> W;
  rep(i, N) { cin >> v[i] >> w[i]; }
  cout << rec(0, W) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int N, W;
int dp[110][10010];
vector<int> v(1100);
vector<int> w(1100);
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() {
  memset(dp, -1, sizeof(dp));
  cin >> N >> W;
  rep(i, N) { cin >> v[i] >> w[i]; }
  cout << rec(0, W) << endl;
  return 0;
}
Problem p02315 (C++)

Buggy submission (status: 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;
}

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 (status: 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][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;
}

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][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;
}
Problem p02315 (C++)

Buggy submission (status: Runtime Error):
#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;
}


Fixed submission:
#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;
}
Problem p02315 (C++)

Buggy submission (status: 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 = 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;
}

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 = 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;
}
Problem p02315 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define CI cin >>
#define CO cout <<
#define E << endl;

using namespace std;

typedef pair<int, int> P;
typedef pair<long, long> LP;
typedef pair<int, P> PP;
typedef pair<long, LP> LPP;

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

int A = 0, B = 0, K = 0, T = 0, W = 0, N = 0, H = 0;
int n = 0;
double L = 0;
double S = 0;
double ar = 0, br = 0, cr = 0;
int answer = 0;

string C;
string sA, strB;
vector<vector<char>> map;
vector<double> num;
vector<string> str;

int sum = 0;

vector<int> value;
vector<int> weight;

int dp[100][10000];

void input() {
  CI N >> W;

  int temp;

  for (int i = 0; i < N; i++) {
    CI temp;
    value.push_back(temp);
    CI temp;
    weight.push_back(temp);
  }

  return;
}

int main(void) {
  input();

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

  CO dp[N][W] E

      return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define CI cin >>
#define CO cout <<
#define E << endl;

using namespace std;

typedef pair<int, int> P;
typedef pair<long, long> LP;
typedef pair<int, P> PP;
typedef pair<long, LP> LPP;

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

int A = 0, B = 0, K = 0, T = 0, W = 0, N = 0, H = 0;
int n = 0;
double L = 0;
double S = 0;
double ar = 0, br = 0, cr = 0;
int answer = 0;

string C;
string sA, strB;
vector<vector<char>> map;
vector<double> num;
vector<string> str;

int sum = 0;

vector<int> value;
vector<int> weight;

int dp[110][10010];

void input() {
  CI N >> W;

  int temp;

  for (int i = 0; i < N; i++) {
    CI temp;
    value.push_back(temp);
    CI temp;
    weight.push_back(temp);
  }

  return;
}

int main(void) {
  input();

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

  CO dp[N][W] E

      return 0;
}
Problem p02315 (C++)

Buggy submission (status: 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[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;
}

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[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;
}
Problem p02315 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define MAX_N 100
#define MAX_W 100001
long long dp[MAX_N][MAX_W];

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


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define MAX_N 101
#define MAX_W 100001
long long dp[MAX_N][MAX_W];

int main() {
  long long N;
  cin >> N;
  long long W;
  cin >> W;
  vector<vector<long long>> v(N, vector<long long>(2));
  for (long long i = 0; i < N; i++) {
    cin >> v[i][0] >> v[i][1];
  }
  for (long long i = 0; i <= W; i++) {
    dp[0][i] = 0;
  }
  for (long long i = 0; i < N; i++) {
    for (long long j = 0; j <= W; j++) {
      if (j < v[i][1]) {
        dp[i + 1][j] = dp[i][j];
      } else {
        dp[i + 1][j] = max(dp[i][j], dp[i][j - v[i][1]] + v[i][0]);
      }
    }
  }
  cout << dp[N][W] << endl;
  return 0;
}
Problem p02315 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;

int n, W, w[100], v[100], memo[101][10001];
int rec(int i, int j) {
  if (memo[i][j] != -1)
    return memo[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 res;
}
int main() {
  cin >> n >> W;
  for (int i = 0; i < n; ++i) {
    cin >> v[i] >> w[i];
  }
  memset(memo, -1, sizeof(memo));
  cout << rec(0, W) << endl;
}

Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;

int n, W, w[100], v[100], memo[101][10001];
int rec(int i, int j) {
  if (memo[i][j] != -1)
    return memo[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 memo[i][j] = res;
}
int main() {
  cin >> n >> W;
  for (int i = 0; i < n; ++i) {
    cin >> v[i] >> w[i];
  }
  memset(memo, -1, sizeof(memo));
  cout << rec(0, W) << endl;
}
Problem p02315 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <cstring>
#include <iostream>

#define nMAX 1000
#define vMAX 10000
#define wMAX 10000
#define WMAX 100000

int n, W;
int v[nMAX], w[nMAX];
int dp[nMAX][WMAX];

using namespace std;

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(void) {
  std::ios_base::sync_with_stdio();

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

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>

#define nMAX 200
#define vMAX 2000
#define wMAX 2000
#define WMAX 20000

int n, W;
int v[nMAX], w[nMAX];
int dp[nMAX][WMAX];

using namespace std;

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(void) {
  std::ios_base::sync_with_stdio();

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

  return 0;
}
Problem p02315 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define INF 2000000000
#define int long long int
#define MOD 1000000007
#define pb push_back
#define cin1(n) (cin >> n)
#define cin2(a, b) (cin >> a >> b)
#define cin3(a, b, c) (cin >> a >> b >> c)
#define cout(n) (cout << n << endl)
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define fr first
#define sc second
#define EPS (1e-10)

using namespace std;

typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<pair<int, int>> vpii;
typedef vector<string> vs;
typedef vector<vector<string>> vss;
typedef pair<int, int> pii;

int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int n, m;
int a, b;
int p, q;

signed main() {
  int N, W;
  cin2(N, W);

  vi v(N + 1, 0), w(W + 1, 0);
  rep(i, 1, N + 1) { cin2(v[i], w[i]); }

  vvi dp(N + 1, vi(W + 1, 0));

  rep(i, 1, N + 1) {
    rep(j, 1, W + 1) {
      if (j - w[i] >= 0) {
        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]);

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define INF 2000000000
#define int long long int
#define MOD 1000000007
#define pb push_back
#define cin1(n) (cin >> n)
#define cin2(a, b) (cin >> a >> b)
#define cin3(a, b, c) (cin >> a >> b >> c)
#define cout(n) (cout << n << endl)
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define fr first
#define sc second
#define EPS (1e-10)

using namespace std;

typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<pair<int, int>> vpii;
typedef vector<string> vs;
typedef vector<vector<string>> vss;
typedef pair<int, int> pii;

int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int n, m;
int a, b;
int p, q;

signed main() {
  int N, W;
  cin2(N, W);

  vi v(N + 1, 0), w(N + 1, 0);
  rep(i, 1, N + 1) { cin2(v[i], w[i]); }

  vvi dp(N + 1, vi(W + 1, 0));

  rep(i, 1, N + 1) {
    rep(j, 1, W + 1) {
      if (j - w[i] >= 0) {
        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]);

  return 0;
}
Problem p02315 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define ll long long
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define ROF(i, a, b) for (int i = b - 1; i >= a; i--)
using namespace std;
//
const int inf = 1e9 + 7;
const int mod = 1e9 + 7;
//
struct poi {
  int X;
  int Y;
  int Z;
  bool operator<(const poi &R) const {
    return X == R.X ? Y == R.Y ? Z < R.Z : Y < R.Y : X < R.X;
  }
};
//
ll GCD(ll a, ll b) { return (b == 0) ? (a) : (GCD(b, a % b)); }
ll LCM(ll a, ll b) { return a / GCD(a, b) * b; }
//
int DP[101][10001];
int main() {
  int N, W;
  int v[100], w[100];
  cin >> N >> W;
  FOR(i, 0, N) { cin >> v[i] >> w[i]; }
  FOR(i, 0, N) {
    FOR(j, 0, W + 1) {
      DP[i + 1][j] = max(DP[i + 1][j], DP[i][j]);
      if (W - j >= w[i]) {
        DP[i + 1][j + w[i]] = max(DP[i + 1][j + w[i]], DP[i][j] + v[i]);
      } else {
        DP[i + 1][j + w[i]] = DP[i][j + w[i]];
      }
    }
  }
  cout << DP[N][W] << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define ll long long
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define ROF(i, a, b) for (int i = b - 1; i >= a; i--)
using namespace std;
//
const int inf = 1e9 + 7;
const int mod = 1e9 + 7;
//
struct poi {
  int X;
  int Y;
  int Z;
  bool operator<(const poi &R) const {
    return X == R.X ? Y == R.Y ? Z < R.Z : Y < R.Y : X < R.X;
  }
};
//
ll GCD(ll a, ll b) { return (b == 0) ? (a) : (GCD(b, a % b)); }
ll LCM(ll a, ll b) { return a / GCD(a, b) * b; }
//
int DP[101][10001];
int main() {
  int N, W;
  int v[100], w[100];
  cin >> N >> W;
  FOR(i, 0, N) { cin >> v[i] >> w[i]; }
  FOR(i, 0, N) {
    FOR(j, 0, W + 1) {
      DP[i + 1][j] = max(DP[i + 1][j], DP[i][j]);
      if (W - j >= w[i]) {
        DP[i + 1][j + w[i]] = max(DP[i + 1][j + w[i]], DP[i][j] + v[i]);
      }
    }
  }
  cout << DP[N][W] << endl;
  return 0;
}
Problem p02315 (C++)

Buggy submission (status: 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[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;
}


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[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;
}
Problem p02315 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int main() {
  int dp[1000][10010] = {0}, w[10000] = {0}, k[10000] = {0}, N, M, i, j;
  cin >> N >> M;
  for (i = 1; i <= N; i++) {
    cin >> k[i] >> w[i];
  }
  for (i = 1; i <= N; i++) {
    for (j = 1; j <= M; j++) {
      if (i == 1) {
        if (j >= w[i]) {
          dp[i][j] = k[i];
          if (dp[i][j] <= dp[i - 1][j]) {
            dp[i][j] = dp[i - 1][j];
          }
        }

      } else {
        if (dp[i][j] <= dp[i - 1][j]) {
          dp[i][j] = dp[i - 1][j];
        }
        if (j >= w[i]) {
          if (k[i] + dp[i - 1][j - w[i]] > dp[i][j]) {
            dp[i][j] = k[i] + dp[i - 1][j - w[i]];
          }
        }
      }
    }
  }
  cout << dp[N][M] << endl;
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int main() {
  int dp[120][10001] = {0}, w[1000] = {0}, k[1000] = {0}, N, M, i, j;
  cin >> N >> M;
  for (i = 1; i <= N; i++) {
    cin >> k[i] >> w[i];
  }
  for (i = 1; i <= N; i++) {
    for (j = 1; j <= M; j++) {
      if (i == 1) {
        if (j >= w[i]) {
          dp[i][j] = k[i];
          if (dp[i][j] <= dp[i - 1][j]) {
            dp[i][j] = dp[i - 1][j];
          }
        }

      } else {
        if (dp[i][j] <= dp[i - 1][j]) {
          dp[i][j] = dp[i - 1][j];
        }
        if (j >= w[i]) {
          if (k[i] + dp[i - 1][j - w[i]] > dp[i][j]) {
            dp[i][j] = k[i] + dp[i - 1][j - w[i]];
          }
        }
      }
    }
  }
  cout << dp[N][M] << endl;
  return 0;
}
Problem p02315 (C++)

Buggy submission (status: 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 + 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";
}


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; 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 (status: Runtime Error):
#include <iostream>
using namespace std;

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

int main(void) {

  cin >> N >> W;

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

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

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

  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;

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

int main(void) {

  cin >> N >> W;

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

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

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

  return 0;
}
Problem p02315 (C++)

Buggy submission (status: 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][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;
}


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][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;
}
Problem p02315 (C++)

Buggy submission (status: 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, 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;
}

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, 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;
}
Problem p02315 (C++)

Buggy submission (status: Runtime Error):
#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;
}

Fixed submission:
#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;
}