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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll i, j, n, m, w, c, ans, p[109], wt[109], dp[100009][109];
map<ll, ll> mp;
ll func(ll w, ll i) {
  if (mp[w] == 0)
    mp[w] = ++c;
  ll x = mp[w];
  if (dp[x][i] != -1)
    return dp[x][i];
  if (i == n)
    return 0;
  if (w >= wt[i])
    dp[x][i] = max(p[i] + func(w - wt[i], i + 1), func(w, i + 1));
  else
    dp[x][i] = func(w, i + 1);
  return dp[x][i];
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0);
  cin >> n >> w;
  memset(dp, -1, sizeof(dp));
  for (i = 0; i < n; i++)
    cin >> wt[i] >> p[i];
  ans = func(w, 0);
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll i, j, n, m, w, c, ans, p[109], wt[109], dp[100009][109], mp[100009];
ll func(ll w, ll i) {
  if (mp[w] == 0)
    mp[w] = ++c;
  ll x = mp[w];
  if (dp[x][i] != -1)
    return dp[x][i];
  if (i == n)
    return 0;
  if (w >= wt[i])
    dp[x][i] = max(p[i] + func(w - wt[i], i + 1), func(w, i + 1));
  else
    dp[x][i] = func(w, i + 1);
  return dp[x][i];
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0);
  cin >> n >> w;
  memset(dp, -1, sizeof(dp));
  for (i = 0; i < n; i++)
    cin >> wt[i] >> p[i];
  ans = func(w, 0);
  cout << ans << endl;
}
Problem p03163 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ios                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define rep(i, a, b) for (int i = a; i < b; i++)
#define pb push_back

int main() {
  ios int N, W;
  cin >> N >> W;
  vector<int> w(110, 0);
  vector<int> v(110, 0);
  vector<vector<ll>> dp(110, vector<ll>(110000, 0));
  rep(i, 0, N) cin >> w[i] >> v[i];
  int k = 0;
  rep(i, 0, N) dp[i][k] = 0;
  rep(i, 0, N) rep(j, 1, W + 1) {
    if (i == 0) {
      rep(j, 0, W + 1) {
        if (j < w[i])
          dp[i][j] = 0;
        else
          dp[i][j] = v[i];
      }
      continue;
    }
    if (j < w[i])
      dp[i][j] = dp[i - 1][j];
    else {
      dp[i][j] = max(v[i] + dp[i - 1][j - w[i]], dp[i - 1][j]);
    }
  }
  cout << dp[N - 1][W];
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ios                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define rep(i, a, b) for (int i = a; i < b; i++)
#define pb push_back

int main() {
  ios int N, W;
  cin >> N >> W;
  vector<int> w(110, 0);
  vector<int> v(110, 0);
  vector<vector<ll>> dp(110, vector<ll>(110000, 0));
  rep(i, 0, N) cin >> w[i] >> v[i];
  int k = 0;
  rep(i, 0, N) dp[i][k] = 0;
  rep(i, 0, N) rep(j, 1, W + 1) {
    if (i == 0) {
      if (j < w[i])
        dp[i][j] = 0;
      else
        dp[i][j] = v[i];
      continue;
    }
    if (j < w[i])
      dp[i][j] = dp[i - 1][j];
    else {
      dp[i][j] = max(v[i] + dp[i - 1][j - w[i]], dp[i - 1][j]);
    }
  }
  cout << dp[N - 1][W];
  return 0;
}
Problem p03163 (C++)

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

const ll INF = 100000000009;
ll dp[105][100005];

template <class T> inline bool chmax(T &x, T y) {
  if (x < y) {
    x = y;
    return true;
  } else
    return false;
}

int main() {
  ll N, W;
  cin >> N >> W;
  vl n(N);
  vl w(N);
  rep(i, N) cin >> w[i] >> n[i];
  rep(i, N + 10) {
    rep(j, W + 10) { dp[i][j] = 0; }
  }
  rep(i, N) {
    rep(j, W + 1) {
      if (j >= w[i])
        dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + n[i]);
      else
        dp[i + 1][j] = dp[i][j];
    }
  }
  cout << dp[N][W] << endl;
}

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

const ll INF = 100000000009;
ll dp[200][200000];

template <class T> inline bool chmax(T &x, T y) {
  if (x < y) {
    x = y;
    return true;
  } else
    return false;
}

int main() {
  ll N, W;
  cin >> N >> W;
  vl n(N);
  vl w(N);
  rep(i, N) cin >> w[i] >> n[i];
  rep(i, N + 10) {
    rep(j, W + 10) { dp[i][j] = 0; }
  }
  rep(i, N) {
    rep(j, W + 1) {
      if (j >= w[i])
        dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + n[i]);
      else
        dp[i + 1][j] = dp[i][j];
    }
  }
  cout << dp[N][W] << endl;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
int dp[101][10001];
int n;
int weight[200001];
int value[200001];

