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

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

#define ll long long int
#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define nl '\n'
#define repd(i, a, b) for (int i = a; i >= b; --i)
#define pb push_back
#define all(a) a.begin(), a.end()
#define F first
#define S second
const ll N = 1e6 + 9;
const ll mod = 1e9 + 7;

ll a[100][N];
vector<pair<ll, ll>> v;

void solve() {
  ll n, w, w1, v1;
  cin >> n >> w;
  rep(i, 1, n) {
    cin >> w1 >> v1;
    v.pb({w1, v1});
  }
  v.pb({0, 0});
  sort(all(v));

  rep(i, 1, n) {
    rep(j, 1, w) {
      if (j - v[i].F >= 0)
        a[i][j] = max(a[i - 1][j], a[i - 1][j - v[i].F] + v[i].S);
      else
        a[i][j] = a[i - 1][j]; //,cout<<"A";
      //	cout<<a[i][j]<<" ";
    }
    // cout<<nl;
  }
  cout << a[n][w];
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll t = 1;
  // pre();
  // cin>>t;
  // if(t^1)exit(0);
  while (t--)
    solve();
}

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

#define ll long long int
#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define nl '\n'
#define repd(i, a, b) for (int i = a; i >= b; --i)
#define pb push_back
#define all(a) a.begin(), a.end()
#define F first
#define S second
const ll N = 1e6 + 9;
const ll mod = 1e9 + 7;

ll a[105][N];
vector<pair<ll, ll>> v;

void solve() {
  ll n, w, w1, v1;
  cin >> n >> w;
  rep(i, 1, n) {
    cin >> w1 >> v1;
    v.pb({w1, v1});
  }
  v.pb({0, 0});
  sort(all(v));

  rep(i, 1, n) {
    rep(j, 1, w) {
      if (j - v[i].F >= 0)
        a[i][j] = max(a[i - 1][j], a[i - 1][j - v[i].F] + v[i].S);
      else
        a[i][j] = a[i - 1][j]; //,cout<<"A";
      //	cout<<a[i][j]<<" ";
    }
    // cout<<nl;
  }
  cout << a[n][w];
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll t = 1;
  // pre();
  // cin>>t;
  // if(t^1)exit(0);
  while (t--)
    solve();
}
Problem p03163 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 1e5 + 10;
long long n, W;
vector<long long> v, w;
long long dp[110][MAXN];
long long dfs(long long x, long long curr) {
  if (x == n) {
    return 0;
  }
  // if ( dp[x][curr] != -1 ) return dp[x][curr] ;
  long long choice1 = 0 + dfs(x + 1, curr);
  long long choice2 = -1e18;
  if (curr + w[x] <= W) {
    choice2 = v[x] + dfs(x + 1, curr + w[x]);
  }
  long long ans = max(choice1, choice2);
  dp[x][curr] = ans;
  return ans;
}

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 1e5 + 10;
long long n, W;
vector<long long> v, w;
long long dp[110][MAXN];
long long dfs(long long x, long long curr) {
  if (x == n) {
    return 0;
  }
  if (dp[x][curr] != -1)
    return dp[x][curr];
  long long choice1 = 0 + dfs(x + 1, curr);
  long long choice2 = -1e18;
  if (curr + w[x] <= W) {
    choice2 = v[x] + dfs(x + 1, curr + w[x]);
  }
  long long ans = max(choice1, choice2);
  dp[x][curr] = ans;
  return ans;
}

