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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define rep(a, b, c) for (int a = (int)b; a < (int)c; a++)
#define repk(a, b, c, k) for (int a = (int)b; a < (int)c; a += (int)k)
#define comeback                                                               \
  std::ios_base::sync_with_stdio(false);                                       \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define mod 1000000007
#define int long long int
#define pii pair<int, int>
#define ful(a) a.begin(), a.end()
#define ub upper_bound
#define lb lower_bound
#define ff first
#define ss second
using namespace std;
int em(int x, int n) {
  if (n == 0)
    return 1;
  else if (n % 2 == 0)
    return em((x % mod * x % mod) % mod, n / 2);
  else
    return (x % mod * em((x % mod * x % mod) % mod, (n - 1) / 2)) % mod;
}
signed main() {
  comeback
#ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
  freopen("output1.txt", "w", stdout);
#endif
  int n, W, i, j, k, l;
  cin >> n >> W;
  int w[n + 1], va[n + 1];
  for (i = 1; i <= n; i++) {
    cin >> w[i] >> va[i];
  }
  int dp[n + 1][W + 1];
  memset(dp, -1, sizeof(dp));
  for (i = 0; i <= W; i++)
    dp[0][i] = 0;
  for (i = 1; i <= n; i++) {
    for (j = 0; j <= W; j++) {
      if (w[i] <= j)
        dp[i][j] = max(va[i] + dp[i - 1][j - w[i]], dp[i - 1][j]);
      else
        dp[i][j] = dp[i - 1][j];
    }
  }
  cout << dp[n][W];
}

Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define rep(a, b, c) for (int a = (int)b; a < (int)c; a++)
#define repk(a, b, c, k) for (int a = (int)b; a < (int)c; a += (int)k)
#define comeback                                                               \
  std::ios_base::sync_with_stdio(false);                                       \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define mod 1000000007
#define int long long int
#define pii pair<int, int>
#define ful(a) a.begin(), a.end()
#define ub upper_bound
#define lb lower_bound
#define ff first
#define ss second
using namespace std;
int em(int x, int n) {
  if (n == 0)
    return 1;
  else if (n % 2 == 0)
    return em((x % mod * x % mod) % mod, n / 2);
  else
    return (x % mod * em((x % mod * x % mod) % mod, (n - 1) / 2)) % mod;
}
signed main() {
  comeback
      /*#ifndef ONLINE_JUDGE
              freopen("input.txt","r",stdin);
              freopen("output1.txt","w",stdout);
      #endif*/
      int n,
      W, i, j, k, l;
  cin >> n >> W;
  int w[n + 1], va[n + 1];
  for (i = 1; i <= n; i++) {
    cin >> w[i] >> va[i];
  }
  int dp[n + 1][W + 1];
  memset(dp, -1, sizeof(dp));
  for (i = 0; i <= W; i++)
    dp[0][i] = 0;
  for (i = 1; i <= n; i++) {
    for (j = 0; j <= W; j++) {
      if (w[i] <= j)
        dp[i][j] = max(va[i] + dp[i - 1][j - w[i]], dp[i - 1][j]);
      else
        dp[i][j] = dp[i - 1][j];
    }
  }
  cout << dp[n][W];
}
Problem p03163 (C++)

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int n, w;
  int value[100], weight[100];
  long int bag[101][100005] = {0};
  cin >> n;
  cin >> w;
  for (int i = 0; i < n; i++) {
    cin >> weight[i] >> value[i];
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= w; j++) {
      if (weight[i] <= j) {
        bag[i + 1][j] = max(bag[i][j - weight[i]] + value[i], bag[i][j]);
      } else {
        bag[i + 1][j] = bag[i][j];
      }
    }
  }
  cout << bag[n][w] << endl;
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
#define int long long
using namespace std;

int n, w;
int dp[100001][1001];
int v[100001][1001];
int w1[100001];
int v1[100001];

int s1(int wr, int i) {
  if (wr < 0) {
    return INT_MIN;
  }
  if (i == n + 1) {
    return 0;
  }
  if (v[wr][i]) {
    return dp[wr][i];
  }
  v[wr][i] = 1;

  dp[wr][i] = max(v1[i] + s1(wr - w1[i], i + 1), s1(wr, i + 1));

  return dp[wr][i];
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  // cin>>t;
  // int k=1;
  while (t--) {
    cin >> n >> w;

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

    cout << s1(w, 1) << '\n';
  }
}

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

int n, w;
int dp[100001][101];
int v[100001][101];
int w1[101];
int v1[101];

int s1(int wr, int i) {
  if (wr < 0) {
    return INT_MIN;
  }
  if (i == n + 1) {
    return 0;
  }
  if (v[wr][i]) {
    return dp[wr][i];
  }
  v[wr][i] = 1;

  dp[wr][i] = max(v1[i] + s1(wr - w1[i], i + 1), s1(wr, i + 1));

  return dp[wr][i];
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  // cin>>t;
  // int k=1;
  while (t--) {
    cin >> n >> w;

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

    cout << s1(w, 1) << '\n';
  }
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define int long long int
#define mod 1000000007
#define inf (1LL << 60)
#define f(i, a, b) for (int i = a; i < b; ++i)
using namespace std;

signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, w;
  cin >> n >> w;
  pair<int, int> p[n];
  for (int i = 0; i < n; i++)
    cin >> p[i].first >> p[i].second;
  int dp[n + 1][w + 1];
  memset(dp, 0, sizeof dp);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= w; j++) {
      dp[i][j] = dp[i - 1][j];
      if (p[i].first >= j)
        dp[i][j] = max(dp[i][j], p[i].second + dp[i - 1][j - p[i].first]);
    }
  }
  cout << dp[n][w];
}


Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define int long long int
#define mod 1000000007
#define inf (1LL << 60)
#define f(i, a, b) for (int i = a; i < b; ++i)
using namespace std;

signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, w;
  cin >> n >> w;
  pair<int, int> p[n];
  for (int i = 0; i < n; i++)
    cin >> p[i].first >> p[i].second;
  int dp[n + 1][w + 1];
  memset(dp, 0, sizeof dp);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= w; j++) {
      dp[i][j] = dp[i - 1][j];
      if (p[i - 1].first <= j)
        dp[i][j] =
            max(dp[i][j], p[i - 1].second + dp[i - 1][j - p[i - 1].first]);
    }
  }
  cout << dp[n][w];
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <iterator>
#include <queue>
#include <stack>
#include <vector>
typedef long long ll;
using namespace std;
#define rep(x, y) for (ll i = x; i <= y; i++)
#define repi(x, y) for (ll i = x; i >= y; i--)
priority_queue<ll> cancel;
ll weight[200005];
ll value[200005];
int main() {
  ll test = 1;
  // cin>>test;
  for (ll z = 0; z < test; z++) {
    ll n, W;
    cin >> n >> W;
    ll dp[n][W];
    for (ll i = 1; i <= n; i++) {
      cin >> weight[i];
      cin >> value[i];
    }

    for (ll i = 0; i <= n; i++) {
      dp[0][i] = 0;
    }

    for (ll i = 1; i <= W; i++) {
      dp[i][0] = 0;
    }

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

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <iterator>
#include <queue>
#include <stack>
#include <vector>
typedef long long ll;
using namespace std;
#define rep(x, y) for (ll i = x; i <= y; i++)
#define repi(x, y) for (ll i = x; i >= y; i--)
priority_queue<ll> cancel;
ll weight[200005];
ll value[200005];
int main() {
  ll test = 1;
  // cin>>test;
  for (ll z = 0; z < test; z++) {
    ll n, W;
    cin >> n >> W;
    ll dp[W + 5][n + 5];
    for (ll i = 1; i <= n; i++) {
      cin >> weight[i];
      cin >> value[i];
    }

    for (ll i = 0; i <= n; i++) {
      dp[0][i] = 0;
    }

    for (ll i = 1; i <= W; i++) {
      dp[i][0] = 0;
    }

    for (ll i = 0; i <= W; i++) {
      for (ll j = 1; j <= n; j++) {
        if (weight[j] > i) {
          dp[i][j] = dp[i][j - 1];
        } else {
          dp[i][j] = max(dp[i][j - 1], dp[i - weight[j]][j - 1] + value[j]);
        }
      }
    }
    cout << dp[W][n] << endl;
  }
}
Problem p03163 (C++)

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

typedef long long int ll;

ll wt[102], val[102];
ll dp[100][100001];

ll func(ll n, ll w) {
  if (dp[n][w] != -1)
    return dp[n][w];
  if (w >= wt[n])
    dp[n][w] = max(func(n - 1, w), func(n - 1, w - wt[n]) + val[n]);
  else
    dp[n][w] = func(n - 1, w);
  return dp[n][w];
}

int main() {
  ios::sync_with_stdio(0);
  ll n, w, i, j;
  cin >> n >> w;
  for (i = 0; i <= n; i++) {
    for (j = 0; j <= 100000; j++)
      dp[i][j] = -1;
  }
  for (i = 1; i <= n; i++)
    cin >> wt[i] >> val[i];
  for (i = 0; i <= w; i++)
    dp[0][i] = 0;
  for (i = 0; i <= n; i++)
    dp[i][0] = 0;
  ll ans = func(n, w);
  cout << ans << "\n";
  return 0;
}

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

typedef long long int ll;

ll wt[102], val[102];
ll dp[102][100001];

ll func(ll n, ll w) {
  if (dp[n][w] != -1)
    return dp[n][w];
  if (w >= wt[n])
    dp[n][w] = max(func(n - 1, w), func(n - 1, w - wt[n]) + val[n]);
  else
    dp[n][w] = func(n - 1, w);
  return dp[n][w];
}

int main() {
  ios::sync_with_stdio(0);
  ll n, w, i, j;
  cin >> n >> w;
  for (i = 0; i <= n; i++) {
    for (j = 0; j <= 100000; j++)
      dp[i][j] = -1;
  }
  for (i = 1; i <= n; i++)
    cin >> wt[i] >> val[i];
  for (i = 0; i <= w; i++)
    dp[0][i] = 0;
  for (i = 0; i <= n; i++)
    dp[i][0] = 0;
  ll ans = func(n, w);
  cout << ans << "\n";
  return 0;
}
Problem p03163 (C++)

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

int w[10500], v[105];
long long dp[105][10500];
using namespace std;
int main() {
  int N, W;
  scanf("%d%d", &N, &W);
  for (int i = 0; i < N; i++)
    scanf("%d%d", &w[i], &v[i]);
  for (int i = N - 1; i >= 0; i--) {
    for (int j = 0; j <= W; j++) {
      if (j < w[i]) {
        dp[i][j] = dp[i + 1][j];
      } else
        dp[i][j] = max(dp[i + 1][j], dp[i + 1][j - w[i]] + v[i]);
    }
  }
  printf("%lld\n", dp[0][W]);
  return 0;
}

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

int w[105], v[105];
long long dp[105][105000];
using namespace std;
int main() {
  int N, W;
  scanf("%d%d", &N, &W);
  for (int i = 0; i < N; i++)
    scanf("%d%d", &w[i], &v[i]);
  for (int i = N - 1; i >= 0; i--) {
    for (int j = 0; j <= W; j++) {
      if (j < w[i]) {
        dp[i][j] = dp[i + 1][j];
      } else
        dp[i][j] = max(dp[i + 1][j], dp[i + 1][j - w[i]] + v[i]);
    }
  }
  printf("%lld\n", dp[0][W]);
  return 0;
}
Problem p03163 (C++)

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

int64_t dp[101][10001];

int main() {
  int N, W, w[100], v[100];
  cin >> N >> W;
  for (int i = 0; i < N; i++)
    cin >> w[i] >> v[i];
  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= W; j++) {
      dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
      if (j + w[i] <= W)
        dp[i + 1][j + w[i]] = max(dp[i + 1][j + w[i]], dp[i][j] + v[i]);
    }
  }
  int64_t ans = 0;
  for (int j = 0; j <= W; j++)
    ans = max(ans, dp[N][j]);
  cout << ans << endl;
  return 0;
}

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