int rec(int idx, int knap) {
  if (dp[idx][knap] != -1)
    return dp[idx][knap];
  if (idx == n)
    return 0;

  if (knap < 0)
    return 0;
  //  if(knap==0) return 0;
  // cout<<max(rec(idx+1,knap),(value[idx]+rec(idx+1,knap-weight[idx])));
  // cout<<" "<<"\n";
  if (weight[idx] > knap)
    return dp[idx][knap] = rec(idx + 1, knap);
  return dp[idx][knap] = max(rec(idx + 1, knap),
                             (value[idx] + rec(idx + 1, knap - weight[idx])));
}

signed main() {

  for (int i = 0; i < 101; i++) {
    for (int j = 0; j < 100001; j++)
      dp[i][j] = -1;
  }
  int w;
  cin >> n >> w;

  for (int i = 0; i < n; i++) {
    cin >> weight[i] >> value[i];
  }
  // cout<<w;
  cout << rec(0, w);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
int dp[101][100001];
int n;
int weight[200001];
int value[200001];

int rec(int idx, int knap) {
  if (dp[idx][knap] != -1)
    return dp[idx][knap];
  if (idx == n)
    return 0;

  if (knap < 0)
    return 0;
  //  if(knap==0) return 0;
  // cout<<max(rec(idx+1,knap),(value[idx]+rec(idx+1,knap-weight[idx])));
  // cout<<" "<<"\n";
  if (weight[idx] > knap)
    return dp[idx][knap] = rec(idx + 1, knap);
  return dp[idx][knap] = max(rec(idx + 1, knap),
                             (value[idx] + rec(idx + 1, knap - weight[idx])));
}

signed main() {

  for (int i = 0; i < 101; i++) {
    for (int j = 0; j < 100001; j++)
      dp[i][j] = -1;
  }
  int w;
  cin >> n >> w;

  for (int i = 0; i < n; i++) {
    cin >> weight[i] >> value[i];
  }
  // cout<<w;
  cout << rec(0, w);
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
using namespace std;
int wt[105];
int val[105];
int ans[100][100005];
int N, w;
int ks(int n, int c) {
  if (ans[n][c] != -1)
    return ans[n][c];
  if (n == 0 or c == 0)
    return 0;
  if (wt[n - 1] > c)
    return ks(n - 1, c);
  int a = ks(n - 1, c);
  int b = val[n - 1] + ks(n - 1, c - wt[n - 1]);
  ans[n][c] = max(a, b);
  return ans[n][c];
}
void solve() {
  cin >> N >> w;
  for (int i = 0; i < N; ++i) {
    cin >> wt[i] >> val[i];
  }
  for (int i = 0; i < N + 1; ++i)
    for (int j = 0; j < w + 1; ++j)
      ans[i][j] = -1;

  cout << ks(N, w);
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define int long long
using namespace std;
int wt[105];
int val[105];
int ans[105][100005];
int N, w;
int ks(int n, int c) {
  if (ans[n][c] != -1)
    return ans[n][c];
  if (n == 0 or c == 0)
    return 0;
  if (wt[n - 1] > c)
    return ks(n - 1, c);
  int a = ks(n - 1, c);
  int b = val[n - 1] + ks(n - 1, c - wt[n - 1]);
  ans[n][c] = max(a, b);
  return ans[n][c];
}
void solve() {
  cin >> N >> w;
  for (int i = 0; i < N; ++i) {
    cin >> wt[i] >> val[i];
  }
  for (int i = 0; i < N + 1; ++i)
    for (int j = 0; j < w + 1; ++j)
      ans[i][j] = -1;

  cout << ks(N, w);
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
}
Problem p03163 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;

vector<int> wei;
vector<int> val;

lli dp[101][100001];

lli knapsack(lli n, lli w) {
  if (n == 0 || w == 0)
    dp[n][w] = 0;

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

  if (wei[n - 1] > w)
    return knapsack(n - 1, w);

  lli x1 = val[n - 1] + knapsack(n - 1, w - wei[n - 1]);
  lli x2 = knapsack(n - 1, w);

  return max(x1, x2);
}

int main() {
  lli n, w, wi, vi;
  cin >> n >> w;

  for (lli i = 0; i < n; i++) {
    cin >> wi >> vi;
    wei.push_back(wi);
    val.push_back(vi);
  }

  for (int i = 0; i < 101; i++)
    for (int j = 0; j < 100001; j++)
      dp[i][j] = -1;

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

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;

vector<int> wei;
vector<int> val;

lli dp[101][100001];

lli knapsack(lli n, lli w) {
  if (n == 0 || w == 0)
    dp[n][w] = 0;

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

  if (wei[n - 1] > w)
    return knapsack(n - 1, w);

  lli x1 = val[n - 1] + knapsack(n - 1, w - wei[n - 1]);
  lli x2 = knapsack(n - 1, w);

  dp[n][w] = max(x1, x2);
  return dp[n][w];
}

int main() {
  lli n, w, wi, vi;
  cin >> n >> w;

  for (lli i = 0; i < n; i++) {
    cin >> wi >> vi;
    wei.push_back(wi);
    val.push_back(vi);
  }

  for (int i = 0; i < 101; i++)
    for (int j = 0; j < 100001; j++)
      dp[i][j] = -1;

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long dp[105][1000005];
int N, W, w[105], v[105];
int main() {
  cin >> N >> W;
  for (int i = 1; i <= N; i++)
    cin >> w[i] >> v[i];
  for (int i = 1; i <= W; i++) {
    for (int j = 1; j <= N; j++) {
      dp[j][i] = max(dp[j - 1][i], dp[j][i]);
      if (i - w[j] >= 0)
        dp[j][i] = max(dp[j - 1][i], max(dp[j][i], dp[j - 1][i - w[j]] + v[j]));
    }
  }
  int ans = 0;

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long dp[105][1000005];
int N, W, w[105], v[105];
int main() {
  cin >> N >> W;
  for (int i = 1; i <= N; i++)
    cin >> w[i] >> v[i];
  for (int i = 1; i <= W; i++) {
    for (int j = 1; j <= N; j++) {
      dp[j][i] = max(dp[j - 1][i], dp[j][i]);
      if (i - w[j] >= 0)
        dp[j][i] = max(dp[j - 1][i], max(dp[j][i], dp[j - 1][i - w[j]] + v[j]));
    }
  }
  int ans = 0;

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

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

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


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

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

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

int N, W;
int N_MAX = 100;
int W_MAX = 100000;
vector<int> w(N_MAX), v(N_MAX);
vector<vector<long long>> dp(N_MAX, vector<long long>(W_MAX + 1, 0));

int main() {
  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++) {
      if (j < w[i])
        dp[i + 1][j] = dp[i][j];
      else
        dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]);
    }
  }
  cout << dp[N][W] << endl;
}


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

int N, W;
int N_MAX = 100;
int W_MAX = 100000;
vector<int> w(N_MAX), v(N_MAX);
vector<vector<long long>> dp(N_MAX + 1, vector<long long>(W_MAX, 0));

int main() {
  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++) {
      if (j < w[i])
        dp[i + 1][j] = dp[i][j];
      else
        dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]);
    }
  }
  cout << dp[N][W] << endl;
}
Problem p03163 (C++)

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

