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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, s, n) for (int i = s; i < (n); ++i)

using namespace std;

typedef long long ll;

static const int INTINF = (2147483647);
static const ll LLINF = (9223372036854775807);
static const int MAX = 100001;
static const ll MOD = 1000000007;

//--global--//

//----------//

void print2dArray(vector<vector<ll>> vv, int a) {
  rep(i, vv.size()) {
    rep(j, a) {
      if (i)
        cout << " ";
      cout << vv[i][j];
    }
    cout << endl;
  }
}

int main(int argc, const char *argv[]) {
  // 提出時、消す----//
  //--------------//

  int n, w;
  cin >> n >> w;
  vector<vector<ll>> vv(n + 1, vector<ll>(MAX + 1));
  vector<ll> W(n);
  vector<ll> V(n);
  rep(i, n + 1) { fill(vv[i].begin(), vv[i].end(), -1); }

  rep(i, n) { cin >> W[i] >> V[i]; }

  vv[0][0] = 0;
  rep2(i, 1, n + 1) {
    vv[i][W[i - 1]] = max(vv[i][W[i - 1]], V[i - 1]);
    rep(j, w + 1) {
      if (vv[i - 1][j] != -1) {
        vv[i][j] = max(vv[i - 1][j], vv[i][j]);
        vv[i][j + W[i - 1]] = max(vv[i][j + W[i - 1]], vv[i - 1][j] + V[i - 1]);
      }
    }
  }

  // print2dArray(vv, w+1);

  ll res = 0;
  rep(i, w + 1) { res = max(vv[n][i], res); }

  cout << res << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, s, n) for (int i = s; i < (n); ++i)

using namespace std;

typedef long long ll;

static const int INTINF = (2147483647);
static const ll LLINF = (9223372036854775807);
static const int MAX = 100001;
static const ll MOD = 1000000007;

//--global--//

//----------//

void print2dArray(vector<vector<ll>> vv, int a) {
  rep(i, vv.size()) {
    rep(j, a) {
      if (i)
        cout << " ";
      cout << vv[i][j];
    }
    cout << endl;
  }
}

int main(int argc, const char *argv[]) {
  // 提出時、消す----//
  //--------------//

  int n, w;
  cin >> n >> w;
  vector<vector<ll>> vv(n + 1, vector<ll>(MAX + 1));
  vector<ll> W(n);
  vector<ll> V(n);
  rep(i, n + 1) { fill(vv[i].begin(), vv[i].end(), -1); }

  rep(i, n) { cin >> W[i] >> V[i]; }

  vv[0][0] = 0;
  rep2(i, 1, n + 1) {
    vv[i][W[i - 1]] = max(vv[i][W[i - 1]], V[i - 1]);
    rep(j, w + 1) {
      if (vv[i - 1][j] != -1) {
        vv[i][j] = max(vv[i - 1][j], vv[i][j]);
        if (j + W[i - 1] <= w)
          vv[i][j + W[i - 1]] =
              max(vv[i][j + W[i - 1]], vv[i - 1][j] + V[i - 1]);
      }
    }
  }

  // print2dArray(vv, w+1);

  ll res = 0;
  rep(i, w + 1) { res = max(vv[n][i], res); }

  cout << res << endl;

  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

long long dp[100][100010] = {0}; // N-1番目までで入れた容量の価値を最大にするもの
long long weight[110], value[110];

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

  for (int i = 0; i < N; ++i) {
    for (int sum_W = 0; sum_W <= W; ++sum_W) {
      // i番目の商品を選ぶ
      if (sum_W - weight[i] >= 0) {
        if (dp[i + 1][sum_W] < dp[i][sum_W - weight[i]] + value[i]) {
          dp[i + 1][sum_W] = dp[i][sum_W - weight[i]] + value[i];
        }
      }

      if (dp[i + 1][sum_W] < dp[i][sum_W]) {
        dp[i + 1][sum_W] = dp[i][sum_W];
      }
    }
  }
  cout << dp[N][W] << endl;

  return 0;
}

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