int main() {
  memset(dp, -1, sizeof dp);
  cin >> n >> W;
  v.resize(n);
  w.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> w[i] >> v[i];
  }
  cout << dfs(0, 0) << endl;
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long int l;
l knapsack(l w[], l v[], l W, l n) {
  if (n == 0 || W == 0)
    return 0;
  if (w[n - 1] <= W) {
    return max(v[n - 1] + knapsack(w, v, W - w[n - 1], n - 1),
               knapsack(w, v, W, n - 1));
  } else
    return knapsack(w, v, W, n - 1);
}
int main() {
  l n, W;
  cin >> n >> W;
  l v[n], w[n];
  for (l i = 0; i < n; i++) {
    cin >> w[i] >> v[i];
  }
  cout << knapsack(w, v, W, n) << "\n";

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long int l;
l knapsack(l wt[], l v[], l W, l n) {
  l dp[n + 1][W + 1];
  for (l i = 0; i <= n; i++) {

    for (l w = 0; w <= W; w++) {
      if (i == 0 || w == 0)
        dp[i][w] = 0;
      else if (wt[i - 1] <= w) {
        dp[i][w] = max(v[i - 1] + dp[i - 1][w - wt[i - 1]], dp[i - 1][w]);
      } else
        dp[i][w] = dp[i - 1][w];
    }
  }
  return dp[n][W];
}
int main() {
  l n, W;
  cin >> n >> W;
  l v[n], w[n];
  for (l i = 0; i < n; i++) {
    cin >> w[i] >> v[i];
  }
  cout << knapsack(w, v, W, n) << "\n";

  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define M 998244353
#define mod 1000000007
#define all(v) v.begin(), v.end()
#define ios                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define F first
#define S second
#define zero(dp) memset(dp, 0, sizeof(dp))
#define pb emplace_back
using namespace std;
int32_t main() {

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

  ios int t = 1;
  // cin>>t;
  while (t--) {
    int n;
    cin >> n;
    int W;
    cin >> W;
    int w[n], v[n];
    for (int i = 0; i < n; i++)
      cin >> w[i] >> v[i];
    int dp[n][W + 1];
    zero(dp);
    for (int i = 0; i < n; i++) {
      for (int j = 1; j <= W; j++)
        dp[i][j] = -1;
    }
    dp[0][w[0]] = v[0];
    for (int i = 1; i < n; i++) {
      for (int j = 0; j <= W; j++) {
        if (dp[i - 1][j] != -1 and j + w[i] <= W)
          dp[i][j + w[i]] = dp[i - 1][j] + v[i];
      }
      for (int j = 0; j <= W; j++)
        dp[i][j] = max(dp[i - 1][j], dp[i][j]);
    }
    int ans = 0;
    for (int i = 0; i <= W; i++)
      ans = max(ans, dp[n - 1][i]);
    cout << ans;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define M 998244353
#define mod 1000000007
#define all(v) v.begin(), v.end()
#define ios                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define F first
#define S second
#define zero(dp) memset(dp, 0, sizeof(dp))
#define pb emplace_back
using namespace std;
int32_t main() {
  /*
  #ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
      freopen("output.txt", "w", stdout);
  #endif
  */
  ios int t = 1;
  // cin>>t;
  while (t--) {
    int n;
    cin >> n;
    int W;
    cin >> W;
    int w[n], v[n];
    for (int i = 0; i < n; i++)
      cin >> w[i] >> v[i];
    int dp[n][W + 1];
    zero(dp);
    for (int i = 0; i < n; i++) {
      for (int j = 1; j <= W; j++)
        dp[i][j] = -1;
    }
    dp[0][w[0]] = v[0];
    for (int i = 1; i < n; i++) {
      for (int j = 0; j <= W; j++) {
        if (dp[i - 1][j] != -1 and j + w[i] <= W)
          dp[i][j + w[i]] = dp[i - 1][j] + v[i];
      }
      for (int j = 0; j <= W; j++)
        dp[i][j] = max(dp[i - 1][j], dp[i][j]);
    }
    int ans = 0;
    for (int i = 0; i <= W; i++)
      ans = max(ans, dp[n - 1][i]);
    cout << ans;
  }
}
Problem p03163 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
vector<vector<ll>> dp(200, vector<ll>(1000000, -1));
ll ans(ll a[][2], ll w, ll i) {

  if (dp[i][w] != -1)
    return dp[i][w];
  if (i == 0 || w == 0) {
    dp[i][w] = 0;
    return dp[i][w];
  }
  if (w - a[i - 1][0] >= 0) {
    dp[i][w] =
        max(ans(a, w - a[i - 1][0], i - 1) + a[i - 1][1], ans(a, w, i - 1));
    return dp[i][w];
  } else {
    dp[i][w] = ans(a, w, i - 1);
    return dp[i][w];
  }
}
int main() {
  ll n, w;
  cin >> n >> w;
  ll a[n][2];
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i][0] >> a[i][1];
  }
  ll x = ans(a, w, n);
  cout << x << endl;
}

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

using namespace std;
typedef long long ll;
vector<vector<ll>> dp(108, vector<ll>(100009, -1));
ll ans(ll a[][2], ll w, ll i) {

  if (dp[i][w] != -1)
    return dp[i][w];
  if (i == 0 || w == 0) {
    dp[i][w] = 0;
    return dp[i][w];
  }
  if (w - a[i - 1][0] >= 0) {
    dp[i][w] =
        max(ans(a, w - a[i - 1][0], i - 1) + a[i - 1][1], ans(a, w, i - 1));
    return dp[i][w];
  } else {
    dp[i][w] = ans(a, w, i - 1);
    return dp[i][w];
  }
}
int main() {
  ll n, w;
  cin >> n >> w;
  ll a[n][2];
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i][0] >> a[i][1];
  }
  ll x = ans(a, w, n);
  cout << x << endl;
}
Problem p03163 (C++)

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

int main() {
  // your code goes here
  long long n, W;
  cin >> n >> W;
  long long weight[n], values[n];
  long long dp[n][W + 1];
  for (int i = 0; i < n; ++i) {
    cin >> weight[i] >> values[i];
  }
  for (int i = 0; i <= n; i++) {
    for (int w = 0; w <= W; w++) {
      if (i == 0 || w == 0)
        dp[i][w] = 0;
      else if (weight[i - 1] <= w)
        dp[i][w] =
            max(values[i - 1] + dp[i - 1][w - weight[i - 1]], dp[i - 1][w]);
      else
        dp[i][w] = dp[i - 1][w];
    }
  }
  cout << dp[n][W] << endl;
}


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