int N, W;
int w[105], v[105];
int cache[105][105];

int dp(int n, int wt) {
  if (wt > W)
    return -1e15;
  if (n == N)
    return 0;
  if (cache[n][wt] != -1)
    return cache[n][wt];
  cache[n][wt] = max(dp(n + 1, wt), v[n] + dp(n + 1, wt + w[n]));
  return cache[n][wt];
}

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

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

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

  cout << dp(0, 0);

  return 0;
}

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

int N, W;
int w[105], v[105];
int cache[105][100005];

int dp(int n, int wt) {
  if (wt > W)
    return -1e15;
  if (n == N)
    return 0;
  if (cache[n][wt] != -1)
    return cache[n][wt];
  cache[n][wt] = max(dp(n + 1, wt), v[n] + dp(n + 1, wt + w[n]));
  return cache[n][wt];
}

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

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

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

  cout << dp(0, 0);

  return 0;
}
Problem p03163 (C++)

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

#define int long long

using namespace std;

int dp[110][100010];
int a[110][2];

int32_t main() {
  int n, w;
  cin >> n >> w;

  for (int i = 1; i <= n; i++)
    cin >> a[i][0] >> a[i][1];

  for (int i = 1; i <= w; i++) {
    for (int j = 1; j <= n; j++) {
      dp[i][j] = dp[i][j - 1];

      if (a[j][0] <= i)
        dp[i][j] = max(dp[i][j], dp[i - a[j][0]][j - 1] + a[j][1]);
    }
  }

  // for(int i=0;i<=w;i++)
  // {
  //     for(int j=0;j<=n;j++)
  //         cout << dp[i][j] << " ";
  //     cout << endl;
  // }

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

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

#define int long long

using namespace std;

int dp[100010][110];
int a[110][2];

int32_t main() {
  int n, w;
  cin >> n >> w;

  for (int i = 1; i <= n; i++)
    cin >> a[i][0] >> a[i][1];

  for (int i = 1; i <= w; i++) {
    for (int j = 1; j <= n; j++) {
      dp[i][j] = dp[i][j - 1];

      if (a[j][0] <= i)
        dp[i][j] = max(dp[i][j], dp[i - a[j][0]][j - 1] + a[j][1]);
    }
  }

  // for(int i=0;i<=w;i++)
  // {
  //     for(int j=0;j<=n;j++)
  //         cout << dp[i][j] << " ";
  //     cout << endl;
  // }

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

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

using lint = long long;

lint Knapsack(int limit, vector<int> &v, vector<int> &w) {
  vector<lint> dyn(limit, -1);
  int n = w.size();
  dyn[0] = 0;
  for (int i = 0; i < n; ++i)
    for (int j = limit; j >= w[i]; --j)
      if (dyn[j - w[i]] >= 0)
        dyn[j] = max(dyn[j], dyn[j - w[i]] + v[i]);
  lint result = 0;
  for (int i = 0; i <= limit; ++i)
    result = max(result, dyn[i]);
  return result;
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n, w;
  cin >> n >> w;
  vector<int> v(n), c(n);
  for (int i = 0; i < n; ++i)
    cin >> c[i] >> v[i];
  cout << Knapsack(w, v, c) << '\n';
}

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

using lint = long long;

lint Knapsack(int limit, vector<int> &v, vector<int> &w) {
  vector<lint> dyn(limit + 1, -1);
  int n = w.size();
  dyn[0] = 0;
  for (int i = 0; i < n; ++i)
    for (int j = limit; j >= w[i]; --j)
      if (dyn[j - w[i]] >= 0)
        dyn[j] = max(dyn[j], dyn[j - w[i]] + v[i]);
  lint result = 0;
  for (int i = 0; i <= limit; ++i)
    result = max(result, dyn[i]);
  return result;
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n, w;
  cin >> n >> w;
  vector<int> v(n), c(n);
  for (int i = 0; i < n; ++i)
    cin >> c[i] >> v[i];
  cout << Knapsack(w, v, c) << '\n';
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define flp(i, a, b) for (int i = a; i < b; i++)
#define ios                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define blp(i, a, b) for (int i = a; i >= b; i--)
#define lol 1000000007

ll a[101], b[101];
ll c[201][201];

ll dp(ll n, ll w) {
  if (c[n][w] != -1) {

    return c[n][w];
  }

  if (n == 0 || w == 0) {
    c[n][w] = 0;

    return c[n][w];
  }

  if (b[n - 1] > w) {
    c[n][w] = dp(n - 1, w);
  } else {
    c[n][w] = max(a[n - 1] + dp(n - 1, w - b[n - 1]), dp(n - 1, w));
  }
  return c[n][w];
}

int main() {
  ios;
  ll n, w;
  cin >> n >> w;
  memset(c, -1, sizeof c);
  flp(i, 0, n) { cin >> b[i] >> a[i]; }
  cout << dp(n, w) << "\n";
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define flp(i, a, b) for (int i = a; i < b; i++)
#define ios                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define blp(i, a, b) for (int i = a; i >= b; i--)
#define lol 1000000007

ll a[101], b[101];
ll c[102][100002];

ll dp(ll n, ll w) {
  if (c[n][w] != -1) {

    return c[n][w];
  }

  if (n == 0 || w == 0) {
    c[n][w] = 0;

    return c[n][w];
  }

  if (b[n - 1] > w) {
    c[n][w] = dp(n - 1, w);
  } else {
    c[n][w] = max(a[n - 1] + dp(n - 1, w - b[n - 1]), dp(n - 1, w));
  }
  return c[n][w];
}

int main() {
  ios;
  ll n, w;
  cin >> n >> w;
  memset(c, -1, sizeof c);
  flp(i, 0, n) { cin >> b[i] >> a[i]; }
  cout << dp(n, w) << "\n";
}
Problem p03163 (C++)

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

int main() {

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

  ll dp[n][k];

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

  cout << dp[n][k];
}


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

int main() {

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

  ll dp[n + 1][k + 1];

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

  cout << dp[n][k];
}
Problem p03163 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define endl '\n'
#define ll long long
#define inf 1000000007
int main() {
  fastio;
#ifdef APNA_IO
  freopen("input.txt", "rt", stdin);
  freopen("output.txt", "wt", stdout);
#endif
  ll n, w;
  cin >> n >> w;
  ll dp[n + 1][w + 1];
  ll arr[n + 1], arr1[n + 1];
  for (ll i = 1; i <= n; i++) {
    cin >> arr[i] >> arr1[i];
  }
  for (ll i = 1; i < w + 1; i++) {
    if (arr[1] > i)
      dp[1][i] = 0;
    else
      dp[1][i] = arr1[1];
  }
  for (ll i = 1; i < n + 1; i++) {
    if (arr[i] > 1)
      dp[i][1] = 0;
    else
      dp[i][1] = arr1[i];
  }
  for (ll i = 0; i < n + 1; i++)
    dp[i][0] = 0;
  for (ll i = 2; i < n + 1; i++) {
    for (ll j = 2; j < w + 1; j++) {
      dp[i][j] = dp[i - 1][j];
      if (j - arr[i] >= 0) {
        for (ll k = 1; k < i; k++)
          dp[i][j] = max(dp[i][j], arr1[i] + dp[k][j - arr[i]]);
      }
    }
  }
  cout << dp[n][w] << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define endl '\n'
#define ll long long
#define inf 1000000007
int main() {
  fastio;
#ifdef APNA_IO
  freopen("input.txt", "rt", stdin);
  freopen("output.txt", "wt", stdout);
#endif
  ll n, w;
  cin >> n >> w;
  ll dp[n + 1][w + 1];
  ll arr[n + 1], arr1[n + 1];
  for (ll i = 1; i <= n; i++) {
    cin >> arr[i] >> arr1[i];
  }
  for (ll i = 1; i < w + 1; i++) {
    if (arr[1] > i)
      dp[1][i] = 0;
    else
      dp[1][i] = arr1[1];
  }
  for (ll i = 1; i < n + 1; i++) {
    if (arr[i] > 1)
      dp[i][1] = 0;
    else
      dp[i][1] = arr1[i];
  }
  for (ll i = 0; i < n + 1; i++)
    dp[i][0] = 0;
  for (ll i = 2; i < n + 1; i++) {
    for (ll j = 2; j < w + 1; j++) {
      dp[i][j] = dp[i - 1][j];
      if (j - arr[i] >= 0) {
        dp[i][j] = max(dp[i][j], arr1[i] + dp[i - 1][j - arr[i]]);
      }
    }
  }
  cout << dp[n][w] << endl;

  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
int N = 0;
int W = 0;
int w[100];
long long v[100];
long long dp[110][110] = {0};
int main(void) {
  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++) {
      if (w[i] <= j) {
        dp[i + 1][j] = max(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 <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
int N = 0;
int W = 0;
int w[100];
long long v[100];
long long dp[110][1000010] = {0};
int main(void) {
  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++) {
      if (w[i] <= j) {
        dp[i + 1][j] = max(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>
using namespace std;
typedef long long ll;

ll w[105], f[105];
ll v[105];

signed main() {
  ll N, W;
  while (cin >> N >> W) {
    for (ll i = 1; i <= N; i++)
      cin >> w[i] >> v[i];

    // 01背包
    memset(f, 0, sizeof f);
    for (ll i = 1; i <= N; i++) {
      for (ll j = W; j >= w[i]; j--)
        f[j] = max(f[j], f[j - w[i]] + v[i]);
    }

    cout << f[W] << endl;
  }
}

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

ll w[105], f[100005];
ll v[105];

signed main() {
  ll N, W;
  while (cin >> N >> W) {
    for (ll i = 1; i <= N; i++)
      cin >> w[i] >> v[i];

    // 01背包
    memset(f, 0, sizeof f);
    for (ll i = 1; i <= N; i++) {
      for (ll j = W; j >= w[i]; j--)
        f[j] = max(f[j], f[j - w[i]] + v[i]);
    }

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

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

int n, w;
int const pp = 10e5;
int arr[32][2];
long long dp[30][pp];

long long solve(int idx, int rem) {
  if (idx >= n)
    return 0;
  long long &ret = dp[idx][rem];
  if (ret)
    return ret;
  if (rem - arr[idx][0] >= 0)
    ret = arr[idx][1] + solve(idx + 1, rem - arr[idx][0]);
  return ret = max(solve(idx + 1, rem), ret);
}
int main() {
  // freopen("input.txt", "rt", stdin);
  // freopen("output.txt", "wt", stdout);

  cin >> n >> w;
  for (int i = 0; i < n; i++)
    cin >> arr[i][0] >> arr[i][1];
  printf("%lld\n", solve(0, w));
  return 0;
}

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

int n, w;
int const pp = 10e5;
int arr[102][2];
long long dp[102][pp];

long long solve(int idx, int rem) {
  if (idx >= n)
    return 0;
  long long &ret = dp[idx][rem];
  if (ret)
    return ret;
  if (rem - arr[idx][0] >= 0)
    ret = arr[idx][1] + solve(idx + 1, rem - arr[idx][0]);
  return ret = max(solve(idx + 1, rem), ret);
}
int main() {
  // freopen("input.txt", "rt", stdin);
  // freopen("output.txt", "wt", stdout);

  cin >> n >> w;
  for (int i = 0; i < n; i++)
    cin >> arr[i][0] >> arr[i][1];
  printf("%lld\n", solve(0, w));
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
// In The Name Of Allah //
#include <bits/stdc++.h>
#define pb emplace_back
#define inf (int)1e9
#define ff first
#define ss second
#define ll long long
#define sp(n) cout << setprecision(n) << fixed
#define hellcat ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define give(x) cout << #x << " " << x << endl
#define debug cout << "dddd" << '\n'
#define debarr(a, n)                                                           \
  for (int i = 0; i < n; ++i)                                                  \
    cout << a[i] << ' ';                                                       \
  cout << endl
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
#pragma GCC optimize("no-stack-protector,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;

#define int long long

template <class S, class T> inline S smax(S &l, T r) {
  return l = max(l, (S)r);
}
template <class S, class T> inline bool smin(S &l, T r) {
  return l = min(l, (S)r);
}

const int MAXN = 2e3 + 10;
int dp[MAXN], n, s, mx = 0;
pair<int, int> p[MAXN];

int32_t main() {
  hellcat;

  cin >> n >> s;
  for (int i = 1; i <= n; i++)
    cin >> p[i].first >> p[i].second;
  for (int i = 1; i <= n; i++)
    for (int j = s; j >= p[i].first; j--)
      dp[j] = max(dp[j], dp[j - p[i].first] + p[i].second);

  cout << dp[s] << '\n';

  return 0;
}


Fixed submission:
// In The Name Of Allah //
#include <bits/stdc++.h>
#define pb emplace_back
#define inf (int)1e9
#define ff first
#define ss second
#define ll long long
#define sp(n) cout << setprecision(n) << fixed
#define hellcat ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define give(x) cout << #x << " " << x << endl
#define debug cout << "dddd" << '\n'
#define debarr(a, n)                                                           \
  for (int i = 0; i < n; ++i)                                                  \
    cout << a[i] << ' ';                                                       \
  cout << endl
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
#pragma GCC optimize("no-stack-protector,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;

#define int long long

template <class S, class T> inline S smax(S &l, T r) {
  return l = max(l, (S)r);
}
template <class S, class T> inline bool smin(S &l, T r) {
  return l = min(l, (S)r);
}

const int MAXN = 2e5 + 10;
int dp[MAXN], n, s, mx = 0;
pair<int, int> p[MAXN];

int32_t main() {
  hellcat;

  cin >> n >> s;
  for (int i = 1; i <= n; i++)
    cin >> p[i].first >> p[i].second;
  for (int i = 1; i <= n; i++)
    for (int j = s; j >= p[i].first; j--)
      dp[j] = max(dp[j], dp[j - p[i].first] + p[i].second);

  cout << dp[s] << '\n';

  return 0;
}
Problem p03163 (C++)

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

#define F first
#define S second
#define PB push_back
#define int ll

using namespace std;

typedef long long ll;

signed main() {

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

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int n, w;
  cin >> n >> w;

  vector<pair<int, int>> items;
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    if (a > w)
      continue;
    items.PB({a, b});
  }

  int dp[w + 1];
  dp[0] = 0;
  for (int i = 1; i <= w; i++) {
    dp[i] = INT_MIN;
  }

  for (int i = 0; i < n; i++) {
    for (int j = w; j >= items[i].F; j--) {
      if (dp[j - items[i].F] == INT_MIN)
        continue;
      dp[j] = max(dp[j], items[i].S + dp[j - items[i].F]);
    }
  }

  int mmax = -1;
  for (int i = 0; i <= w; i++) {
    mmax = max(mmax, dp[i]);
  }

  cout << mmax << "\n";

  return 0;
}

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

#define F first
#define S second
#define PB push_back
#define int ll

using namespace std;

typedef long long ll;

signed main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int n, w;
  cin >> n >> w;

  vector<pair<int, int>> items;
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    if (a > w)
      continue;
    items.PB({a, b});
  }

  int dp[w + 1];
  dp[0] = 0;
  for (int i = 1; i <= w; i++) {
    dp[i] = INT_MIN;
  }

  for (int i = 0; i < n; i++) {
    for (int j = w; j >= items[i].F; j--) {
      if (dp[j - items[i].F] == INT_MIN)
        continue;
      dp[j] = max(dp[j], items[i].S + dp[j - items[i].F]);
    }
  }

  int mmax = -1;
  for (int i = 0; i <= w; i++) {
    mmax = max(mmax, dp[i]);
  }

  cout << mmax << "\n";

  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll mod = 1e9 + 7;
int dy[] = {1, -1, 0, 0};
int dx[] = {0, 0, 1, -1};

int main() {
  int N, W;
  cin >> N >> W;
  vector<int> w(N), v(N);

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

  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll mod = 1e9 + 7;
int dy[] = {1, -1, 0, 0};
int dx[] = {0, 0, 1, -1};

int main() {
  int N, W;
  cin >> N >> W;
  vector<int> w(N), v(N);

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

  cout << ans << endl;
  return 0;
}
Problem p03163 (C++)

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

#define int long long

int knapsack(int w, int wt[], int val[], int n) {
  int dp[n + 1][w + 1];
  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] = max(val[i - 1] + dp[i - 1][j - wt[i - 1]], dp[i - 1][j]);
      } else {
        dp[i][j] = dp[i - 1][j];
      }
    }
  }
  return dp[n][w];
}

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

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

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

  return 0;
}

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

#define int long long

int knapsack(int w, int wt[], int val[], int n) {
  int dp[n + 1][w + 1];
  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] = max(val[i - 1] + dp[i - 1][j - wt[i - 1]], dp[i - 1][j]);
      } else {
        dp[i][j] = dp[i - 1][j];
      }
    }
  }
  return dp[n][w];
}

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

  if (fopen("input.txt", "r")) {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
  }

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

  return 0;
}
Problem p03163 (C++)

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

#define fo(i, n) for (ll i = 0; i < n; i++)
#define endl "\n"
#define of(i, n) for (ll i = n - 1; i >= 0; i--)
#define ll long long
#define vec vector<ll>
#define fio                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define tr(container, it)                                                      \
  for (typeof(container.begin()) it = container.begin();                       \
       it != container.end(); it++)

ll mod = 1e9 + 7;

#define umap unordered_map

string t;

ll sub(string s) {
  ll l = 0, h = s.length();
  ll j = 0;
  for (int i = 0; i < s.length() && j < t.length(); i++)
    if (t[j] == s[i])
      j++;

  return (j == t.length());
}

ll check(ll n, string s) {
  ll l, h;
  l = 0;
  h = n - 1;
  while (h < s.length()) {
    if (sub(s.substr(l, h)))
      return 1;
    l++;
    h++;
  }
  return 0;
}

ll ab(ll x) {
  if (x < 0)
    x *= -1;
  return x;
}

int main() {
  fio;
  ll n, w;
  cin >> n >> w;
  ll dp[n][w];
  int wt[n], v[n];
  fo(i, n) cin >> wt[i] >> v[i];

  fo(i, n + 1) {
    fo(j, w + 1) {
      if (i == 0 || j == 0)
        dp[i][j] = 0;
      else if (wt[i - 1] <= j) {
        dp[i][j] = max(v[i - 1] + dp[i - 1][j - wt[i - 1]], dp[i - 1][j]);
      } else
        dp[i][j] = dp[i - 1][j];
    }
  }
  cout << dp[n][w] << endl;
}


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

#define fo(i, n) for (ll i = 0; i < n; i++)
#define endl "\n"
#define of(i, n) for (ll i = n - 1; i >= 0; i--)
#define ll long long
#define vec vector<ll>
#define fio                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define tr(container, it)                                                      \
  for (typeof(container.begin()) it = container.begin();                       \
       it != container.end(); it++)

ll mod = 1e9 + 7;

#define umap unordered_map

string t;

ll sub(string s) {
  ll l = 0, h = s.length();
  ll j = 0;
  for (int i = 0; i < s.length() && j < t.length(); i++)
    if (t[j] == s[i])
      j++;

  return (j == t.length());
}

ll check(ll n, string s) {
  ll l, h;
  l = 0;
  h = n - 1;
  while (h < s.length()) {
    if (sub(s.substr(l, h)))
      return 1;
    l++;
    h++;
  }
  return 0;
}

ll ab(ll x) {
  if (x < 0)
    x *= -1;
  return x;
}

int main() {
  fio;
  ll n, w;
  cin >> n >> w;
  ll dp[n + 1][w + 1];
  int wt[n], v[n];
  fo(i, n) cin >> wt[i] >> v[i];

  fo(i, n + 1) {
    fo(j, w + 1) {
      if (i == 0 || j == 0)
        dp[i][j] = 0;
      else if (wt[i - 1] <= j) {
        dp[i][j] = max(v[i - 1] + dp[i - 1][j - wt[i - 1]], dp[i - 1][j]);
      } else
        dp[i][j] = dp[i - 1][j];
    }
  }
  cout << dp[n][w] << endl;
}
Problem p03163 (C++)

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n;
  long long w, w_arr[100], v_arr[100], dp[101]; // (W+1)
  memset(dp, 0, sizeof(dp));
  cin >> n >> w;
  for (int i = 0; i < n; i++) {
    cin >> w_arr[i] >> v_arr[i];
  }
  for (int i = 0; i < n; i++) {
    for (long long j = w; j >= w_arr[i]; j--) {
      dp[j] = max(dp[j], v_arr[i] + dp[j - w_arr[i]]);
    }
  }
  cout << dp[w];

  return 0;
}

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n;
  long long w, w_arr[100], v_arr[100], dp[100001]; // (W+1)
  memset(dp, 0, sizeof(dp));
  cin >> n >> w;
  for (int i = 0; i < n; i++) {
    cin >> w_arr[i] >> v_arr[i];
  }
  for (int i = 0; i < n; i++) {
    for (long long j = w; j >= w_arr[i]; j--) {
      dp[j] = max(dp[j], v_arr[i] + dp[j - w_arr[i]]);
    }
  }
  cout << dp[w];

  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
typedef long long ll;
const ll inf = 1LL << 60;

int n, W;
ll dp[100][100010];
ll w[110];
ll v[110];

int main() {
  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 j = 0; j <= W; j++) {
      if (j < w[i])
        dp[i][j] = dp[i + 1][j];
      else {
        dp[i][j] = max(dp[i + 1][j], dp[i + 1][j - w[i]] + v[i]);
      }
    }
  }

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

  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;
typedef long long ll;
const ll inf = 1LL << 60;

int n, W;
ll dp[110][100010] = {0};
ll w[110];
ll v[110];

int main() {
  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 j = 0; j <= W; j++) {
      if (j < w[i])
        dp[i][j] = dp[i + 1][j];
      else {
        dp[i][j] = max(dp[i + 1][j], dp[i + 1][j - w[i]] + v[i]);
      }
    }
  }

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

  return 0;
}
Problem p03163 (C++)

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