int64_t dp[101][100001];

int main() {
  int N, W, w[100], v[100];
  cin >> N >> W;
  for (int i = 0; i < N; i++)
    cin >> w[i] >> v[i];
  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= W; j++) {
      dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
      if (j + w[i] <= W)
        dp[i + 1][j + w[i]] = max(dp[i + 1][j + w[i]], dp[i][j] + v[i]);
    }
  }
  int64_t ans = 0;
  for (int j = 0; j <= W; j++)
    ans = max(ans, dp[N][j]);
  cout << ans << endl;
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int ll
#define D(x) cout << "D: " << #x << '=' << x << endl;
#define R(x, l)                                                                \
  cout << "R: Array " << #x << endl;                                           \
  for (int i = 0; i < l; i++)                                                  \
    cout << ' ' << i << ':' << *(x + i) << endl;
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
template <typename tn> inline tn next(void);
template <typename tn>
inline ostream &operator<<(ostream &os, const vector<tn> &v);
int w[105], p[105], dp[105];
signed main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int a, b;
  cin >> a >> b;
  for (int i = 0; i < a; i++)
    cin >> w[i] >> p[i];
  for (int i = 0; i < a; i++)
    for (int j = b; j >= w[i]; j--)
      dp[j] = max(dp[j], dp[j - w[i]] + p[i]);
  cout << dp[b] << endl;
  return ~~(0 - 0);
}
template <typename tn> inline tn next(void) {
  tn k;
  cin >> k;
  return k;
}
template <typename tn>
inline ostream &operator<<(ostream &os, const vector<tn> &v) {
  for (unsigned i = 0; i < v.size(); i++)
    os << v[i] << ' ';
  return os;
}


Fixed submission:
#include <bits/stdc++.h>
#define int ll
#define D(x) cout << "D: " << #x << '=' << x << endl;
#define R(x, l)                                                                \
  cout << "R: Array " << #x << endl;                                           \
  for (int i = 0; i < l; i++)                                                  \
    cout << ' ' << i << ':' << *(x + i) << endl;
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
template <typename tn> inline tn next(void);
template <typename tn>
inline ostream &operator<<(ostream &os, const vector<tn> &v);
int w[105], p[105], dp[100500];
signed main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int a, b;
  cin >> a >> b;
  for (int i = 0; i < a; i++)
    cin >> w[i] >> p[i];
  for (int i = 0; i < a; i++)
    for (int j = b; j >= w[i]; j--)
      dp[j] = max(dp[j], dp[j - w[i]] + p[i]);
  cout << dp[b] << endl;
  return ~~(0 - 0);
}
template <typename tn> inline tn next(void) {
  tn k;
  cin >> k;
  return k;
}
template <typename tn>
inline ostream &operator<<(ostream &os, const vector<tn> &v) {
  for (unsigned i = 0; i < v.size(); i++)
    os << v[i] << ' ';
  return os;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
using ll = long long;
using namespace std;

const long long MOD = 1000000007;
const ll LINF = 1LL << 50;

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

int main() {
  int N, W;
  cin >> N >> W;
  vector<ll> vn(100010, 0);
  vector<ll> wn(100010, 0);

  vector<vector<ll>> dp(100100, vector<ll>(110, 0));

  REP(n, N) cin >> wn.at(n) >> vn.at(n);

  for (int n = 0; n < N; n++) {
    for (int w = 0; w <= W; w++) {
      if (w - wn[n] >= 0) {
        chmax(dp[n + 1][w], dp[n][w - wn[n]] + vn[n]);
      }

      chmax(dp[n + 1][w], dp[n][w]);
    }
  }

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


Fixed submission:
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
using ll = long long;
using namespace std;

const long long MOD = 1000000007;
const ll LINF = 1LL << 50;

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

int main() {
  int N, W;
  cin >> N >> W;
  vector<ll> vn(100010, 0);
  vector<ll> wn(100010, 0);

  vector<vector<ll>> dp(110, vector<ll>(100100, 0));

  REP(n, N) cin >> wn.at(n) >> vn.at(n);

  for (int n = 0; n < N; n++) {
    for (int w = 0; w <= W; w++) {
      if (w - wn[n] >= 0) {
        chmax(dp[n + 1][w], dp[n][w - wn[n]] + vn[n]);
      }

      chmax(dp[n + 1][w], dp[n][w]);
    }
  }

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

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

using namespace std;

#define maxW 100010
#define maxN 102

typedef long long ll;

int n;
ll s;
ll cache[maxN][maxW], v[maxN], w[maxN];

ll rec(int i, ll W) {
  if (i < 0 || W < 0)
    return 0;
  if (cache[i][W] != -1)
    return cache[i][W];
  if (w[i] > W)
    return cache[W][i] = rec(i - 1, W);
  return cache[i][W] = max(rec(i - 1, W), v[i] + rec(i - 1, W - w[i]));
}

int main() {
  cin >> n >> s;
  for (int i = 0; i < n; i++) {
    cin >> w[i] >> v[i];
    for (int j = 0; j < maxW; j++) {
      cache[i][j] = -1;
    }
  }
  cout << rec(n - 1, s) << endl;
  return 0;
}

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

using namespace std;

#define maxW 100010
#define maxN 102

typedef long long ll;

int n;
ll s;
ll cache[maxN][maxW], v[maxN], w[maxN];

ll rec(int i, ll W) {
  if (i < 0 || W < 0)
    return 0;
  if (cache[i][W] != -1)
    return cache[i][W];
  if (w[i] > W)
    return cache[i][W] = rec(i - 1, W);
  return cache[i][W] = max(rec(i - 1, W), v[i] + rec(i - 1, W - w[i]));
}

int main() {
  cin >> n >> s;
  for (int i = 0; i < n; i++) {
    cin >> w[i] >> v[i];
    for (int j = 0; j < maxW; j++) {
      cache[i][j] = -1;
    }
  }
  cout << rec(n - 1, s) << endl;
  return 0;
}
Problem p03163 (C++)

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

#ifdef LOCAL
#define D(a) cerr << #a << " = " << a << endl
#else
#define D(a)
#define cerr false && cerr
#endif
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)
#define dforsn(i, s, n) for (int i = int(n - 1); i >= int(s); i--)
#define forsn(i, s, n) for (int i = int(s); i < int(n); i++)
#define dforn(i, n) dforsn(i, 0, n)
#define forn(i, n) forsn(i, 0, n)
#define all(a) a.begin(), a.end()
#define si(a) int((a).size())
#define pb emplace_back
#define mp make_pair
#define snd second
#define fst first
#define endl '\n'
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long long ll;

const int N = 100, W = 1e5 + 1;
int w[N], v[N];
ll t[N][W];

ll dp(int i, int c) {
  if (t[i][c])
    return t[i][c];
  if (i == 0)
    t[i][c] = c >= w[i] ? v[i] : 0;
  else
    t[i][c] = max(dp(i - 1, c),
                  (c >= w[i] ? dp(i - 1, c - w[i]) + v[i] : dp(i - 1, c)));
  return t[i][c];
}

int main() {
  fastio;

  int n, m;
  cin >> n >> m;
  forn(i, n) {
    int x, y;
    cin >> x >> y;
    w[i] = x, v[i] = y;
  }
  cout << dp(n - 1, m) << endl;
  return 0;
}


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

#ifdef LOCAL
#define D(a) cerr << #a << " = " << a << endl
#else
#define D(a)
#define cerr false && cerr
#endif
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)
#define dforsn(i, s, n) for (int i = int(n - 1); i >= int(s); i--)
#define forsn(i, s, n) for (int i = int(s); i < int(n); i++)
#define dforn(i, n) dforsn(i, 0, n)
#define forn(i, n) forsn(i, 0, n)
#define all(a) a.begin(), a.end()
#define si(a) int((a).size())
#define pb emplace_back
#define mp make_pair
#define snd second
#define fst first
#define endl '\n'
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long long ll;

const int N = 100, W = 1e5 + 1;
int w[N], v[N];
ll t[N][W];

ll dp(int i, int c) {
  if (t[i][c])
    return t[i][c];
  if (i == 0)
    t[i][c] = c >= w[i] ? v[i] : 0;
  else {
    ll x = dp(i - 1, c);
    t[i][c] = max(x, (c >= w[i] ? dp(i - 1, c - w[i]) + v[i] : x));
  }
  return t[i][c];
}

int main() {
  fastio;

  int n, m;
  cin >> n >> m;
  forn(i, n) {
    int x, y;
    cin >> x >> y;
    w[i] = x, v[i] = y;
  }
  cout << dp(n - 1, m) << endl;
  return 0;
}
Problem p03163 (C++)

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

typedef long long ll;
const int MAX_WEIGHTS = 1000005;
ll w[105], v[105], dp[MAX_WEIGHTS];
int N, W;
// ll weights[MAX_WEIGHTS];

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

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

  ll ans = 0;
  for (int i = 0; i < N; i++) {
    for (int x = MAX_WEIGHTS - 2; x >= 0; x--) {
      if (dp[x] || x == 0)
        dp[x + w[i]] = max(dp[x + w[i]], dp[x] + v[i]);
    }
  }

  for (int i = 1; i <= W; i++)
    ans = max(ans, dp[i]);
  cout << ans << "\n";

  return 0;
}


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