int main() {
  // your code goes here
  long long n, W;
  cin >> n >> W;
  long long weight[n], values[n];
  long long dp[n + 1][W + 1];
  for (int i = 0; i < n; ++i) {
    cin >> weight[i] >> values[i];
  }
  for (int i = 0; i <= n; i++) {
    for (int w = 0; w <= W; w++) {
      if (i == 0 || w == 0)
        dp[i][w] = 0;
      else if (weight[i - 1] <= w)
        dp[i][w] =
            max(values[i - 1] + dp[i - 1][w - weight[i - 1]], dp[i - 1][w]);
      else
        dp[i][w] = dp[i - 1][w];
    }
  }
  cout << dp[n][W] << endl;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#define ll long long int
using namespace std;
int main() {
  ll n, W;
  cin >> n >> W;

  ll i, j;
  ll v[200];
  ll w[200];
  ll dp[200] = {0};

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

  for (i = 0; i < n; i++) {
    for (j = W; j >= w[i]; j--)
      dp[j] = max(dp[j], dp[j - w[i]] + v[i]);
  }
  cout << dp[W];
}

Fixed submission:
#include <iostream>
#define ll long long int
using namespace std;
int main() {
  ll n, W;
  cin >> n >> W;

  ll i, j;
  ll v[200];
  ll w[200];
  ll dp[100005] = {0};

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

  for (i = 0; i < n; i++) {
    for (j = W; j >= w[i]; j--)
      dp[j] = max(dp[j], dp[j - w[i]] + v[i]);
  }
  cout << dp[W];
}
Problem p03163 (C++)

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

ll dp[101][10001] = {0};

ll helper(vector<int> &w, vector<int> &v, int index, int capacity) {
  if (dp[index][capacity] != 0)
    return dp[index][capacity];
  if (index == w.size())
    return 0;
  if (capacity < w[index])
    return helper(w, v, index + 1, capacity);
  ll notSelected = helper(w, v, index + 1, capacity);
  ll selected = helper(w, v, index + 1, capacity - w[index]) + v[index];
  dp[index][capacity] = max(notSelected, selected);
  return dp[index][capacity];
}

int main() {
  int n, capacity;
  cin >> n >> capacity;
  vector<int> w(n);
  vector<int> v(n);
  for (int i = 0; i < n; i++)
    cin >> w[i] >> v[i];
  cout << helper(w, v, 0, capacity) << endl;
  return 0;
}

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

ll dp[101][100001] = {0};

ll helper(vector<int> &w, vector<int> &v, int index, int capacity) {
  if (dp[index][capacity] != 0)
    return dp[index][capacity];
  if (index == w.size())
    return 0;
  if (capacity < w[index])
    return helper(w, v, index + 1, capacity);
  ll notSelected = helper(w, v, index + 1, capacity);
  ll selected = helper(w, v, index + 1, capacity - w[index]) + v[index];
  dp[index][capacity] = max(notSelected, selected);
  return dp[index][capacity];
}

int main() {
  int n, capacity;
  cin >> n >> capacity;
  vector<int> w(n);
  vector<int> v(n);
  for (int i = 0; i < n; i++)
    cin >> w[i] >> v[i];
  cout << helper(w, v, 0, capacity) << endl;
  return 0;
}
Problem p03163 (C++)

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

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

  ll weight[n], value[n];

  for (int i = 0; i < n; i++) {
    cin >> weight[i] >> value[i];
  }

  ll 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 (weight[i - 1] <= w)
        dp[i][j] =
            max(dp[i - 1][j], value[i - 1] + dp[i - 1][j - weight[i - 1]]);
      else
        dp[i][j] = dp[i - 1][j];
    }
  }
  cout << dp[n][w];
}

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

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

  ll weight[n], value[n];

  for (int i = 0; i < n; i++) {
    cin >> weight[i] >> value[i];
  }

  ll 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 (weight[i - 1] <= j)
        dp[i][j] =
            max(dp[i - 1][j], value[i - 1] + dp[i - 1][j - weight[i - 1]]);
      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;
typedef long long ll;

const ll w_max = 10005;
const ll n_max = 105;

ll dp[w_max][n_max];