ll n, W;
ll w[110], v[110];
vector<vector<ll>> dp(110, vector<ll>(110, -1));
ll rec(ll i, ll j) {
  if (dp.at(i).at(j) != -1) {
    return dp.at(i).at(j);
  }

  ll res = 0;
  if (i == n) {
    res = 0;
  } else if (j < w[i]) {
    res = rec(i + 1, j);
  } else {
    res = max(rec(i + 1, j), rec(i + 1, j - w[i]) + v[i]);
  }
  // cout << "res" << i << ',' << j << "=" << res << endl;
  return dp.at(i).at(j) = res;
}

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

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

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
// ok 理解
using namespace std;
using ll = long long;

ll n, W;
ll w[110], v[110];
vector<vector<ll>> dp(110, vector<ll>(100010, -1));
ll rec(ll i, ll j) {
  if (dp.at(i).at(j) != -1) {
    return dp.at(i).at(j);
  }

  ll res = 0;
  if (i == n) {
    res = 0;
  } else if (j < w[i]) {
    res = rec(i + 1, j);
  } else {
    res = max(rec(i + 1, j), rec(i + 1, j - w[i]) + v[i]);
  }
  // cout << "res" << i << ',' << j << "=" << res << endl;
  return dp.at(i).at(j) = res;
}

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

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

  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int n, W, v[107], w[107];