long long dp[110][100010] = {0}; // N-1番目までで入れた容量の価値を最大にするもの
long long weight[110], value[110];

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

  for (int i = 0; i < N; ++i) {
    for (int sum_W = 0; sum_W <= W; ++sum_W) {
      // i番目の商品を選ぶ
      if (sum_W - weight[i] >= 0) {
        if (dp[i + 1][sum_W] < dp[i][sum_W - weight[i]] + value[i]) {
          dp[i + 1][sum_W] = dp[i][sum_W - weight[i]] + value[i];
        }
      }

      if (dp[i + 1][sum_W] < dp[i][sum_W]) {
        dp[i + 1][sum_W] = dp[i][sum_W];
      }
    }
  }
  cout << dp[N][W] << endl;

  return 0;
}
Problem p03163 (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++)
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;
const ll MOD = 1000000007;
ll dp[2][100000 + 100];
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
int main() {
  int N, W;
  cin >> N >> W;
  vector<ll> w(N), v(N);
  REP(i, N) cin >> w[i] >> v[i];
  ll *prev = dp[0], *next = dp[1];
  REP(i, N) {
    REP(j, W + 1) {
      // chmax(next[j], prev[j]);
      chmax(next[j + w[i]], prev[j] + v[i]);
      chmax(next[j], prev[j]);
    }
    swap(prev, next);
  }
  cout << prev[W] << endl;
  int t = 10;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;
const ll MOD = 1000000007;
ll dp[2][100000 + 100];
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
int main() {
  int N, W;
  cin >> N >> W;
  vector<ll> w(N), v(N);
  REP(i, N) cin >> w[i] >> v[i];
  ll *prev = dp[0], *next = dp[1];
  REP(i, N) {
    REP(j, W + 1) {
      // chmax(next[j], prev[j]);
      if (j + w[i] < W + 10)
        chmax(next[j + w[i]], prev[j] + v[i]);
      chmax(next[j], prev[j]);
    }
    swap(prev, next);
  }
  cout << prev[W] << endl;
  int t = 10;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define PRINT(V) cout << V << "\n"
#define SORT(V) sort((V).begin(), (V).end())
#define RSORT(V) sort((V).rbegin(), (V).rend())
using namespace std;
using ll = long long;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
ll N, W;
ll w[105], v[105];
vector<vector<ll>> dp(105, vector<ll>(10005, 0));
void knapsack() {
  rep(i, N) rep(j, W) {
    if (j + w[i] <= W) {
      chmax(dp[i + 1][j + w[i]], dp[i][j] + v[i]);
    }
    chmax(dp[i + 1][j], dp[i][j]);
  }
}
int main() {
  cin >> N >> W;
  rep(i, N) cin >> w[i] >> v[i];
  knapsack();
  ll ans = 0;
  rep(i, W + 1) chmax(ans, dp[N][i]);
  PRINT(ans);
}


Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define PRINT(V) cout << V << "\n"
#define SORT(V) sort((V).begin(), (V).end())
#define RSORT(V) sort((V).rbegin(), (V).rend())
using namespace std;
using ll = long long;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
ll N, W;
ll w[105], v[105];
vector<vector<ll>> dp(105, vector<ll>(100005, 0));
void knapsack() {
  rep(i, N) rep(j, W) {
    if (j + w[i] <= W) {
      chmax(dp[i + 1][j + w[i]], dp[i][j] + v[i]);
    }
    chmax(dp[i + 1][j], dp[i][j]);
  }
}
int main() {
  cin >> N >> W;
  rep(i, N) cin >> w[i] >> v[i];
  knapsack();
  ll ans = 0;
  rep(i, W + 1) chmax(ans, dp[N][i]);
  PRINT(ans);
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
using namespace std;

ll t[105][10005];
vector<int> wt, val;

ll knapsack(int n, int w) {
  if (n == 0 || w == 0)
    return 0;

  if (t[n][w] != -1)
    return t[n][w];

  if (wt[n - 1] <= w) {
    return t[n][w] = max(val[n - 1] + knapsack(n - 1, w - wt[n - 1]),
                         knapsack(n - 1, w));
  } else
    return t[n][w] = knapsack(n - 1, w);
}

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

  memset(t, -1, sizeof(t));
  int n, w;
  cin >> n >> w;
  wt.resize(n);
  val.resize(n);

  for (int i = 0; i < n; i++)
    cin >> wt[i] >> val[i];

  cout << knapsack(n, w) << "\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;

ll t[105][100005];
vector<int> wt, val;

ll knapsack(int n, int w) {
  if (n == 0 || w == 0)
    return 0;

  if (t[n][w] != -1)
    return t[n][w];

  if (wt[n - 1] <= w) {
    return t[n][w] = max(val[n - 1] + knapsack(n - 1, w - wt[n - 1]),
                         knapsack(n - 1, w));
  } else
    return t[n][w] = knapsack(n - 1, w);
}

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

  memset(t, -1, sizeof(t));
  int n, w;
  cin >> n >> w;
  wt.resize(n);
  val.resize(n);

  for (int i = 0; i < n; i++)
    cin >> wt[i] >> val[i];

  cout << knapsack(n, w) << "\n";
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
const int inf = 1001001001;

int main() {

  int n, w;
  cin >> n >> w;
  vector<ll> v(n);
  vector<ll> m(n);
  rep(i, n) cin >> m.at(i) >> v.at(i);

  vector<vector<ll>> dp(100, vector<ll>(100000));
  for (int i = 0; i < n; i++) {
    for (int j = 1; j <= w; j++) {

      if (j - m.at(i) >= 0) {
        dp.at(i + 1).at(j) =
            max(dp.at(i + 1).at(j), dp.at(i).at(j - m.at(i)) + v.at(i));
      }
      dp.at(i + 1).at(j) = max(dp.at(i + 1).at(j), dp.at(i).at(j));
    }
  }

  cout << dp.at(n).at(w) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
const int inf = 1001001001;

int main() {

  int n, w;
  cin >> n >> w;
  vector<ll> v(n);
  vector<ll> m(n);
  rep(i, n) cin >> m.at(i) >> v.at(i);

  vector<vector<ll>> dp(110, vector<ll>(100010));
  for (int i = 0; i < n; i++) {
    for (int j = 1; j <= w; j++) {

      if (j - m.at(i) >= 0) {
        dp.at(i + 1).at(j) =
            max(dp.at(i + 1).at(j), dp.at(i).at(j - m.at(i)) + v.at(i));
      }
      dp.at(i + 1).at(j) = max(dp.at(i + 1).at(j), dp.at(i).at(j));
    }
  }

  cout << dp.at(n).at(w) << endl;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MN = 112;
ll n, c, w[MN], v[MN], dp[MN][MN];
int main(void) {
  cin >> n >> c;
  for (ll i = 0; i < n; i++)
    cin >> w[i] >> v[i];
  for (ll cc = 1; cc <= c; cc++) {
    for (ll i = 1; i <= n; i++) {
      ll cw = w[i - 1], cv = v[i - 1];
      if (cw > cc)
        dp[cc][i] = dp[cc][i - 1];
      else
        dp[cc][i] = max(dp[cc][i - 1], dp[cc - cw][i - 1] + cv);
    }
  }
  cout << dp[c][n] << "\n";
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MN = 112;
const int MW = 100012;
ll n, c, w[MN], v[MN], dp[MW][MN];
int main(void) {
  cin >> n >> c;
  for (ll i = 0; i < n; i++)
    cin >> w[i] >> v[i];
  for (ll cc = 1; cc <= c; cc++) {
    for (ll i = 1; i <= n; i++) {
      ll cw = w[i - 1], cv = v[i - 1];
      if (cw > cc)
        dp[cc][i] = dp[cc][i - 1];
      else
        dp[cc][i] = max(dp[cc][i - 1], dp[cc - cw][i - 1] + cv);
    }
  }
  cout << dp[c][n] << "\n";
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define f first
#define s second

#define MM INT_MAX / 5
#define MIM INT_MIN / 2

int di[] = {1, -1, 0, 0};
int dj[] = {0, 0, 1, -1};

#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i, a, b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i, a) for (int i = (a)-1; i >= 0; i--)

#define v vector
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define beg(x) x.begin()
#define en(x) x.end()
#define all(x) beg(x), en(x)
#define resz resize

#define MAX (int)1e5 + 1

ll vals[101];
int weights[MAX];
ll dp[101];

int main() {
  for (int i = 0; i < 101; i++)
    dp[i] = -1;
  int N, W;
  cin >> N >> W;
  for (int i = 1; i <= N; i++) {
    cin >> weights[i];
    cin >> vals[i];
  }
  dp[0] = 0;
  for (int i = 1; i <= N; i++)
    for (int j = W; j >= 0; j--) {
      if (dp[j] >= 0 && j + weights[i] <= W)
        dp[j + weights[i]] = max(dp[j] + vals[i], dp[j + weights[i]]);
    }
  ll ans = -1;
  for (int i = W; i > 0; i--)
    ans = max(ans, dp[i]);
  cout << ans;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define f first
#define s second

#define MM INT_MAX / 5
#define MIM INT_MIN / 2

int di[] = {1, -1, 0, 0};
int dj[] = {0, 0, 1, -1};

#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i, a, b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i, a) for (int i = (a)-1; i >= 0; i--)

#define v vector
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define beg(x) x.begin()
#define en(x) x.end()
#define all(x) beg(x), en(x)
#define resz resize

#define MAX (int)1e5 + 1

ll vals[101];
int weights[101];
ll dp[MAX];

int main() {
  for (int i = 0; i < 101; i++)
    dp[i] = -1;
  int N, W;
  cin >> N >> W;
  for (int i = 1; i <= N; i++) {
    cin >> weights[i];
    cin >> vals[i];
  }
  dp[0] = 0;
  for (int i = 1; i <= N; i++)
    for (int j = W; j >= 0; j--) {
      if (dp[j] >= 0 && j + weights[i] <= W)
        dp[j + weights[i]] = max(dp[j] + vals[i], dp[j + weights[i]]);
    }
  ll ans = -1;
  for (int i = W; i > 0; i--)
    ans = max(ans, dp[i]);
  cout << ans;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define sc second
#define fr first
#define mk make_pair
#define ll long long
#define ii pair<int, int>
#define mp make_pair
using namespace std;
int n;
ll memo[101][10005];
ll a[105];
ll w[105];
ll solve(int i, int sum) {
  if (i == n) {
    return 0;
  }
  ll &ret = memo[i][sum];
  if (ret != -1)
    return ret;
  ret = 0;
  if (sum >= w[i]) {
    ret = max(ret, a[i] + solve(i + 1, sum - w[i]));
  }
  ret = max(ret, solve(i + 1, sum));
  return ret;
}
int main() {
  memset(memo, -1, sizeof(memo));
  int x;
  cin >> n >> x;
  for (int i = 0; i < n; i++) {
    cin >> w[i] >> a[i];
  }
  cout << solve(0, x);
}


Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define sc second
#define fr first
#define mk make_pair
#define ll long long
#define ii pair<int, int>
#define mp make_pair
using namespace std;
int n;
ll memo[101][100005];
ll a[105];
ll w[105];
ll solve(int i, int sum) {
  if (i == n) {
    return 0;
  }
  ll &ret = memo[i][sum];
  if (ret != -1)
    return ret;
  ret = 0;
  if (sum >= w[i]) {
    ret = max(ret, a[i] + solve(i + 1, sum - w[i]));
  }
  ret = max(ret, solve(i + 1, sum));
  return ret;
}
int main() {
  memset(memo, -1, sizeof(memo));
  int x;
  cin >> n >> x;
  for (int i = 0; i < n; i++) {
    cin >> w[i] >> a[i];
  }
  cout << solve(0, x);
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define maxn 100
#define maxw 10001
int main() {
  long long n, w;
  cin >> n >> w;
  long long dp[maxn][maxw], weight[maxn], val[maxn];
  for (long long i = 0; i < n; i++)
    cin >> weight[i] >> val[i];
  for (long long i = 0; i <= w; i++)
    dp[0][i] = 0;
  dp[0][w - weight[0]] = val[0];
  for (long long i = 1; i < n; i++) {
    for (long long j = 0; j <= w; j++) {
      if (j + weight[i] > w)
        dp[i][j] = dp[i - 1][j];
      else
        dp[i][j] = max(val[i] + dp[i - 1][j + weight[i]], dp[i - 1][j]);
    }
  }
  long long max = 0;
  for (long long i = 0; i <= w; i++) {
    if (dp[n - 1][i] > max)
      max = dp[n - 1][i];
  }
  cout << max;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define maxn 100
#define maxw 100001
int main() {
  long long n, w;
  cin >> n >> w;
  long long dp[maxn][maxw], weight[maxn], val[maxn];
  for (long long i = 0; i < n; i++)
    cin >> weight[i] >> val[i];
  for (long long i = 0; i <= w; i++)
    dp[0][i] = 0;
  dp[0][w - weight[0]] = val[0];
  for (long long i = 1; i < n; i++) {
    for (long long j = 0; j <= w; j++) {
      if (j + weight[i] > w)
        dp[i][j] = dp[i - 1][j];
      else
        dp[i][j] = max(val[i] + dp[i - 1][j + weight[i]], dp[i - 1][j]);
    }
  }
  long long max = 0;
  for (long long i = 0; i <= w; i++) {
    if (dp[n - 1][i] > max)
      max = dp[n - 1][i];
  }
  cout << max;
  return 0;
}
Problem p03163 (C++)

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

template <class T> inline bool chmax(T &a, const T &b) {
  return (a < b) ? a = b, true : false;
}

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

  std::vector<int> w(N);
  std::vector<long long> v(N);
  for (int i = 0; i < N; ++i)
    std::cin >> w[i] >> v[i];

  // dp[i][j] = i 個までで重さ j 以下の場合の価値の最大値
  std::vector<std::vector<long long>> dp(N + 7,
                                         std::vector<long long>(W + 7, 0LL));

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

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


Fixed submission:
#include <iostream>
#include <vector>

template <class T> inline bool chmax(T &a, const T &b) {
  return (a < b) ? a = b, true : false;
}

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

  std::vector<int> w(N);
  std::vector<long long> v(N);
  for (int i = 0; i < N; ++i)
    std::cin >> w[i] >> v[i];

  // dp[i][j] = i 個までで重さ j 以下の場合の価値の最大値
  std::vector<std::vector<long long>> dp(
      N + 7, std::vector<long long>(2 * W + 7, 0LL));

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

  std::cout << dp[N][W] << std::endl;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

long long dp[610];
int w[610], v[610];
int N, W;

int main() {
  cin >> N >> W;
  for (int i = 1; i <= N; i++)
    cin >> w[i] >> v[i];
  dp[0] = 0;
  for (int i = 1; i <= N; i++) {
    for (int j = W - w[i]; j >= 0; j--) {
      dp[j + w[i]] = max(dp[j] + v[i], dp[j + w[i]]);
    }
  }
  cout << dp[W] << '\n';
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

long long dp[100100];
int w[100100], v[100100];
int N, W;

int main() {
  cin >> N >> W;
  for (int i = 1; i <= N; i++)
    cin >> w[i] >> v[i];
  dp[0] = 0;
  for (int i = 1; i <= N; i++) {
    for (int j = W - w[i]; j >= 0; j--) {
      dp[j + w[i]] = max(dp[j] + v[i], dp[j + w[i]]);
    }
  }
  cout << dp[W] << '\n';
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define f .first
#define s .second
using namespace std;
using ll = long long;
int n, W;
pair<ll, ll> item[105];
ll dp[10005], maxv;
int main() {
  cin >> n >> W;
  for (int i = 1; i <= n; ++i) {
    cin >> item[i] f >> item[i] s;
  }
  for (int i = 0; i <= W; ++i) {
    dp[i] = -1;
  }
  sort(item + 1, item + 1 + n);
  dp[0] = 0;
  for (int i = 1; i <= n; ++i) {
    for (int j = W; j >= item[i] f; --j) {
      if (dp[j - item[i] f] != -1) {
        dp[j] = max(dp[j], dp[j - item[i] f] + item[i] s);
      }
    }
  }
  for (int i = 1; i <= W; ++i) {
    maxv = max(maxv, dp[i]);
  }
  cout << maxv << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define f .first
#define s .second
using namespace std;
using ll = long long;
int n, W;
pair<ll, ll> item[100005];
ll dp[100005], maxv;
int main() {
  cin >> n >> W;
  for (int i = 1; i <= n; ++i) {
    cin >> item[i] f >> item[i] s;
  }
  for (int i = 0; i <= W; ++i) {
    dp[i] = -1;
  }
  sort(item + 1, item + 1 + n);
  dp[0] = 0;
  for (int i = 1; i <= n; ++i) {
    for (int j = W; j >= item[i] f; --j) {
      if (dp[j - item[i] f] != -1) {
        dp[j] = max(dp[j], dp[j - item[i] f] + item[i] s);
      }
    }
  }
  for (int i = 1; i <= W; ++i) {
    maxv = max(maxv, dp[i]);
  }
  cout << maxv << '\n';
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"
#define int long long
#define watch(x) cout << (#x) << " is " << (x) << endl
#define pb push_back
const int N = 1e18;

int32_t main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  IOS;

  int n, W, x, y;
  cin >> n >> W;
  int w[n + 1], v[n + 1], dp[n + 1][W + 1];
  for (int i = 1; i <= n; i++) {
    cin >> x >> y;
    w[i] = x;
    v[i] = y;
  }

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

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= W; j++) {
      if (w[i] > j) {
        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[n][W] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"
#define int long long
#define watch(x) cout << (#x) << " is " << (x) << endl
#define pb push_back
const int N = 1e18;

int32_t main() {

  IOS;

  int n, W, x, y;
  cin >> n >> W;
  int w[n + 1], v[n + 1], dp[n + 1][W + 1];
  for (int i = 1; i <= n; i++) {
    cin >> x >> y;
    w[i] = x;
    v[i] = y;
  }

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

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= W; j++) {
      if (w[i] > j) {
        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[n][W] << endl;
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

long N, W;
long w[100], v[100];
long dp[100][100000];

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

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

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

long N, W;
long w[110], v[110];
long dp[110][100100];

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

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

  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef int in;
#define int long long
int MAX = 1e9;
int MAXEST = 1e18;
int MOD = 1e9 + 7;

int n, w;
int v[105], s[105];
int dp[2][100005];

in main() {
  int tc = 1;
  //    cin>>tc;
  while (tc--) {
    cin >> n >> w;
    for (int i = 1; i <= n; i++) {
      cin >> s[i] >> v[i];
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j <= w; j++) {
        dp[i & 1][j] = dp[(i - 1) & 1][j];
        if (j >= s[i])
          dp[i & 1][j] = max(dp[i & 1][j], dp[(i - 1) & 1][j - s[i]] + v[i]);
      }
    }
    cout << dp[n][w] << endl;
  }
  return 0;
}


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

typedef int in;
#define int long long
int MAX = 1e9;
int MAXEST = 1e18;
int MOD = 1e9 + 7;

int n, w;
int v[105], s[105];
int dp[2][100005];

in main() {
  int tc = 1;
  //    cin>>tc;
  while (tc--) {
    cin >> n >> w;
    for (int i = 1; i <= n; i++) {
      cin >> s[i] >> v[i];
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j <= w; j++) {
        dp[i & 1][j] = dp[(i - 1) & 1][j];
        if (j >= s[i])
          dp[i & 1][j] = max(dp[i & 1][j], dp[(i - 1) & 1][j - s[i]] + v[i]);
      }
    }
    cout << dp[n & 1][w] << endl;
  }
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
typedef long long ll;
ll dp[105][105], weight[105], value[105];
int n, cap;
int main() {
  cin >> n >> cap;
  for (int i = 1; i <= n; i++) {
    ll x, y;
    cin >> x >> y;
    weight[i] = x;
    value[i] = y;
  }
  for (int i = 1; i <= n; i++) {
    ll w = weight[i], v = value[i];
    for (int j = 1; j <= cap; j++) {
      if (j >= w) {
        ll hold = dp[i - 1][j - w] + v;
        dp[i][j] = dp[i - 1][j] > hold ? dp[i - 1][j] : hold;
      } else {
        dp[i][j] = dp[i - 1][j];
      }
    }
  }
  cout << dp[n][cap] << endl;
  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;
typedef long long ll;
ll dp[105][100005], weight[105], value[105];
int n, cap;
int main() {
  cin >> n >> cap;
  for (int i = 1; i <= n; i++) {
    ll x, y;
    cin >> x >> y;
    weight[i] = x;
    value[i] = y;
  }
  for (int i = 1; i <= n; i++) {
    ll w = weight[i], v = value[i];
    for (int j = 1; j <= cap; j++) {
      if (j >= w) {
        ll hold = dp[i - 1][j - w] + v;
        dp[i][j] = dp[i - 1][j] > hold ? dp[i - 1][j] : hold;
      } else {
        dp[i][j] = dp[i - 1][j];
      }
    }
  }
  cout << dp[n][cap] << endl;
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>
using namespace std;

long long knapsack(int *w, int *v, vector<vector<long long>> &dp, int i,
                   int maxw) {
  if (maxw <= 0)
    return 0;
  if (i < 0)
    return 0;

  if (dp[i][maxw] != -1)
    return dp[i][maxw];

  if (w[i] <= maxw) {
    dp[i][maxw] = max(knapsack(w, v, dp, i - 1, maxw),
                      knapsack(w, v, dp, i - 1, maxw - w[i]) + v[i]);
  } else {
    dp[i][maxw] = knapsack(w, v, dp, i - 1, maxw);
  }
  return dp[i][maxw];
}

int main() {
  int n;
  cin >> n;
  int maxw;
  cin >> maxw;
  vector<long long> t(10001, -1);
  vector<vector<long long>> dp(101, t);
  int w[n];
  int v[n];
  for (int i = 0; i < n; i++) {
    cin >> w[i] >> v[i];
  }
  cout << knapsack(w, v, dp, n - 1, maxw);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
using namespace std;

long long knapsack(int *w, int *v, vector<vector<long long>> &dp, int i,
                   int maxw) {
  if (maxw <= 0)
    return 0;
  if (i < 0)
    return 0;

  if (dp[i][maxw] != -1)
    return dp[i][maxw];

  if (w[i] <= maxw) {
    dp[i][maxw] = max(knapsack(w, v, dp, i - 1, maxw),
                      knapsack(w, v, dp, i - 1, maxw - w[i]) + v[i]);
  } else {
    dp[i][maxw] = knapsack(w, v, dp, i - 1, maxw);
  }
  return dp[i][maxw];
}

int main() {
  int n;
  cin >> n;
  int maxw;
  cin >> maxw;
  vector<long long> t(100001, -1);
  vector<vector<long long>> dp(101, t);
  int w[n];
  int v[n];
  for (int i = 0; i < n; i++) {
    cin >> w[i] >> v[i];
  }
  cout << knapsack(w, v, dp, n - 1, maxw);
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define ld long double
typedef pair<ll, ll> P;
#define boost                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
bool comp(pair<ll, ll> p1, pair<ll, ll> p2) {
  if (p1.first < p2.first)
    return true;
  else if (p1.first == p2.first && p1.second >= p2.second)
    return true;
  return false;
}
int main() {
  ll n, w, i, a, b, j;
  cin >> n >> w;
  vector<pair<ll, ll>> V;
  for (i = 0; i < n; i++) {
    cin >> a >> b;
    V.push_back(make_pair(a, b));
  }
  sort(V.begin(), V.end(), comp);
  ll DP[n + 1][w + 1];
  memset(DP, 0, sizeof(DP));
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= w; j++) {
      if (j >= V[i - 1].first) {
        DP[i][j] = max(max(DP[i - 1][j], DP[i][j - 1]),
                       V[i - 1].second + DP[i - 1][j - V[i - 1].first]);
      } else {
        DP[i][j] = max(DP[i - 1][j], DP[i][j - 1]);
      }
    }
  }
  /*
  for(i=0;i<=n;i++)
  {
  for(j=0;j<=w;j++)
  {
      cout<<DP[i][j]<<" ";
  }
  cout<<endl;
  } */
  cout << DP[n][w] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define ld long double
typedef pair<ll, ll> P;
#define boost                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
bool comp(pair<ll, ll> p1, pair<ll, ll> p2) {
  if (p1.first < p2.first)
    return true;
  else if (p1.first == p2.first && p1.second >= p2.second)
    return true;
  return false;
}
int main() {
  ll n, w, i, a, b, j;
  cin >> n >> w;
  vector<pair<ll, ll>> V;
  for (i = 0; i < n; i++) {
    cin >> a >> b;
    V.push_back(make_pair(a, b));
  }
  sort(V.begin(), V.end());
  ll DP[n + 1][w + 1];
  memset(DP, 0, sizeof(DP));
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= w; j++) {
      if (j >= V[i - 1].first) {
        DP[i][j] = max(max(DP[i - 1][j], DP[i][j - 1]),
                       V[i - 1].second + DP[i - 1][j - V[i - 1].first]);
      } else {
        DP[i][j] = max(DP[i - 1][j], DP[i][j - 1]);
      }
    }
  }
  /*
  for(i=0;i<=n;i++)
  {
  for(j=0;j<=w;j++)
  {
      cout<<DP[i][j]<<" ";
  }
  cout<<endl;
  } */
  cout << DP[n][w] << endl;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long int lmax(long long int a, long long int b) {
  if (a < b)
    return b;
  else
    return a;
}
int main() {
  long long int n, W;
  cin >> n >> W;
  vector<long long int> w(n);
  vector<long long int> v(n);
  for (long long int i = 0; i < n; i++) {
    cin >> w[i] >> v[i];
  }

  vector<vector<long long int>> dp(n + 1, vector<long long int>(W, 0));

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

  cout << dp[n][W] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long int lmax(long long int a, long long int b) {
  if (a < b)
    return b;
  else
    return a;
}
int main() {
  long long int n, W;
  cin >> n >> W;
  vector<long long int> w(n);
  vector<long long int> v(n);
  for (long long int i = 0; i < n; i++) {
    cin >> w[i] >> v[i];
  }

  vector<vector<long long int>> dp(n + 1, vector<long long int>(W + 1, 0));

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

  cout << dp[n][W] << endl;
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>
#define ll long long
#define MOD 1000000007
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll s, n;
  cin >> s >> n;
  ll size[n];
  ll value[n];
  for (ll i = 0; i < n; i++)
    cin >> size[i] >> value[i];
  ll dp[n + 1][s + 1];
  for (ll i = 0; i < n + 1; i++) {
    for (ll j = 0; j < s + 1; j++) {
      if (i == 0 || j == 0)
        dp[i][j] = 0;
    }
  }
  for (ll i = 1; i < n + 1; i++) {
    for (ll j = 1; j < s + 1; j++) {
      if (size[i - 1] <= j) {
        // cout<<value[i-1]<<" value "<< dp[i-1][j-size[i-1]]<<endl;
        // cout<<value[i-1]+dp[i-1][j-value[i-1]]<<" val "<<dp[i-1][j]<<endl;
        dp[i][j] = max(value[i - 1] + dp[i - 1][j - size[i - 1]], dp[i - 1][j]);
      } else
        dp[i][j] = dp[i - 1][j];
      // cout<<dp[i][j]<<" ";
    }
    // cout<<endl;
  }
  /*for(ll i=0;i<n+1;i++)
  {
      for(ll j=0;j<s+1;j++)
      {
          cout<<dp[i][j]<<" "<<;
      }
      cout<<endl;
  }*/
  cout << dp[n][s];
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
#define ll long long
#define MOD 1000000007
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n, s;
  cin >> n >> s;
  ll size[n];
  ll value[n];
  for (ll i = 0; i < n; i++)
    cin >> size[i] >> value[i];
  ll dp[n + 1][s + 1];
  for (ll i = 0; i < n + 1; i++) {
    for (ll j = 0; j < s + 1; j++) {
      if (i == 0 || j == 0)
        dp[i][j] = 0;
    }
  }
  for (ll i = 1; i < n + 1; i++) {
    for (ll j = 1; j < s + 1; j++) {
      if (size[i - 1] <= j) {
        // cout<<value[i-1]<<" value "<< dp[i-1][j-size[i-1]]<<endl;
        // cout<<value[i-1]+dp[i-1][j-value[i-1]]<<" val "<<dp[i-1][j]<<endl;
        dp[i][j] = max(value[i - 1] + dp[i - 1][j - size[i - 1]], dp[i - 1][j]);
      } else
        dp[i][j] = dp[i - 1][j];
      // cout<<dp[i][j]<<" ";
    }
    // cout<<endl;
  }
  /*for(ll i=0;i<n+1;i++)
  {
      for(ll j=0;j<s+1;j++)
      {
          cout<<dp[i][j]<<" "<<;
      }
      cout<<endl;
  }*/
  cout << dp[n][s];
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
using llong = long long;

llong N, W;
llong w[105], v[105];
llong dp[105][105];

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

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

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

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
using llong = long long;

llong N, W;
llong w[105], v[105];
llong dp[105][100005];

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

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

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

  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
// #include <stdio.h>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <utility>
#include <vector>

using namespace std;

#define ll long long
vector<vector<ll>> dp;
vector<ll> w, v;
ll N, W;

ll res(ll i, ll j) {
  if (i >= N) {
    return 0;
  }

  if (dp.at(i).at(j) != -1) {
    return dp.at(i).at(j);
  }
  ll ret;
  if (w.at(i) > j) {
    ret = res(i + 1, j);
  } else {
    ret = max(res(i + 1, j), res(i + 1, j - w.at(i)) + v.at(i));
  }

  return dp.at(i).at(j) = ret;
}

void solve() {
  cin >> N >> W;
  dp = vector<vector<ll>>(N, vector<ll>(100000, -1));
  w = vector<ll>(N);
  v = vector<ll>(N);
  for (ll i = 0; i < N; i++) {
    cin >> w.at(i) >> v.at(i);
  }

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

  return;
}

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


Fixed submission:
// #include <stdio.h>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <utility>
#include <vector>

using namespace std;

#define ll long long
vector<vector<ll>> dp;
vector<ll> w, v;
ll N, W;

ll res(ll i, ll j) {
  if (i >= N) {
    return 0;
  }

  if (dp.at(i).at(j) != -1) {
    return dp.at(i).at(j);
  }
  ll ret;
  if (w.at(i) > j) {
    ret = res(i + 1, j);
  } else {
    ret = max(res(i + 1, j), res(i + 1, j - w.at(i)) + v.at(i));
  }

  return dp.at(i).at(j) = ret;
}

void solve() {
  cin >> N >> W;
  dp = vector<vector<ll>>(N, vector<ll>(1000000, -1));
  w = vector<ll>(N);
  v = vector<ll>(N);
  for (ll i = 0; i < N; i++) {
    cin >> w.at(i) >> v.at(i);
  }

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

  return;
}

int main() {
  solve();
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;

int main() {
  // 入力
  ll N, W;
  cin >> N >> W;
  ll w[110];
  ll v[110];
  for (ll i = 0; i < N; i++)
    cin >> w[i] >> v[i];

  // dp,初期化
  ll dp[110][1000000] = {
      0}; // dp[i][sum_w] =
          // i番目未満の品物(iは含まない)を用い、sum_w以下で最大のvalue

  // index整理（0始まりとして）
  // dp[i] = i番目未満の品物（iは含まない）
  // w[i] = i番目の品物の重さ
  // v[i] = i番目の品物の価値

  // ループ
  // dp[i+1][sum_w]へは、
  // dp[i][sum_w - w[i]]から遷移する。(ただし、sum_w - w[i] >= 0)
  for (ll i = 0; i < N; i++) { // i番目までの品物から選ぶ
    for (ll sum_w = 0; sum_w <= W; sum_w++) { // sum_w以下で、選べるか
      if (sum_w - w[i] >=
          0) { // 今回の品物の重さw[i]を足して、sum_wを超えないかチェック
        chmax(dp[i + 1][sum_w],
              dp[i][sum_w - w[i]] +
                  v[i]); // 超えないなら、品物 iの価値（v[i]）足してみる
      }
      chmax(dp[i + 1][sum_w], dp[i][sum_w]); // 選ばない場合
    }
  }

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;

int main() {
  // 入力
  ll N, W;
  cin >> N >> W;
  ll w[110];
  ll v[110];
  for (ll i = 0; i < N; i++)
    cin >> w[i] >> v[i];

  // dp,初期化
  ll dp[110][100100] = {
      0}; // dp[i][sum_w] =
          // i番目未満の品物(iは含まない)を用い、sum_w以下で最大のvalue

  // index整理（0始まりとして）
  // dp[i] = i番目未満の品物（iは含まない）
  // w[i] = i番目の品物の重さ
  // v[i] = i番目の品物の価値

  // ループ
  // dp[i+1][sum_w]へは、
  // dp[i][sum_w - w[i]]から遷移する。(ただし、sum_w - w[i] >= 0)
  for (ll i = 0; i < N; i++) { // i番目までの品物から選ぶ
    for (ll sum_w = 0; sum_w <= W; sum_w++) { // sum_w以下で、選べるか
      if (sum_w - w[i] >=
          0) { // 今回の品物の重さw[i]を足して、sum_wを超えないかチェック
        chmax(dp[i + 1][sum_w],
              dp[i][sum_w - w[i]] +
                  v[i]); // 超えないなら、品物 iの価値（v[i]）足してみる
      }
      chmax(dp[i + 1][sum_w], dp[i][sum_w]); // 選ばない場合
    }
  }

  cout << dp[N][W] << endl;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
// Subhash Suman
// Description :

#include <bits/stdc++.h>
#define ll long long int
#define pll pair<ll, ll>
#define pii pair<int, int>

using namespace std;
const int N = 100, W = 1e5 + 5;
ll n, w, dp[N][W], arr1[N], arr2[N];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> w;
  for (int i = 1; i <= n; i++) {
    cin >> arr1[i] >> arr2[i];
  }

  for (int i = 1; i <= n; i++) {
    for (int wi = 1; wi <= w; wi++) {
      if (wi - arr1[i] >= 0) {
        dp[i][wi] = max(dp[i][wi], dp[i - 1][wi - arr1[i]] + arr2[i]);
      }
      dp[i][wi] = max(dp[i][wi], dp[i - 1][wi]);
      dp[i][wi] = max(dp[i][wi], dp[i][wi - 1]);
    }
  }
  cout << dp[n][w] << '\n';
  return 0;
}

Fixed submission:
// Subhash Suman
// Description :

#include <bits/stdc++.h>
#define ll long long int
#define pll pair<ll, ll>
#define pii pair<int, int>

using namespace std;
const int N = 105, W = 1e5 + 5;
ll n, w, dp[N][W], arr1[N], arr2[N];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> w;
  for (int i = 1; i <= n; i++) {
    cin >> arr1[i] >> arr2[i];
  }

  for (int i = 1; i <= n; i++) {
    for (int wi = 1; wi <= w; wi++) {
      if (wi - arr1[i] >= 0) {
        dp[i][wi] = max(dp[i][wi], dp[i - 1][wi - arr1[i]] + arr2[i]);
      }
      dp[i][wi] = max(dp[i][wi], dp[i - 1][wi]);
      dp[i][wi] = max(dp[i][wi], dp[i][wi - 1]);
    }
  }
  cout << dp[n][w] << '\n';
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
#pragma GCC optimize("Ofast")

// Begin {{{
using namespace std;

#ifndef DEBUG
#define dump(...)
#endif

template <class A, class B> inline bool chmax(A &a, const B &b) {
  return b > a && (a = b, true);
}
template <class A, class B> inline bool chmin(A &a, const B &b) {
  return b < a && (a = b, true);
}

template <class T> inline vector<T> make_v(const T &initvalue, size_t sz) {
  return vector<T>(sz, initvalue);
}

template <class T, class... Args>
inline auto make_v(const T &initvalue, size_t sz, Args... args) {
  return vector<decltype(make_v<T>(initvalue, args...))>(
      sz, make_v<T>(initvalue, args...));
}

constexpr size_t operator""_sz(unsigned long long value) { return value; };
constexpr intmax_t operator""_im(unsigned long long value) { return value; };
constexpr uintmax_t operator""_um(unsigned long long value) { return value; };
// }}} End

size_t N, W;
vector<int64_t> w(110), v(110);

auto dp = make_v(-1_im, 110, int(1e5) + 5);

intmax_t rec(size_t idx, int64_t weight) {
  if (idx >= N)
    return 0;
  if (weight + w[idx] > W)
    return rec(idx + 1, weight);
  if (~dp[idx][weight])
    return dp[idx][weight];
  return max(rec(idx + 1, weight + w[idx]) + v[idx], rec(idx + 1, weight));
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

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

  cout << rec(0, 0) << "\n";

  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
#pragma GCC optimize("Ofast")

// Begin {{{
using namespace std;

#ifndef DEBUG
#define dump(...)
#endif

template <class A, class B> inline bool chmax(A &a, const B &b) {
  return b > a && (a = b, true);
}
template <class A, class B> inline bool chmin(A &a, const B &b) {
  return b < a && (a = b, true);
}

template <class T> inline vector<T> make_v(const T &initvalue, size_t sz) {
  return vector<T>(sz, initvalue);
}

template <class T, class... Args>
inline auto make_v(const T &initvalue, size_t sz, Args... args) {
  return vector<decltype(make_v<T>(initvalue, args...))>(
      sz, make_v<T>(initvalue, args...));
}

constexpr size_t operator""_sz(unsigned long long value) { return value; };
constexpr intmax_t operator""_im(unsigned long long value) { return value; };
constexpr uintmax_t operator""_um(unsigned long long value) { return value; };
// }}} End

size_t N, W;
vector<int64_t> w(110), v(110);

auto dp = make_v(-1_im, 110, int(1e5) + 5);

intmax_t rec(size_t idx, int64_t weight) {
  if (idx >= N)
    return 0;
  if (weight + w[idx] > W)
    return rec(idx + 1, weight);
  if (~dp[idx][weight])
    return dp[idx][weight];
  return dp[idx][weight] =
             max(rec(idx + 1, weight + w[idx]) + v[idx], rec(idx + 1, weight));
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

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

  cout << rec(0, 0) << "\n";

  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define intt long long

const int N = 105;
const int NN = 10005;

int n, W;

int v[N], w[N];

intt dp[NN];

int main() {

  cin >> n >> W;

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

  for (int k = 1; k <= n; k++) {
    for (int x = W; x >= 0; x--) {
      if (x - w[k] >= 0) {
        dp[x] = max(dp[x], v[k] + dp[x - w[k]]);
      }
    }
  }

  cout << dp[W] << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define intt long long

const int N = 105;
const int NN = 100005;

int n, W;

int v[N], w[N];

intt dp[NN];

int main() {

  cin >> n >> W;

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

  for (int k = 1; k <= n; k++) {
    for (int x = W; x >= 0; x--) {
      if (x - w[k] >= 0) {
        dp[x] = max(dp[x], v[k] + dp[x - w[k]]);
      }
    }
  }

  cout << dp[W] << '\n';
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
// It's time to be who I am, rather than who I am supposed to be.
#include <bits/stdc++.h>
#define int long long
using namespace std;

const int inf = 1e12;
const int N = 105;
const int W = 1e5 + 5;

int n, total, weight[N], value[N], dp[N][W];

int solve(int item, int remain) {
  if (remain < 0)
    return -inf;
  if (remain == 0 || item == 0)
    return 0;

  int &ans = dp[item][remain];
  if (ans != 0)
    return ans;

  int take = value[item] + solve(item - 1, remain - weight[item]);
  int dont_take = solve(item - 1, remain);

  ans = max(take, dont_take);

  return ans;
}

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  cin >> n >> total;
  for (int i = 1; i <= n; i++)
    cin >> weight[i] >> value[i];

  cout << solve(n, total) << '\n';
}


Fixed submission:
// It's time to be who I am, rather than who I am supposed to be.
#include <bits/stdc++.h>
#define int long long
using namespace std;

const int inf = 1e12;
const int N = 105;
const int W = 1e5 + 5;

int n, total, weight[N], value[N], dp[N][W];

int solve(int item, int remain) {
  if (remain < 0)
    return -inf;
  if (remain == 0 || item == 0)
    return 0;

  int &ans = dp[item][remain];
  if (ans != 0)
    return ans;

  int take = value[item] + solve(item - 1, remain - weight[item]);
  int dont_take = solve(item - 1, remain);

  ans = max(take, dont_take);

  return ans;
}

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  cin >> n >> total;
  for (int i = 1; i <= n; i++)
    cin >> weight[i] >> value[i];

  cout << solve(n, total) << '\n';
}