int main() {
  ll N, W;
  cin >> N >> W;

  vector<ll> v(N + 1);
  vector<ll> w(N + 1);

  for (ll i = 1; i <= N; i++)
    cin >> w[i] >> v[i];

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

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

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

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

const ll w_max = 100005;
const ll n_max = 105;

ll dp[w_max][n_max];

int main() {
  ll N, W;
  cin >> N >> W;

  vector<ll> v(N + 1);
  vector<ll> w(N + 1);

  for (ll i = 1; i <= N; i++)
    cin >> w[i] >> v[i];

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

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

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mod 1000000007
using namespace std;
ll power(ll x, ll y) {
  ll res = 1;
  while (y > 0) {
    if (y % 2 == 1)
      res = (res * x);
    x = (x * x);
    y = y / 2;
  }
  return res;
}
int main() {
  ll n, W;
  cin >> n >> W;
  ll val[n + 1], wt[n + 1], knap[n + 1][W + 1];
  for (ll i = 0; i < n; i++)
    cin >> wt[i] >> val[i];
  //         W=8
  //         val{2,3,1,4}//see video abdul bari
  //         wt{3,4,6,5}
  //         0 1 2 3 4 5 6 7 8 i--->
  // vi wi 0 0 0 0 0 0 0 0 0 0 j---v (downwards)
  //  2  3 1 0
  //  3  4 2 0
  //  1  6 3 0
  //  4  5 4 0
  for (ll i = 0; i <= n; i++) {
    for (ll j = 0; j <= W; j++) {
      if (i == 0 || j == 0)
        knap[i][j] = 0;
      else if (j >= wt[i])
        knap[i][j] =
            max(knap[i - 1][j], knap[i - 1][j - wt[i - 1]] + val[i - 1]);
      else
        knap[i][j] = knap[i - 1][j];
    }
  }
  cout << knap[n][W];
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mod 1000000007
using namespace std;
ll power(ll x, ll y) {
  ll res = 1;
  while (y > 0) {
    if (y % 2 == 1)
      res = (res * x);
    x = (x * x);
    y = y / 2;
  }
  return res;
}
int main() {
  ll n, W;
  cin >> n >> W;
  ll val[n + 1], wt[n + 1], knap[n + 1][W + 1];
  for (ll i = 0; i < n; i++)
    cin >> wt[i] >> val[i];
  //         W=8
  //         val{2,3,1,4}//see video abdul bari
  //         wt{3,4,6,5}
  //         0 1 2 3 4 5 6 7 8 i--->
  // vi wi 0 0 0 0 0 0 0 0 0 0 j---v (downwards)
  //  2  3 1 0
  //  3  4 2 0
  //  1  6 3 0
  //  4  5 4 0
  for (ll i = 0; i <= n; i++) {
    for (ll j = 0; j <= W; j++) {
      if (i == 0 || j == 0)
        knap[i][j] = 0;
      else if (j >= wt[i - 1])
        knap[i][j] =
            max(knap[i - 1][j], knap[i - 1][j - wt[i - 1]] + val[i - 1]);
      else
        knap[i][j] = knap[i - 1][j];
    }
  }
  cout << knap[n][W];
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mod 1000000007
using namespace std;
ll power(ll x, ll y) {
  ll res = 1;
  while (y > 0) {
    if (y % 2 == 1)
      res = (res * x);
    x = (x * x);
    y = y / 2;
  }
  return res;
}
int main() {
  ll n, W;
  cin >> n >> W;
  ll val[n + 1], wt[n + 1], knap[n + 1][W + 1];
  for (ll i = 0; i < n; i++)
    cin >> wt[i] >> val[i];
  //         W=8
  //         val{2,3,1,4}//see video abdul bari
  //         wt{3,4,6,5}
  //         0 1 2 3 4 5 6 7 8 i--->
  // vi wi 0 0 0 0 0 0 0 0 0 0 j---v (downwards)
  //  2  3 1 0
  //  3  4 2 0
  //  1  6 3 0
  //  4  5 4 0
  for (ll i = 0; i <= n; i++) {
    for (ll j = 0; j <= W; j++) {
      if (i == 0 || j == 0)
        knap[i][j] = 0;
      else if (j - wt[i] >= 0)
        knap[i][j] =
            max(knap[i - 1][j], knap[i - 1][j - wt[i - 1]] + val[i - 1]);
      else
        knap[i][j] = knap[i - 1][j];
    }
  }
  cout << knap[n][W];
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mod 1000000007
using namespace std;
ll power(ll x, ll y) {
  ll res = 1;
  while (y > 0) {
    if (y % 2 == 1)
      res = (res * x);
    x = (x * x);
    y = y / 2;
  }
  return res;
}
int main() {
  ll n, W;
  cin >> n >> W;
  ll val[n + 1], wt[n + 1], knap[n + 1][W + 1];
  for (ll i = 0; i < n; i++)
    cin >> wt[i] >> val[i];
  //         W=8
  //         val{2,3,1,4}//see video abdul bari
  //         wt{3,4,6,5}
  //         0 1 2 3 4 5 6 7 8 i--->
  // vi wi 0 0 0 0 0 0 0 0 0 0 j---v (downwards)
  //  2  3 1 0
  //  3  4 2 0
  //  1  6 3 0
  //  4  5 4 0
  for (ll i = 0; i <= n; i++) {
    for (ll j = 0; j <= W; j++) {
      if (i == 0 || j == 0)
        knap[i][j] = 0;
      else if (j - wt[i - 1] >= 0)
        knap[i][j] =
            max(knap[i - 1][j], knap[i - 1][j - wt[i - 1]] + val[i - 1]);
      else
        knap[i][j] = knap[i - 1][j];
    }
  }
  cout << knap[n][W];
}
Problem p03163 (C++)

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

int main() {
  long n, W;
  cin >> n >> W;
  long w[n], v[n];
  for (int i = 0; i < n; i++) {
    cin >> w[i] >> v[i];
  }
  long dp[n][W];
  for (int i = 0; i <= W; i++) {
    dp[0][i] = 0;
  }
  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 main() {
  long n, W;
  cin >> n >> W;
  long w[n], v[n];
  for (int i = 0; i < n; i++) {
    cin >> w[i] >> v[i];
  }
  long dp[n + 1][W + 1];
  for (int i = 0; i <= W; i++) {
    dp[0][i] = 0;
  }
  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>
using namespace std;

const int MAX_N = 105;
const int MAX_W = 1e3 + 5;

int n_test;
int n, W, w, v;

long long solve() {
  scanf("%d %d", &n, &W);
  long long res[MAX_N][MAX_W] = {{0L}};

  for (int i = 1; i <= n; ++i) {
    scanf("%d %d", &w, &v);
    for (int j = 0; j <= W; ++j)
      res[i][j] = res[i - 1][j];
    for (int j = w; j <= W; ++j) {
      res[i][j] = max(res[i][j], res[i - 1][j - w] + v);
    }
  }
  return res[n][W];
}

int main() {
  n_test = 1;
  // scanf("%d", &n_test);
  for (int i = 0; i < n_test; ++i)
    printf("%lld\n", solve());
}


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

const int MAX_N = 105;
const int MAX_W = 1e5 + 5;

int n_test;
int n, W, w, v;

long long solve() {
  scanf("%d %d", &n, &W);
  long long res[MAX_N][MAX_W] = {{0L}};

  for (int i = 1; i <= n; ++i) {
    scanf("%d %d", &w, &v);
    for (int j = 0; j <= W; ++j)
      res[i][j] = res[i - 1][j];
    for (int j = w; j <= W; ++j) {
      res[i][j] = max(res[i][j], res[i - 1][j - w] + v);
    }
  }
  return res[n][W];
}

int main() {
  n_test = 1;
  // scanf("%d", &n_test);
  for (int i = 0; i < n_test; ++i)
    printf("%lld\n", solve());
}
Problem p03163 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
long long memo[1001][100001];
long long knapsack(long long value[], long long weight[], int index, int n,
                   long long w) {
  if (index >= n || w <= 0)
    return 0;
  if (weight[index] > w)
    return knapsack(value, weight, index, n, w);
  if (memo[index][w] != -1)
    return memo[index][w];
  long long temp1;
  long long temp2;
  temp1 =
      value[index] + knapsack(value, weight, index + 1, n, w - weight[index]);
  temp2 = knapsack(value, weight, index + 1, n, w);
  memo[index][w] = max(temp1, temp2);
  return memo[index][w];
  // return
  // memo[index][w]=max(value[index]+knapsack(value,weight,index+1,n,w-weight[index]),knapsack(value,weight,index+1,n,w));
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  long long w;
  cin >> w;
  long long weight[n], value[n];
  for (int i = 0; i < n; i++)
    cin >> weight[i] >> value[i];
  for (long long i = 0; i <= n; i++) {
    for (long long j = 0; j <= w; j++)
      memo[i][j] = -1;
  }
  cout << knapsack(value, weight, 0, n, w) << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long memo[1001][100001];
long long knapsack(long long value[], long long weight[], int index, int n,
                   long long w) {
  if (index >= n || w <= 0)
    return 0;
  if (weight[index] > w) {
    if (memo[index][w] != -1)
      return memo[index][w];

    return memo[index][w] = knapsack(value, weight, index + 1, n, w);
  }
  if (memo[index][w] != -1)
    return memo[index][w];
  long long temp1;
  long long temp2;
  temp1 =
      value[index] + knapsack(value, weight, index + 1, n, w - weight[index]);
  temp2 = knapsack(value, weight, index + 1, n, w);
  memo[index][w] = max(temp1, temp2);
  return memo[index][w];
  // return
  // memo[index][w]=max(value[index]+knapsack(value,weight,index+1,n,w-weight[index]),knapsack(value,weight,index+1,n,w));
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  long long w;
  cin >> w;
  long long weight[n], value[n];
  for (int i = 0; i < n; i++)
    cin >> weight[i] >> value[i];
  for (long long i = 0; i <= n; i++) {
    for (long long j = 0; j <= w; j++)
      memo[i][j] = -1;
  }
  cout << knapsack(value, weight, 0, n, w) << endl;
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
//*******Abhijit Burman***********//
// Jalpaiguri Government Engineering College//

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

#define pb push_back
#define mk make_pair
#define MAXX (1000000000000000000 + 7)
#define fio                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)

ll dp[110][100100];

ll knapsack(ll a[], ll b[], ll wt, ll index) {
  // cout<<wt<<endl;
  if (index < 0) {
    return 0;
  }
  if (dp[wt][index] != -1) {
    return dp[wt][index];
  } else {
    if (wt - a[index] < 0) {
      dp[wt][index] = knapsack(a, b, wt, index - 1);
    } else
      dp[wt][index] = max(b[index] + knapsack(a, b, wt - a[index], index - 1),
                          knapsack(a, b, wt, index - 1));
    return dp[wt][index];
  }
}
void solve(ll t) {
  ll n, w;
  cin >> n >> w;
  ll a[n], b[n];
  memset(dp, -1, sizeof(dp));
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
  }
  ll ans = knapsack(a, b, w, n - 1);

  cout << ans << endl;
}

int main() {
  ll t = 1;
  // cin>>t;
  while (t--) {
    solve(t);
  }
  return 0;
}


Fixed submission:
//*******Abhijit Burman***********//
// Jalpaiguri Government Engineering College//

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

#define pb push_back
#define mk make_pair
#define MAXX (1000000000000000000 + 7)
#define fio                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)

ll dp[100100][110];

ll knapsack(ll a[], ll b[], ll wt, ll index) {
  // cout<<wt<<endl;
  if (index < 0) {
    return 0;
  }
  if (dp[wt][index] != -1) {
    return dp[wt][index];
  } else {
    if (wt - a[index] < 0) {
      dp[wt][index] = knapsack(a, b, wt, index - 1);
    } else
      dp[wt][index] = max(b[index] + knapsack(a, b, wt - a[index], index - 1),
                          knapsack(a, b, wt, index - 1));
    return dp[wt][index];
  }
}
void solve(ll t) {
  ll n, w;
  cin >> n >> w;
  ll a[n], b[n];
  memset(dp, -1, sizeof(dp));
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
  }
  ll ans = knapsack(a, b, w, n - 1);

  cout << ans << endl;
}

int main() {
  ll t = 1;
  // cin>>t;
  while (t--) {
    solve(t);
  }
  return 0;
}
Problem p03163 (C++)

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

ll fun(int n, int W, ll **dp) {
  if (W == 0 || n < 0)
    return 0;

  if (W - wt[n] >= 0) {
    dp[n][W] = max(dp[n][W], fun(n - 1, W - wt[n], dp) + v[n]);
  }

  dp[n][W] = max(dp[n][W], fun(n - 1, W, dp));
  return dp[n][W];
}
int main() {
  int n, W;
  cin >> n >> W;

  for (int i = 0; i < n; i++) {
    cin >> wt[i] >> v[i];
  }
  ll **dp = new ll *[n];
  for (int i = 0; i < n; i++)
    dp[i] = new ll[W + 1];
  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j < W + 1; j++) {
      dp[i][j] = -1;
    }
  }

  cout << fun(n - 1, W, dp);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
ll wt[101], v[101];

ll fun(int n, int W, ll **dp) {
  if (W == 0 || n < 0)
    return 0;
  if (dp[n][W] != -1)
    return dp[n][W];
  if (W - wt[n] >= 0) {
    dp[n][W] = max(dp[n][W], fun(n - 1, W - wt[n], dp) + v[n]);
  }

  dp[n][W] = max(dp[n][W], fun(n - 1, W, dp));
  return dp[n][W];
}
int main() {
  int n, W;
  cin >> n >> W;

  for (int i = 0; i < n; i++) {
    cin >> wt[i] >> v[i];
  }
  ll **dp = new ll *[n];
  for (int i = 0; i < n; i++)
    dp[i] = new ll[W + 1];
  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j < W + 1; j++) {
      dp[i][j] = -1;
    }
  }

  cout << fun(n - 1, W, dp);
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long dp[100][100000];

int main() {
  long long n, W, i, x, y, tw, j;
  cin >> n >> W;
  vector<long long> a, w;
  for (i = 1; i <= n; i++) {
    cin >> y >> x;
    a.push_back(x);
    w.push_back(y);
  }
  for (i = 0; i <= n; i++) {
    for (j = 0; j <= W; j++) {
      if (i == 0 || j == 0)
        dp[i][j] = 0;
      else if (j >= w[i - 1])
        dp[i][j] = max(dp[i - 1][j], a[i - 1] + dp[i - 1][j - w[i - 1]]);
      else
        dp[i][j] = dp[i - 1][j];
      // cout<<dp[i][j]<<" ";
    }
    // cout<<endl;
  }
  cout << dp[n][W] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long dp[101][100001];

int main() {
  long long n, W, i, x, y, tw, j;
  cin >> n >> W;
  vector<long long> a, w;
  for (i = 1; i <= n; i++) {
    cin >> y >> x;
    a.push_back(x);
    w.push_back(y);
  }
  for (i = 0; i <= n; i++) {
    for (j = 0; j <= W; j++) {
      if (i == 0 || j == 0)
        dp[i][j] = 0;
      else if (j >= w[i - 1])
        dp[i][j] = max(dp[i - 1][j], a[i - 1] + dp[i - 1][j - w[i - 1]]);
      else
        dp[i][j] = dp[i - 1][j];
      // cout<<dp[i][j]<<" ";
    }
    // cout<<endl;
  }
  cout << dp[n][W] << endl;
  return 0;
}
Problem p03163 (C++)

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

using namespace std;
typedef long long ll;
#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)

const int MAX = 1e5 + 10;

ll N, W, w, v;
ll dp[111];
ll ans;

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

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

using namespace std;
typedef long long ll;
#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)

const int MAX = 1e5 + 10;

ll N, W, w, v;
ll dp[MAX];
ll ans;

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

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

using namespace std;
const int MAXN = 1e5 + 5;

long long DP[MAXN];

int main(int argc, char *argv[]) {
  int N, W, w, v;
  long long answer = 0;
  scanf("%d%d", &N, &W);

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

    for (int j = W; 0 <= j; j--)
      if ((DP[j] or j == 0) and j + w <= W) {
        DP[j + w] = max(DP[j + w], DP[j] + v);
        answer = max(answer, DP[j + w]);
      }
  }

  printf("%lld\n", answer);
  return 0;
}

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

using namespace std;
const int MAXN = 1e5 + 5;

long long DP[MAXN];

int main(int argc, char *argv[]) {
  int N, W, w, v;
  long long answer = 0;
  scanf("%d%d", &N, &W);

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

    for (int j = W; 0 <= j; j--)
      if ((DP[j] or j == 0) and j + w <= W) {
        DP[j + w] = max(DP[j + w], DP[j] + v);
        answer = max(answer, DP[j + w]);
      }
  }

  printf("%lld\n", answer);
  return 0;
}
Problem p03163 (C++)

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

using namespace std;

using ll = long long;

int main() {

  ll n, w;
  cin >> n >> w;
  vector<ll> weight(n), value(n);
  for (ll i = 1; i <= n; ++i) {
    cin >> weight[i] >> value[i];
  }
  ll arr[n + 1][w + 1];
  for (ll i = 0; i < n + 1; ++i) {
    for (ll j = 0; j < w + 1; ++j)
      arr[i][j] = 0;
  }
  for (ll i = 1; i <= n; ++i) {
    for (ll j = 1; j <= w; ++j) {
      if (j < weight[i]) {
        arr[i][j] = arr[i - 1][j];
      } else
        arr[i][j] = max(value[i] + arr[i - 1][j - weight[i]], arr[i - 1][j]);
    }
  }

  cout << *max_element(arr[n], arr[n] + w + 1);
  return 0;
}


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

using namespace std;

using ll = long long;

int main() {

  ll n, w;
  cin >> n >> w;
  vector<ll> weight(101), value(101);
  for (ll i = 1; i <= n; ++i) {
    cin >> weight[i] >> value[i];
  }
  ll arr[n + 1][w + 1];
  for (ll i = 0; i < n + 1; ++i) {
    for (ll j = 0; j < w + 1; ++j)
      arr[i][j] = 0;
  }
  for (ll i = 1; i <= n; ++i) {
    for (ll j = 1; j <= w; ++j) {
      if (j < weight[i]) {
        arr[i][j] = arr[i - 1][j];
      } else
        arr[i][j] = max(value[i] + arr[i - 1][j - weight[i]], arr[i - 1][j]);
    }
  }

  cout << *max_element(arr[n], arr[n] + w + 1);
  return 0;
}
Problem p03163 (C++)

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

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef set<int> si;
typedef map<int, int> mii;
typedef map<ll, ll> mll;
typedef map<string, int> msi;
typedef set<ll> sl;
typedef set<char> sc;
typedef pair<int, int> pii;

#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
const int INF = 1 << 29;
#define MOD (ll)(1e9 + 7)
#define eps 1e-6
#define pb push_back
#define mp make_pair
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define F first
#define S second
#define all(s) s.begin, s.end()

int main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  freopen("input.txt", "r", stdin);

  int n, W;
  cin >> n >> W;
  int w[n], v[n];
  REP(i, n) cin >> w[i] >> v[i];
  long long dp[n + 1][W + 1];
  REP(i, n + 1) REP(j, W + 1) dp[i][j] = 0;
  REP(i, n) {
    REP(j, W + 1) {
      if (j >= w[i])
        dp[i + 1][j] = max(dp[i][j], v[i] + dp[i][j - w[i]]);
      else
        dp[i + 1][j] = dp[i][j];
    }
  }
  /*
  REP(i,n+1) {
          REP(j,W+1) {
                  cout << dp[i][j] << " " ;
          }
          cout << endl;
  }
  */
  cout << dp[n][W] << endl;
  return 0;
}


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

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef set<int> si;
typedef map<int, int> mii;
typedef map<ll, ll> mll;
typedef map<string, int> msi;
typedef set<ll> sl;
typedef set<char> sc;
typedef pair<int, int> pii;

#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
const int INF = 1 << 29;
#define MOD (ll)(1e9 + 7)
#define eps 1e-6
#define pb push_back
#define mp make_pair
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define F first
#define S second
#define all(s) s.begin, s.end()

int main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  // freopen("input.txt","r",stdin);

  int n, W;
  cin >> n >> W;
  int w[n], v[n];
  REP(i, n) cin >> w[i] >> v[i];
  long long dp[n + 1][W + 1];
  REP(i, n + 1) REP(j, W + 1) dp[i][j] = 0;
  REP(i, n) {
    REP(j, W + 1) {
      if (j >= w[i])
        dp[i + 1][j] = max(dp[i][j], v[i] + dp[i][j - w[i]]);
      else
        dp[i + 1][j] = dp[i][j];
    }
  }
  /*
  REP(i,n+1) {
          REP(j,W+1) {
                  cout << dp[i][j] << " " ;
          }
          cout << endl;
  }
  */
  cout << dp[n][W] << endl;
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define debug(x) cerr << #x << " = " << x << endl
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;

const int maxN = 1e3 + 5;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;

ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }
ll modP(ll a, ll b) {
  return (!b ? 1 : (modP(a, b / 2) * modP(a, b / 2) * (b % 2 ? a : 1)) % MOD);
}

ll n, m, W, dp[maxN], w[maxN], v[maxN];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  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 = W; j > -1; j--) {
      if (j == 0 || j < w[i])
        continue;
      dp[j] = max(dp[j], v[i] + dp[j - w[i]]);
    }
  }
  cout << dp[W];
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define debug(x) cerr << #x << " = " << x << endl
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;