long long dp[107][107];
int main() {
  ios::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
  cin >> n >> W;
  for (int i = 1; i <= n; i++) {
    cin >> w[i] >> v[i];
  }
  for (int i = 1; i <= n; i++) {
    for (int wt = 1; wt <= W; wt++) {
      if (w[i] <= wt) {
        dp[i][wt] = max(v[i] + dp[i - 1][wt - w[i]], dp[i - 1][wt]);
      } else
        dp[i][wt] = dp[i - 1][wt];
    }
  }
  cout << dp[n][W];
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, W, v[107], w[107];
long long dp[107][100007];
int main() {
  ios::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
  cin >> n >> W;
  for (int i = 1; i <= n; i++) {
    cin >> w[i] >> v[i];
  }
  for (int i = 1; i <= n; i++) {
    for (int wt = 1; wt <= W; wt++) {
      if (w[i] <= wt) {
        dp[i][wt] = max(v[i] + dp[i - 1][wt - w[i]], dp[i - 1][wt]);
      } else
        dp[i][wt] = dp[i - 1][wt];
    }
  }
  cout << dp[n][W];
}
Problem p03163 (C++)

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

long long
    dp[110][100010]; // dp[i][j]はi番目までの品物を重さjまでに入れたときの最大値

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

  // dp初期条件
  for (int j = 0; j <= W; j++)
    dp[0][j] = 0;

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

  return 0;
}

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