typedef long long ll;
const int MAX_WEIGHTS = 10000005;
ll w[105], v[105], dp[MAX_WEIGHTS];
int N, W;
// ll weights[MAX_WEIGHTS];

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

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

  ll ans = 0;
  for (int i = 0; i < N; i++) {
    for (int x = MAX_WEIGHTS - 2; x >= 0; x--) {
      if (dp[x] || x == 0)
        dp[x + w[i]] = max(dp[x + w[i]], dp[x] + v[i]);
    }
  }

  for (int i = 1; i <= W; i++)
    ans = max(ans, dp[i]);
  cout << ans << "\n";

  return 0;
}
Problem p03163 (C++)

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
unsigned long long int dp[105][100005];
unsigned long long int knapsack(unsigned long long int *wt,
                                unsigned long long int *val,
                                unsigned long long int w,
                                unsigned long long int n) {
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= w; j++) {
      if (i == 0 || j == 0) {
        dp[i][j] = 0;
      } else {
        if (wt[i - 1] > j) {
          dp[i][j] = dp[i - 1][j];
        } else {
          dp[i][j] = max(val[i - 1] + dp[i - 1][j - wt[i - 1]], dp[i - 1][j]);
        }
      }
    }
  }
  return dp[n][w];
}
int main() {
  int n, w;
  cin >> n;
  cin >> w;
  unsigned long long int wt[n];
  unsigned long long int val[n];
  for (int i = 0; i < n; i++) {
    cin >> wt[i];
    cin >> val[i];
  }
  cout << knapsack(wt, val, w, n) << endl;
  return 0;
}
Problem p03163 (C++)

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

using namespace std;

template <class T, class U> void ckmin(T &a, U b) {
  if (a > b)
    a = b;
}
template <class T, class U> void ckmax(T &a, U b) {
  if (a < b)
    a = b;
}

#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a)-1; i >= (b); i--)
#define SZ(x) ((int)((x).size()))
#define ALL(x) (x).begin(), (x).end()
#define INF 1000000007
#define LLINF 2696969696969696969ll
#define MAXN 113

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;

int N, W;
ll dp[MAXN];
ll ans;

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> N >> W;
  FOR(i, 0, N) {
    int x, y;
    cin >> x >> y;
    FORD(j, W + 1, 0) {
      if (j + x > W)
        continue;
      ckmax(dp[j + x], dp[j] + y);
    }
  }
  FORD(j, W + 1, 0) { ckmax(ans, dp[j]); }
  cout << ans << '\n';
}


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