const int maxN = 1e3 + 5;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;

ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }
ll modP(ll a, ll b) {
  return (!b ? 1 : (modP(a, b / 2) * modP(a, b / 2) * (b % 2 ? a : 1)) % MOD);
}

ll n, m, W, dp[maxN * maxN], w[maxN], v[maxN];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  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 = W; j > -1; j--) {
      if (j == 0 || j < w[i])
        continue;
      dp[j] = max(dp[j], v[i] + dp[j - w[i]]);
    }
  }
  cout << dp[W];
  return 0;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF 1000000007
using namespace std;
using P = pair<int, int>;
using ll = long long;
int main() {
  int n, m;
  cin >> n >> m;
  vector<ll> w(n);
  vector<ll> v(n);
  for (int i = 0; i < n; i++)
    cin >> w[i] >> v[i];
  vector<vector<ll>> dp(n + 1, vector<ll>(m, 0));
  for (int i = n - 1; i >= 0; i--) {
    for (int j = 0; j <= m; j++) {
      if (w[i] > j)
        dp[i][j] = dp[i + 1][j];
      else
        dp[i][j] = max(dp[i + 1][j], dp[i + 1][j - w[i]] + v[i]);
    }
  }
  cout << dp[0][m] << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF 1000000007
using namespace std;
using P = pair<int, int>;
using ll = long long;
int main() {
  int n, m;
  cin >> n >> m;
  vector<ll> w(n);
  vector<ll> v(n);
  for (int i = 0; i < n; i++)
    cin >> w[i] >> v[i];
  vector<vector<ll>> dp(n + 1, vector<ll>(m + 1, 0));
  for (int i = n - 1; i >= 0; i--) {
    for (int j = 0; j <= m; j++) {
      if (w[i] > j)
        dp[i][j] = dp[i + 1][j];
      else
        dp[i][j] = max(dp[i + 1][j], dp[i + 1][j - w[i]] + v[i]);
    }
  }
  cout << dp[0][m] << endl;
  return 0;
}
Problem p03163 (C++)

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

using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define MOD 1000000007

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef long double ld;

long long INF = LLONG_MAX;

int n, W;
pll w[101];
ll v[100111];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
  cin >> n >> W;
  memset(v, 0, sizeof(v));
  for (int i = 0; i < n; i++) {
    cin >> w[i].fi >> w[i].se;
  }
  sort(w, w + n);
  for (auto &cur : w) {
    ll cw = cur.fi;
    ll cv = cur.se;
    for (int i = W; i > 0; i--) {
      if (v[i] > 0)
        v[i + cw] = max(v[i + cw], cv + v[i]);
    }
    v[cw] = max(v[cw], cv);
  }
  /*
  for(int i=0; i<=W; i++){
          cout << i << ' ' << v[i] << endl;
  }
  */
  ll ans = 0;
  for (int i = 0; i <= W; i++)
    ans = max(ans, v[i]);
  cout << ans << endl;
}


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