long long
    dp[110][100010]; // dp[i][j]はi番目までの品物を重さjまでに入れたときの最大値

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

  // dp初期条件
  for (int j = 0; j <= W; j++)
    dp[0][j] = 0;

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

  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#pragma GCC optimize("-O3")
#define ll long long
#define en "\n"
const ll inf = 1e9;
const ll mod = 1e9 + 7;
using namespace std;
int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  ll a[100005], b[100005], m, n, dp[100005] = {0};
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> a[i] >> b[i];
  }
  for (int i = 1; i <= n; i++) {
    for (int j = m; j > 0; j--) {
      if (dp[j] != 0) {
        dp[j + a[i]] = max(dp[j + a[i]], b[i] + dp[j]);
      }
    }
    dp[a[i]] = max(dp[a[i]], b[i]);
  }
  ll mx = -inf * inf;
  for (int i = m; i > 0; i--) {
    mx = max(mx, dp[i]);
  }
  cout << mx << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#pragma GCC optimize("-O3")
#define ll long long
#define en "\n"
const ll inf = 1e9;
const ll mod = 1e9 + 7;
using namespace std;
int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  ll a[100005], b[100005], m, n, dp[300005] = {0};
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> a[i] >> b[i];
  }
  for (int i = 1; i <= n; i++) {
    for (int j = m; j > 0; j--) {
      if (dp[j] != 0) {
        dp[j + a[i]] = max(dp[j + a[i]], b[i] + dp[j]);
      }
    }
    dp[a[i]] = max(dp[a[i]], b[i]);
  }
  ll mx = -inf * inf;
  for (int i = m; i > 0; i--) {
    mx = max(mx, dp[i]);
  }
  cout << mx << endl;
  return 0;
}
Problem p03163 (C++)

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