using namespace std;

template <class T, class U> void ckmin(T &a, U b) {
  if (a > b)
    a = b;
}
template <class T, class U> void ckmax(T &a, U b) {
  if (a < b)
    a = b;
}

#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a)-1; i >= (b); i--)
#define SZ(x) ((int)((x).size()))
#define ALL(x) (x).begin(), (x).end()
#define INF 1000000007
#define LLINF 2696969696969696969ll
#define MAXN 100013

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;

int N, W;
ll dp[MAXN];
ll ans;

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> N >> W;
  FOR(i, 0, N) {
    int x, y;
    cin >> x >> y;
    FORD(j, W + 1, 0) {
      if (j + x > W)
        continue;
      ckmax(dp[j + x], dp[j] + y);
    }
  }
  FORD(j, W + 1, 0) { ckmax(ans, dp[j]); }
  cout << ans << '\n';
}
Problem p03163 (C++)

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

long long weights[101];
long long values[101];
long long dp[100][100001];

int main() {
  int n;
  long long W;
  cin >> n >> W;
  for (int i = 0; i < n; i++) {
    cin >> weights[i] >> values[i];
  }

  for (int i = 0; i < n; i++) {
    for (long long w = 0; w <= W; w++) {
      if (w >= weights[i]) {
        dp[i + 1][w] = max(dp[i][w - weights[i]] + values[i], dp[i][w]);
      } else {
        dp[i + 1][w] = dp[i][w];
      }
    }
  }
  cout << dp[n][W];
}

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

long long weights[101];
long long values[101];
long long dp[101][100001];

int main() {
  int n;
  long long W;
  cin >> n >> W;
  for (int i = 0; i < n; i++) {
    cin >> weights[i] >> values[i];
  }

  for (int i = 0; i < n; i++) {
    for (long long w = 0; w <= W; w++) {
      if (w >= weights[i]) {
        dp[i + 1][w] = max(dp[i][w - weights[i]] + values[i], dp[i][w]);
      } else {
        dp[i + 1][w] = dp[i][w];
      }
    }
  }
  cout << dp[n][W];
}
Problem p03163 (C++)

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

using namespace std;
// #define ll long long int;
vector<pair<long long int, long long int>> v;
long long int dp[105][10005];
long long int maxi(long long int w, long long int i, long long int n) {
  if (i == n)
    return 0;
  if (w - v[i].first >= 0) {
    if (dp[i][w] == -1)
      return dp[i][w] = max(v[i].second + maxi(w - v[i].first, i + 1, n),
                            maxi(w, i + 1, n));
    else
      return dp[i][w];
  } else {
    if (dp[i + 1][w] == -1)
      return dp[i + 1][w] = maxi(w, i + 1, n);
    else
      return dp[i + 1][w];
  }
}
int main() {
  memset(dp, -1, sizeof(dp));
  long long int n, w, x, y;
  cin >> n >> w;

  for (int i = 0; i < n; i++) {
    cin >> x >> y;
    v.push_back({x, y});
  }
  cout << maxi(w, 0, n);
}

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