using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define MOD 1000000007

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef long double ld;

long long INF = LLONG_MAX;

int n, W;
pll w[101];
ll v[100111];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
  cin >> n >> W;
  memset(v, 0, sizeof(v));
  for (int i = 0; i < n; i++) {
    cin >> w[i].fi >> w[i].se;
  }
  sort(w, w + n);
  for (auto &cur : w) {
    ll cw = cur.fi;
    ll cv = cur.se;
    for (int i = W; i > 0; i--) {
      if (v[i] > 0 && i + cw <= W)
        v[i + cw] = max(v[i + cw], cv + v[i]);
    }
    v[cw] = max(v[cw], cv);
  }
  /*
  for(int i=0; i<=W; i++){
          cout << i << ' ' << v[i] << endl;
  }
  */
  ll ans = 0;
  for (int i = 0; i <= W; i++)
    ans = max(ans, v[i]);
  cout << ans << endl;
}
Problem p03163 (C++)

Buggy submission (status: Runtime Error):
/**
 * Title: D - Knapsack 1
 * Url: https://atcoder.jp/contests/dp/tasks/dp_d
 */
#include <bits/stdc++.h>

using namespace std;

#define ll long long

int N, W;
const int MAX_N = 100;
const int MAX_W = 100000;

int w[MAX_N + 1], v[MAX_N + 1];
ll dp[MAX_N + 1][MAX_W + 1];