using namespace std;

const int mx = 1e5 + 123;
#define mem(a, b) memset(a, b, sizeof(a));

int w[120], v[120], W, n;
long long dp[120][mx];

long long solve(int i, int j) {

  if (i > n)
    return 0;
  if (dp[i][j] != -1)
    return dp[i][j];

  long long left = 0, right = 0;

  if (j + w[i] <= W)
    3;
  left = v[i] + solve(i + 1, j + w[i]);
  right = solve(i + 1, j);

  return dp[i][j] = max(left, right);
}

int main() {

  cin >> n >> W;

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

  mem(dp, -1);
  cout << solve(1, 0) << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <list>
#include <queue>
#include <vector>

using namespace std;

const int mx = 1e5 + 123;
#define mem(a, b) memset(a, b, sizeof(a));

int w[120], v[120], W, n;
long long dp[120][mx];

long long solve(int i, int j) {

  if (i > n)
    return 0;
  if (dp[i][j] != -1)
    return dp[i][j];

  long long left = 0, right = 0;

  if (j + w[i] <= W)
    left = v[i] + solve(i + 1, j + w[i]);
  right = solve(i + 1, j);

  return dp[i][j] = max(left, right);
}

int main() {

  cin >> n >> W;

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

  mem(dp, -1);
  cout << solve(1, 0) << endl;

  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
int dp[123456];
int N, W, price[123], wt[123];
int32_t main() {
  cin >> N >> W;
  for (int i = 0; i < N; i++) {
    cin >> wt[i] >> price[i];
  }

  for (int i = 0; i < N; i++) {
    for (int j = W - wt[i]; j >= 0; j--) {
      dp[j + wt[i]] = max(dp[W + wt[i]], dp[j] + price[i]);
    }
  }

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
int dp[123456];
int N, W, price[123], wt[123];
int32_t main() {
  cin >> N >> W;
  for (int i = 0; i < N; i++) {
    cin >> wt[i] >> price[i];
  }

  for (int i = 0; i < N; i++) {
    for (int j = W - wt[i]; j >= 0; j--) {
      dp[j + wt[i]] = max(dp[j + wt[i]], dp[j] + price[i]);
    }
  }

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

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

using namespace std;

int main() {

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

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

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

  return 1;
}

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

using namespace std;

int main() {

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

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

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

  return 0;
}