using namespace std;
// #define ll long long int;
vector<pair<long long int, long long int>> v;
long long int dp[105][100005];
long long int maxi(long long int w, long long int i, long long int n) {
  if (i == n)
    return 0;
  if (w - v[i].first >= 0) {
    if (dp[i][w] == -1)
      return dp[i][w] = max(v[i].second + maxi(w - v[i].first, i + 1, n),
                            maxi(w, i + 1, n));
    else
      return dp[i][w];
  } else {
    if (dp[i + 1][w] == -1)
      return dp[i + 1][w] = maxi(w, i + 1, n);
    else
      return dp[i + 1][w];
  }
}
int main() {
  memset(dp, -1, sizeof(dp));
  long long int n, w, x, y;
  cin >> n >> w;

  for (int i = 0; i < n; i++) {
    cin >> x >> y;
    v.push_back({x, y});
  }
  cout << maxi(w, 0, n);
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef double db;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef vector<char> vc;
typedef string S;
typedef queue<int> qi;
typedef pair<ll, ll> P;
typedef vector<P> vp;
#define sort(a) sort(a.begin(), a.end())
#define reverse(a) reverse(a.begin(), a.end())
#define pb push_back
#define ft first
#define sd second
#define elif else if
#define unique(a) a.erase(unique(a.begin(), a.end()), a.end())
#define mp make_pair
#define fr(i, n) for (ll i = 0; i < (n); i++)
#define ifr(i, n) for (ll i = (n)-1; i >= 0; i--)
const int MOD = 1e9 + 7;
const ll INF = 1e18;

int main() {
  ll n, W;
  cin >> n >> W;
  vl weight(n), value(n);
  fr(i, n) cin >> weight[i] >> value[i];
  ll dp[110][100010] = {0};
  fr(i, n) {
    fr(sum_w, W + 1) {
      dp[i + 1][sum_w] = max(dp[i][sum_w], dp[i][sum_w - weight[i]] + value[i]);
    }
  }
  cout << dp[n][W] << endl;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef double db;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef vector<char> vc;
typedef string S;
typedef queue<int> qi;
typedef pair<ll, ll> P;
typedef vector<P> vp;
#define sort(a) sort(a.begin(), a.end())
#define reverse(a) reverse(a.begin(), a.end())
#define pb push_back
#define ft first
#define sd second
#define elif else if
#define unique(a) a.erase(unique(a.begin(), a.end()), a.end())
#define mp make_pair
#define fr(i, n) for (ll i = 0; i < (n); i++)
#define ifr(i, n) for (ll i = (n)-1; i >= 0; i--)
const int MOD = 1e9 + 7;
const ll INF = 1e18;

int main() {
  ll n, W;
  cin >> n >> W;
  vl weight(n), value(n);
  fr(i, n) cin >> weight[i] >> value[i];
  ll dp[110][100010] = {0};
  fr(i, n) {
    fr(sum_w, W + 1) {
      if (sum_w >= weight[i])
        dp[i + 1][sum_w] =
            max(dp[i][sum_w], dp[i][sum_w - weight[i]] + value[i]);
      else
        dp[i + 1][sum_w] = dp[i][sum_w];
    }
  }
  cout << dp[n][W] << endl;
}
Problem p03163 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define ll long long

int n, w;
pii arr[101];
ll dp[101][100005];

ll knap(int i, int n, int sm) {
  if (i == n)
    return 0;
  if (dp[i][sm] != -1)
    return dp[i][sm];
  ll ans = 0;
  if (sm + arr[i].first <= w) {
    ans = max(ans, arr[i].second + knap(i + 1, n, sm + arr[i].first));
    ans = max(ans, knap(i + 1, n, sm));
  } else {
    ans = max(ans, knap(i + 1, n, sm));
  }
  return ans;
}

int main(int argc, char const *argv[]) {

  cin >> n >> w;
  for (int i = 0; i < n; ++i) {
    cin >> arr[i].first >> arr[i].second;
  }

  memset(dp, -1, sizeof(dp));
  cout << knap(0, n, 0);
  return 0;
}

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

int n, w;
pii arr[101];
ll dp[101][100005];

ll knap(int i, int n, int sm) {
  if (i == n)
    return 0;
  if (dp[i][sm] != -1)
    return dp[i][sm];
  ll ans = 0;
  if (sm + arr[i].first <= w) {
    ans = max(ans, arr[i].second + knap(i + 1, n, sm + arr[i].first));
    ans = max(ans, knap(i + 1, n, sm));
  } else {
    ans = max(ans, knap(i + 1, n, sm));
  }
  return dp[i][sm] = ans;
}

int main(int argc, char const *argv[]) {

  cin >> n >> w;
  for (int i = 0; i < n; ++i) {
    cin >> arr[i].first >> arr[i].second;
  }

  memset(dp, -1, sizeof(dp));
  cout << knap(0, n, 0);
  return 0;
}
Problem p03163 (C++)

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

long long dp[100][100010], v[110], w[110];
int main() {
  long long n, W;
  cin >> n >> W;
  for (int i = 0; i < n; i++)
    cin >> w[i] >> v[i];

  for (int i = n - 1; i >= 0; i--)
    for (int curw = 0; curw <= W; curw++)
      dp[i][curw] =
          max(dp[i + 1][curw],
              (curw + w[i] <= W ? dp[i + 1][curw + w[i]] + v[i] : -1));

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


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

long long dp[110][100010], v[110], w[110];
int main() {
  long long n, W;
  cin >> n >> W;
  for (int i = 0; i < n; i++)
    cin >> w[i] >> v[i];

  for (int i = n - 1; i >= 0; i--)
    for (int curw = 0; curw <= W; curw++)
      dp[i][curw] =
          max(dp[i + 1][curw],
              (curw + w[i] <= W ? dp[i + 1][curw + w[i]] + v[i] : -1));

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

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

// using INF = INT_MAX / 2;

#define DEBUG
#ifdef DEBUG
#define dump(x) cout << "[*] " #x ": " << x << endl
#define debug(x) x
#else
#define dump(x)
#define debug(x)
#endif

// dp[w][i] := w の重さの時 i 個の荷物を入れた時の価値の総和
long long dp[100][100005];

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

  for (int i = 0; i < N; ++i) {
    for (int j = 0; j <= W; ++j) {
      dp[i + 1][j] =
          max(dp[i][j], j - w[i] >= 0 ? dp[i][j - w[i]] + v[i] : dp[i][j]);
    }
  }
  long long int ans = 0;
  for (int i = 0; i <= W; ++i) {
    ans = max(ans, dp[N][i]);
  }
  cout << ans << endl;

  return 0;
}


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

// using INF = INT_MAX / 2;

#define DEBUG
#ifdef DEBUG
#define dump(x) cout << "[*] " #x ": " << x << endl
#define debug(x) x
#else
#define dump(x)
#define debug(x)
#endif

// dp[w][i] := w の重さの時 i 個の荷物を入れた時の価値の総和
long long dp[105][100005];

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

  for (int i = 0; i < N; ++i) {
    for (int j = 0; j <= W; ++j) {
      dp[i + 1][j] =
          max(dp[i][j], j - w[i] >= 0 ? dp[i][j - w[i]] + v[i] : dp[i][j]);
    }
  }
  long long int ans = 0;
  for (int i = 0; i <= W; ++i) {
    ans = max(ans, dp[N][i]);
  }
  cout << ans << endl;

  return 0;
}
Problem p03163 (C++)

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

using namespace std;

void setIO() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
#ifdef DEBUG
  // freopen("solution.out", "w", stdout);
  freopen("1.in", "r", stdin);
#endif
}

const int maxn = 100, maxw = 1e5 + 1;
const long long inf = 1e18;
long long dp[maxn][maxw];

int main() {
  setIO();

  int n, w;
  cin >> n >> w;
  vector<pair<int, int>> item(n); // first = weight, second = value
  for (int i = 0; i < n; ++i) {
    cin >> item[i].first >> item[i].second;
    for (int j = 0; j < w + 1; ++j) {
      dp[i][j] = -inf;
    }
  }
  dp[0][item[0].first] = item[0].second;
  long long sol = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j <= w; ++j) {
      if (item[i].first <= j) {
        dp[i][j] = max(dp[i][j], (long long)item[i].second);
      }
      dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
      if (j + item[i + 1].first <= w) {
        dp[i + 1][j + item[i + 1].first] = max(
            {dp[i + 1][j + item[i + 1].first], dp[i][j] + item[i + 1].second});
      }
      if (i == n - 1) {
        sol = max(sol, dp[i][j]);
      }
    }
  }
  cout << sol << "\n";
  return 0;
}


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

using namespace std;

void setIO() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
#ifdef DEBUG
  // freopen("solution.out", "w", stdout);
  freopen("1.in", "r", stdin);
#endif
}