ll rec(int i, int j) {
  if (dp[i][j] != -1)
    return dp[i][j];
  if (i == N + 1)
    return dp[i][j] = 0;
  if (j < w[i])
    return dp[i][j] = rec(i + 1, j);
  return dp[i][j] = max(rec(i + 1, j), rec(i + 1, j - w[i]) + v[i]);
}

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

Fixed submission:
/**
 * Title: D - Knapsack 1
 * Url: https://atcoder.jp/contests/dp/tasks/dp_d
 */
#include <bits/stdc++.h>

using namespace std;

#define ll long long

int N, W;
const int MAX_N = 100;
const int MAX_W = 100000;

int w[MAX_N + 1], v[MAX_N + 1];
ll dp[MAX_N + 2][MAX_W + 2];

ll rec(int i, int j) {
  if (dp[i][j] != -1)
    return dp[i][j];
  if (i == N + 1)
    return dp[i][j] = 0;
  if (j < w[i])
    return dp[i][j] = rec(i + 1, j);
  return dp[i][j] = max(rec(i + 1, j), rec(i + 1, j - w[i]) + v[i]);
}

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

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

const int maxn = 105;
const int maxw = 10005;

int N, W, w[maxn], v[maxn];
long long dp[maxn][maxw];

int main() {
#ifdef LOCAL
  freopen("./input.txt", "r", stdin);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  cin >> N >> W;

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

  for (int i = 0; i <= W; i++) {
    if (i < w[1])
      dp[1][i] = 0;
    else
      dp[1][i] = v[1];
  }

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

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


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

const int maxn = 105;
const int maxw = 100005;

int N, W, w[maxn], v[maxn];
long long dp[maxn][maxw];

int main() {
#ifdef LOCAL
  freopen("./input.txt", "r", stdin);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  cin >> N >> W;

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

  for (int i = 0; i <= W; i++) {
    if (i < w[1])
      dp[1][i] = 0;
    else
      dp[1][i] = v[1];
  }

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

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define endl "\n"
#define F first
#define S second
#define mod 1000000007
#define pb push_back
#define FOR(i, a, n) for (int i = a; i < n; i++)
#define REV(i, a, n) for (int i = a; i >= n; i--)
#define all(a) a.begin(), a.end()
#define UB upper_bound
#define LB lower_bound
const int NUM = 2e5 + 5;
int dp[100][10001];
int w[100], v[100];
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int num_tests = 1;
  // cin >> num_tests;
  while (num_tests-- > 0) {
    int n, W;
    cin >> n >> W;
    FOR(i, 0, n) { cin >> w[i] >> v[i]; }
    dp[0][0] = 0;
    dp[0][w[0]] = v[0];
    for (int i = 1; i < n; i++) {
      dp[i][0] = 0; // bag is empty
      for (int j = 1; j <= W; j++) {
        // discarding an object
        dp[i][j] = dp[i - 1][j];
        // picking element
        if (j >= w[i])
          dp[i][j] = max(dp[i][j], v[i] + dp[i - 1][j - w[i]]);
      }
    }
    int ans = -1e18;
    FOR(i, 0, W + 1)
    ans = max(ans, dp[n - 1][i]);
    cout << ans;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define endl "\n"
#define F first
#define S second
#define mod 1000000007
#define pb push_back
#define FOR(i, a, n) for (int i = a; i < n; i++)
#define REV(i, a, n) for (int i = a; i >= n; i--)
#define all(a) a.begin(), a.end()
#define UB upper_bound
#define LB lower_bound
const int NUM = 2e5 + 5;
int dp[100][100001];
int w[100], v[100];
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int num_tests = 1;
  // cin >> num_tests;
  while (num_tests-- > 0) {
    int n, W;
    cin >> n >> W;
    FOR(i, 0, n) { cin >> w[i] >> v[i]; }
    dp[0][0] = 0;
    dp[0][w[0]] = v[0];
    for (int i = 1; i < n; i++) {
      dp[i][0] = 0; // bag is empty
      for (int j = 1; j <= W; j++) {
        // discarding an object
        dp[i][j] = dp[i - 1][j];
        // picking element
        if (j >= w[i])
          dp[i][j] = max(dp[i][j], v[i] + dp[i - 1][j - w[i]]);
      }
    }
    int ans = -1e18;
    FOR(i, 0, W + 1)
    ans = max(ans, dp[n - 1][i]);
    cout << ans;
  }
}
Problem p03163 (C++)

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

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

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

signed main() {
  int N, W;
  cin >> N >> W;
  vector<vector<int>> dp(110, vector<int>(100010, 0));
  int v, w;
  for (int i = 0; i < N; i++) {
    cin >> w >> v;
    for (int j = 0; j <= W; j++) {
      if (j - w < 0) {
        dp[i + 1][j] = dp[i][j];
      } else {
        dp[i + 1][j] = max(dp[i][j - w] + v, dp[i][j]);
      }
    }
  }
  cout << dp[N][W] << endl;
}
Problem p03163 (C++)

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

#define ll long long

vector<int> V;
ll n, W, w, v;
ll dp[100][100010];

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


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

#define ll long long

vector<int> V;
ll n, W, w, v;
ll dp[110][100010];

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