const int maxn = 101, maxw = 1e5 + 1;
const long long inf = 1e18;
long long dp[maxn][maxw];

int main() {
  setIO();

  int n, w;
  cin >> n >> w;
  vector<pair<int, int>> item(n); // first = weight, second = value
  for (int i = 0; i < n; ++i) {
    cin >> item[i].first >> item[i].second;
    for (int j = 0; j < w + 1; ++j) {
      dp[i][j] = -inf;
    }
  }
  dp[0][item[0].first] = item[0].second;
  long long sol = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j <= w; ++j) {
      if (item[i].first <= j) {
        dp[i][j] = max(dp[i][j], (long long)item[i].second);
      }
      dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
      if (j + item[i + 1].first <= w) {
        dp[i + 1][j + item[i + 1].first] = max(
            {dp[i + 1][j + item[i + 1].first], dp[i][j] + item[i + 1].second});
      }
      if (i == n - 1) {
        sol = max(sol, dp[i][j]);
      }
    }
  }
  cout << sol << "\n";
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define vin vector<int>
#define vcn vector<char>
#define ver pair<int, int>
#define pb push_back
#define ld long double
#define x first
#define y second
#define blush                                                                  \
  fflush(stdout);                                                              \
  fflush(stdin);
#define $(a) (int)a.size()
#define scanf ;
#define printf ;
using namespace std;
void i_love_misaka_mikoto();
int32_t main() {
#ifdef RNS
  freopen("input.txt", "r", stdin);
#ifndef DBG
  freopen("output.txt", "w", stdout);
#endif
#else
  ios_base::sync_with_stdio(false);
#endif
  i_love_misaka_mikoto();
#ifdef RNS
  cout << '\n' << (ld)clock() / CLOCKS_PER_SEC << '\n';
#endif
  return 0;
}
int rz[(int)1e5 + 10];

void i_love_misaka_mikoto() {
  int n, w;
  cin >> n >> w;
  vector<ver> zn(n);
  for (int i = 0; i < n; i++) {
    cin >> zn[i].x >> zn[i].y;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 1e5 + 9; j >= 0; j--) {
      if (rz[j] || j == 0) {
        rz[j + zn[i].x] = max(rz[j + zn[i].x], rz[j] + zn[i].y);
      }
    }
  }
  int mx = 0;
  for (int i = 0; i <= w; i++) {
    if (rz[mx] <= rz[i])
      mx = i;
  }
  cout << rz[mx];
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define vin vector<int>
#define vcn vector<char>
#define ver pair<int, int>
#define pb push_back
#define ld long double
#define x first
#define y second
#define blush                                                                  \
  fflush(stdout);                                                              \
  fflush(stdin);
#define $(a) (int)a.size()
#define scanf ;
#define printf ;
using namespace std;
void i_love_misaka_mikoto();
int32_t main() {
#ifdef RNS
  freopen("input.txt", "r", stdin);
#ifndef DBG
  freopen("output.txt", "w", stdout);
#endif
#else
  ios_base::sync_with_stdio(false);
#endif
  i_love_misaka_mikoto();
#ifdef RNS
  cout << '\n' << (ld)clock() / CLOCKS_PER_SEC << '\n';
#endif
  return 0;
}
int rz[(int)1e6 + 10];

void i_love_misaka_mikoto() {
  int n, w;
  cin >> n >> w;
  vector<ver> zn(n);
  for (int i = 0; i < n; i++) {
    cin >> zn[i].x >> zn[i].y;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 1e5 + 9; j >= 0; j--) {
      if (rz[j] || j == 0) {
        rz[j + zn[i].x] = max(rz[j + zn[i].x], rz[j] + zn[i].y);
      }
    }
  }
  int mx = 0;
  for (int i = 0; i <= w; i++) {
    if (rz[mx] <= rz[i])
      mx = i;
  }
  cout << rz[mx];
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;
long long N, W;
long long w[105], u[105];
long long dp[105][105];
long long ans = 0;
int main() {
  scanf("%d%d", &N, &W);
  for (int i = 1; i <= N; i++)
    scanf("%d%d", &w[i], &u[i]);
  for (int i = 1; i <= W; i++)
    for (int j = 1; j <= N; j++)
      if (i >= w[j])
        dp[i][j] = max(dp[i][j - 1], dp[i - w[j]][j - 1] + u[j]);
      else
        dp[i][j] = max(dp[i][j - 1], dp[i][j]);
  for (int i = 1; i <= N; i++)
    ans = max(ans, dp[W][i]);
  cout << ans;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;
long long N, W;
long long w[105], u[105];
long long dp[100005][105];
long long ans = 0;
int main() {
  scanf("%d%d", &N, &W);
  for (int i = 1; i <= N; i++)
    scanf("%d%d", &w[i], &u[i]);
  for (int i = 1; i <= W; i++)
    for (int j = 1; j <= N; j++)
      if (i >= w[j])
        dp[i][j] = max(dp[i][j - 1], dp[i - w[j]][j - 1] + u[j]);
      else
        dp[i][j] = max(dp[i][j - 1], dp[i][j]);
  for (int i = 1; i <= N; i++)
    ans = max(ans, dp[W][i]);
  cout << ans;
  return 0;
}
Problem p03163 (C++)

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

#define ll long long int
#define ri register int
#define endl "\n"

using namespace std;

ll dp[102][(int)1e5 + 2] = {};

int main() {
  ios_base::sync_with_stdio(false);
  ll n, w;
  cin >> n >> w;

  vector<pair<ll, ll>> items(n + 1);
  for (ri i = 1; i <= n; i++)
    cin >> items[i].first >> items[i].second;

  for (ri i = 0; i <= n; i++) {
    for (ri j = 0; j <= w; j++) {
      if (j - items[i].first >= 0) {
        dp[i][j] =
            max(dp[i - 1][j - items[i].first] + items[i].second, dp[i - 1][j]);
      } else {
        dp[i][j] = dp[i - 1][j];
      }
    }
  }

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


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

#define ll long long int
#define ri register int
#define endl "\n"

using namespace std;

ll dp[102][(int)1e5 + 2] = {};

int main() {
  ios_base::sync_with_stdio(false);
  ll n, w;
  cin >> n >> w;

  vector<pair<ll, ll>> items(n + 1);
  for (ri i = 1; i <= n; i++)
    cin >> items[i].first >> items[i].second;

  for (ri i = 1; i <= n; i++) {
    for (ri j = 0; j <= w; j++) {
      if (j - items[i].first >= 0) {
        dp[i][j] =
            max(dp[i - 1][j - items[i].first] + items[i].second, dp[i - 1][j]);
      } else {
        dp[i][j] = dp[i - 1][j];
      }
    }
  }

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

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <iostream>
#define mp make_pair

using namespace std;

typedef long long LL;
typedef pair<int, int> pii;
typedef pair<int, LL> pil;

const int MAXN = 105;
const int MAXW = 1e5 + 7;

template <typename T> inline T read() {
  T res = 0, flag = 1;
  char in = getchar();
  while (!isdigit(in)) {
    if (in == '-')
      flag = -1;
    in = getchar();
  }
  while (isdigit(in)) {
    res = (res << 1) + (res << 3) + in - '0';
    in = getchar();
  }
  return res * flag;
}

template <typename T> inline void chkmax(T &a, T b) {
  if (a < b)
    a = b;
}
template <typename T> inline void chkmin(T &a, T b) {
  if (a > b)
    a = b;
}

int N, W;
int w[MAXN], val[MAXN];
LL dp[MAXN][MAXW], ans;

inline void solve() {
  N = read<int>();
  W = read<int>();
  for (int i = 1; i <= N; ++i) {
    w[i] = read<int>();
    val[i] = read<int>();
  }
  for (int i = 1; i <= N; ++i) {
    //		for(int j = 0; j < w[i]; ++j) dp[i][j] = dp[i - 1][j];
    for (int j = 0; j <= W; ++j) {
      if (j >= w[i])
        chkmax(dp[i][j], dp[i - 1][j - w[i]] + val[i]);
      chkmax(dp[i][j], dp[i - 1][j]);
      // printf("%d %d %d\n", i, j, dp[i][j]);
    }
  }
  for (int i = 0; i <= W; ++i)
    chkmax(ans, dp[N][i]);
  printf("%lld\n", ans);
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("D.in", "r", stdin);
  freopen("D.out", "w", stdout);
#endif

  solve();

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <iostream>
#define mp make_pair

using namespace std;

typedef long long LL;
typedef pair<int, int> pii;
typedef pair<int, LL> pil;

const int MAXN = 105;
const int MAXW = 1e5 + 7;

template <typename T> inline T read() {
  T res = 0, flag = 1;
  char in = getchar();
  while (!isdigit(in)) {
    if (in == '-')
      flag = -1;
    in = getchar();
  }
  while (isdigit(in)) {
    res = (res << 1) + (res << 3) + in - '0';
    in = getchar();
  }
  return res * flag;
}

template <typename T> inline void chkmax(T &a, T b) {
  if (a < b)
    a = b;
}
template <typename T> inline void chkmin(T &a, T b) {
  if (a > b)
    a = b;
}

int N, W;
int w[MAXN], val[MAXN];
LL dp[MAXN][MAXW], ans;

inline void solve() {
  N = read<int>();
  W = read<int>();
  for (int i = 1; i <= N; ++i) {
    w[i] = read<int>();
    val[i] = read<int>();
  }
  for (int i = 1; i <= N; ++i) {
    //		for(int j = 0; j < w[i]; ++j) dp[i][j] = dp[i - 1][j];
    for (int j = 0; j <= W; ++j) {
      if (j >= w[i])
        chkmax(dp[i][j], dp[i - 1][j - w[i]] + val[i]);
      chkmax(dp[i][j], dp[i - 1][j]);
      // printf("%d %d %d\n", i, j, dp[i][j]);
    }
  }
  for (int i = 0; i <= W; ++i)
    chkmax(ans, dp[N][i]);
  printf("%lld\n", ans);
}

int main() {

  solve();

  return 0;
}
Problem p03163 (C++)

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

using namespace std;

const long long inf = 1e18;
#define ll long long
#define fr(i, a, n) for (int i = a; i < n; i++)
#define rfr(i, n, a) for (int i = n; i >= a; i--)
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define endl "\n"
typedef vector<int> VI;
typedef vector<ll> VL;

ll n, m, k;
ll dp[101][100001];
vector<pair<int, int>> in;
ll solv(int ind, ll curw) {
  if (curw > k) {
    return -inf;
  }
  if (ind == n)
    return 0;
  if (dp[ind][curw] != -1)
    return dp[ind][curw];
  return max(in[ind].second + solv(ind + 1, curw + in[ind].first),
             solv(ind + 1, curw));
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  memset(dp, -1, sizeof dp);
  fr(i, 0, n) {
    int w, v;
    cin >> w >> v;
    in.pb({w, v});
  }
  cout << solv(0, 0) << endl;
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"

using namespace std;

const long long inf = 1e18;
#define ll long long
#define fr(i, a, n) for (int i = a; i < n; i++)
#define rfr(i, n, a) for (int i = n; i >= a; i--)
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define endl "\n"
typedef vector<int> VI;
typedef vector<ll> VL;

ll n, m, k;
ll dp[101][100001];
vector<pair<int, int>> in;
ll solv(int ind, ll curw) {
  if (curw > k) {
    return -inf;
  }
  if (ind == n)
    return 0;
  if (dp[ind][curw] != -1)
    return dp[ind][curw];
  return dp[ind][curw] =
             max(in[ind].second + solv(ind + 1, curw + in[ind].first),
                 solv(ind + 1, curw));
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  memset(dp, -1, sizeof dp);
  fr(i, 0, n) {
    int w, v;
    cin >> w >> v;
    in.pb({w, v});
  }
  cout << solv(0, 0) << endl;
  return 0;
}