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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1e3 + 10, M = 1e2 + 10;
const int mod = 1e9 + 7;
const int inf = 1e9;
int w[M], v[M];
int dp[M][N];

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

  int ans = 0;
  for (int i = 1; i <= val; i++) {
    if (dp[n][i] <= W)
      ans = i;
  }

  cout << ans << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1e5 + 10, M = 1e2 + 10;
const int mod = 1e9 + 7;
const int inf = 1e9;
int w[M], v[M];
int dp[M][N];

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

  int ans = 0;
  for (int i = 1; i <= val; i++) {
    if (dp[n][i] <= W)
      ans = i;
  }

  cout << ans << '\n';
  return 0;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define DIM 100007
#define INF 1000000000000000007LL
using namespace std;

long long n, w[107], v[107], W, d[107][DIM], res;

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

  for (int i = 1; i < DIM; ++i)
    d[0][i] = INF;

  for (int i = 1; i <= n; ++i)
    for (int j = 1; j < DIM; ++j) {
      d[i][j] = min(d[i - 1][j], d[i - 1][j - v[i]] + w[i]);
      if (d[i][j] <= W)
        res = max(res, (long long)j);
    }
  cout << res;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define DIM 100007
#define INF 1000000000000000007LL
using namespace std;

long long n, w[107], v[107], W, d[107][DIM], res;

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

  for (int i = 1; i < DIM; ++i)
    d[0][i] = INF;

  for (int i = 1; i <= n; ++i)
    for (int j = 1; j < DIM; ++j) {
      d[i][j] = d[i - 1][j];
      if (j >= v[i])
        d[i][j] = min(d[i][j], d[i - 1][j - v[i]] + w[i]);
      if (d[i][j] <= W)
        res = max(res, (long long)j);
    }
  cout << res;
  return 0;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MX = 100000;
const ll INF = 1e18 + 7107;
int n, w[100005], v[100005], ans;
ll f[100005], T;
int main() {
  scanf("%d%lld", &n, &T);
  for (int i = 1; i <= n; i++)
    scanf("%d%d", &w[i], &v[i]);
  for (int i = 1; i <= MX; i++)
    f[i] = INF;
  f[0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = MX; j >= 1; j--) {
      if (f[j] >= v[i])
        f[j] = min(f[j], f[j - v[i]] + w[i]);
    }
  }
  for (int i = 1; i <= MX; i++)
    if (f[i] <= T)
      ans = max(ans, i);
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MX = 100000;
const ll INF = 1e18 + 7107;
int n, w[100005], v[100005], ans;
ll f[100005], T;
int main() {
  scanf("%d%lld", &n, &T);
  for (int i = 1; i <= n; i++)
    scanf("%d%d", &w[i], &v[i]);
  for (int i = 1; i <= MX; i++)
    f[i] = INF;
  f[0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = MX; j >= 1; j--) {
      if (j >= v[i])
        f[j] = min(f[j], f[j - v[i]] + w[i]);
    }
  }
  for (int i = 1; i <= MX; i++)
    if (f[i] <= T)
      ans = max(ans, i);
  printf("%d\n", ans);
  return 0;
}
Problem p03164 (C++)

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

using namespace std;

typedef long long ll;

#ifdef RED
mt19937 rnd(228);
#else
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
#endif

const int N = (int)1e6 + 5;
const ll inf = (ll)1e18;

int n, W;
ll w[N], v[N];
ll dp[N];

int main() {
#ifdef RED
  freopen("a.in", "r", stdin);
#endif
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> W;
  int sum = 0;
  for (int i = 1; i <= n; ++i) {
    cin >> w[i] >> v[i];
    sum += v[i];
  }
  for (int i = 0; i <= sum; ++i) {
    dp[i] = inf;
  }
  dp[0] = 0;
  for (int i = 1; i <= n; ++i) {
    vector<ll> mn(sum);
    for (int j = v[i]; j <= sum; ++j) {
      mn[j] = min(dp[j], dp[j - v[i]] + w[i]);
    }
    for (int j = v[i]; j <= sum; ++j) {
      dp[j] = mn[j];
    }
  }
  for (int i = sum; i >= 0; i--) {
    if (dp[i] <= W) {
      cout << i;
      return 0;
    }
  }
  return 0;
}


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

using namespace std;

typedef long long ll;

#ifdef RED
mt19937 rnd(228);
#else
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
#endif

const int N = (int)1e6 + 5;
const ll inf = (ll)1e18;

int n, W;
ll w[N], v[N];
ll dp[N];

int main() {
#ifdef RED
  freopen("a.in", "r", stdin);
#endif
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> W;
  int sum = 0;
  for (int i = 1; i <= n; ++i) {
    cin >> w[i] >> v[i];
    sum += v[i];
  }
  for (int i = 0; i <= sum; ++i) {
    dp[i] = inf;
  }
  dp[0] = 0;
  for (int i = 1; i <= n; ++i) {
    vector<ll> mn(sum + 10);
    for (int j = v[i]; j <= sum; ++j) {
      mn[j] = min(dp[j], dp[j - v[i]] + w[i]);
    }
    for (int j = v[i]; j <= sum; ++j) {
      dp[j] = mn[j];
    }
  }
  for (int i = sum; i >= 0; i--) {
    if (dp[i] <= W) {
      cout << i;
      return 0;
    }
  }
  return 0;
}
Problem p03164 (C++)

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

using namespace std;

typedef long long ll;

signed main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> a(n);
  vector<ll> b(n);
  for (ll i = 0; i < n; ++i)
    cin >> a[i] >> b[i];
  vector<vector<ll>> dp(n + 1, vector<ll>(1000 * n + 1, 1e12));
  dp[0][0] = 0;
  for (ll i = 0; i < n; ++i) {
    for (ll j = 0; j <= 1000 * n; ++j) {
      if (dp[i][j] == 1e9)
        continue;
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
      dp[i + 1][j + b[i]] = min(dp[i + 1][j + b[i]], dp[i][j] + a[i]);
    }
  }
  for (ll i = 1000 * n; i >= 0; --i) {
    if (dp[n][i] <= m) {
      cout << i << endl;
      return 0;
    }
  }
}


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

using namespace std;

typedef long long ll;

signed main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> a(n);
  vector<ll> b(n);
  for (ll i = 0; i < n; ++i)
    cin >> a[i] >> b[i];
  vector<vector<ll>> dp(n + 1, vector<ll>(1000 * n + 1, 1e12));
  dp[0][0] = 0;
  for (ll i = 0; i < n; ++i) {
    for (ll j = 0; j <= 1000 * n; ++j) {
      if (dp[i][j] == 1e12)
        continue;
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
      dp[i + 1][j + b[i]] = min(dp[i + 1][j + b[i]], dp[i][j] + a[i]);
    }
  }
  for (ll i = 1000 * n; i >= 0; --i) {
    if (dp[n][i] <= m) {
      cout << i << endl;
      return 0;
    }
  }
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define IFOR(i, m, n) for (int i = n - 1; i >= m; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define FOREACH(x, a) for (auto &(x) : (a))
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
const ll INF = 1LL << 60;
const int MOD = 1000000007;
/* テンプレートここまで */

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

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

  vector<int> v(N + 1, 0), w(N + 1, 0);

  FOR(i, 1, N + 1) { cin >> w[i] >> v[i]; }

  int V_MAX = 1000;
  vector<vector<ll>> dp(N + 1, vector<ll>(N * V_MAX + 1, W + 1));

  FOR(i, 0, N + 1) { dp[i][0] = 0; }

  FOR(i, 0, N) {
    REP(j, N * V_MAX) {
      chmin(dp[i + 1][j + v[i + 1]], dp[i][j] + w[i + 1]);
      chmin(dp[i + 1][j], dp[i][j]);
    }
  }

  int ans = 0;
  REP(i, N * V_MAX + 1) {
    if (dp[N][i] <= W)
      chmax(ans, i);
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define IFOR(i, m, n) for (int i = n - 1; i >= m; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define FOREACH(x, a) for (auto &(x) : (a))
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
const ll INF = 1LL << 60;
const int MOD = 1000000007;
/* テンプレートここまで */

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

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

  vector<int> v(N + 1, 0), w(N + 1, 0);

  FOR(i, 1, N + 1) { cin >> w[i] >> v[i]; }

  int V_MAX = 1000;
  vector<vector<ll>> dp(N + 1, vector<ll>(N * V_MAX + 1, W + 1));

  FOR(i, 0, N + 1) { dp[i][0] = 0; }

  FOR(i, 0, N) {
    REP(j, N * V_MAX) {
      if (dp[i][j] <= W)
        chmin(dp[i + 1][j + v[i + 1]], dp[i][j] + w[i + 1]);
      chmin(dp[i + 1][j], dp[i][j]);
    }
  }

  int ans = 0;
  REP(i, N * V_MAX + 1) {
    if (dp[N][i] <= W)
      chmax(ans, i);
  }
  cout << ans << endl;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
// reference ---->  errichto stream
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 1e18;
int main(int argc, char **argv) {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "rt", stdin);
  freopen("output.txt", "wt", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, w;
  cin >> n >> w;
  vector<int> val(n), wei(n);
  int sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> wei[i] >> val[i];
    sum += (val[i]);
  }
  vector<ll> dp(sum + 1, inf); // min weight with value sum + 1
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = sum - val[i]; j >= 0; j--) {
      dp[j + val[i]] = min(dp[j + val[i]], dp[j] + wei[i]);
    }
  }
  ll ans = 0;
  for (ll i = 0; i <= sum; i++) {
    if (dp[i] <= w) {
      ans = i;
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
// reference ---->  errichto stream
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 1e18;
int main(int argc, char **argv) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, w;
  cin >> n >> w;
  vector<int> val(n), wei(n);
  int sum = 0;
  for (int i = 0; i < n; i++) {
    cin >> wei[i] >> val[i];
    sum += (val[i]);
  }
  vector<ll> dp(sum + 1, inf); // min weight with value sum + 1
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = sum - val[i]; j >= 0; j--) {
      dp[j + val[i]] = min(dp[j + val[i]], dp[j] + wei[i]);
    }
  }
  ll ans = 0;
  for (ll i = 0; i <= sum; i++) {
    if (dp[i] <= w) {
      ans = i;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
// dp[i][v] = tuye i taye aval ye tedadi ro var darim ke majume arzeshashun
// beshe v, hade aghal vazneshun che ghadre ! dp[0][v[0]] = w[0] dp[0][0] = 0
// else dp[0][i] = oo

#include <bits/stdc++.h>

using namespace std;

const int N = 110;
const int V = 1e3 + 10;

long long n, W, v[N], w[N], dp[N][N * V];

int main() {
  ios::sync_with_stdio(0);

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

  memset(dp, 63, sizeof(dp));
  dp[0][0] = 0;
  dp[0][v[0]] = w[0];

  for (int i = 1; i < n; i++) {
    for (int j = 0; j < N * V; j++) {
      dp[i][j] = dp[i - 1][j];
      if (j - v[i] >= 0)
        dp[i][j] = min(dp[i - 1][j - v[i]] + w[i], dp[i - 1][j]);
    }
  }

  for (int i = N * V - 1; ~i; i--)
    if (dp[n - 1][i] <= W) {
      cout << i << endl;
      return 0;
    }

  return 0;
}


Fixed submission:
// dp[i][v] = tuye i taye aval ye tedadi ro var darim ke majume arzeshashun
// beshe v, hade aghal vazneshun che ghadre ! dp[0][v[0]] = w[0] dp[0][0] = 0
// else dp[0][i] = oo

#include <bits/stdc++.h>

using namespace std;

const int N = 110;
const int V = 1e3 + 10;

long long n, W, v[N], w[N], dp[N][N * V];

int main() {
  ios::sync_with_stdio(0);

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

  memset(dp, 63, sizeof(dp));
  dp[0][0] = 0;
  dp[0][v[0]] = w[0];

  for (int i = 1; i < n; i++) {
    for (int j = 0; j < N * V; j++) {
      dp[i][j] = dp[i - 1][j];
      if (j - v[i] >= 0)
        dp[i][j] = min(dp[i - 1][j - v[i]] + w[i], dp[i - 1][j]);
    }
  }

  for (int i = N * V - 1; ~i; i--)
    if (dp[n - 1][i] <= W) {
      cout << i << endl;
      return 0;
    }

  return 0;
}
Problem p03164 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define ll long long
#define li long
#define pb push_back
#define mem(arr, x) memset(arr, x, sizeof(arr))

ll n, W;
ll w[101], v[101];
ll dp[101][100010];
ll totalV = 0;

ll minWeight(ll end, ll Val) {
  if (Val <= 0)
    return 0;
  if (end == 0)
    return 1000000010;
  if (dp[end][Val] != 1000000010)
    return dp[end][Val];
  // if(v[end-1]>Val)
  // return dp[end][Val]=minWeight(end-1,Val);
  // else
  return dp[end][Val] = min(w[end - 1] + minWeight(end - 1, Val - v[end - 1]),
                            minWeight(end - 1, Val));
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll i, j;
  cin >> n >> W;
  for (i = 0; i < n; i++) {
    cin >> w[i] >> v[i];
    totalV += v[i];
  }

  for (i = 0; i <= n; i++)
    for (j = 0; j <= totalV; j++)
      dp[i][j] = 1000000010;

  for (i = totalV; i >= 1; i--) {
    if (minWeight(n, i) <= W)
      break;
  }
  cout << i;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define ll long long
#define li long
#define pb push_back
#define mem(arr, x) memset(arr, x, sizeof(arr))

ll n, W;
ll w[101], v[101];
ll dp[101][100010];
ll totalV = 0;

ll minWeight(ll end, ll Val) {
  if (Val <= 0)
    return 0;
  if (end == 0)
    return INT_MAX;
  if (dp[end][Val] != 1000000010)
    return dp[end][Val];
  // if(v[end-1]>Val)
  // return dp[end][Val]=minWeight(end-1,Val);
  // else
  return dp[end][Val] = min(w[end - 1] + minWeight(end - 1, Val - v[end - 1]),
                            minWeight(end - 1, Val));
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll i, j;
  cin >> n >> W;
  for (i = 0; i < n; i++) {
    cin >> w[i] >> v[i];
    totalV += v[i];
  }

  for (i = 0; i <= n; i++)
    for (j = 0; j <= totalV; j++)
      dp[i][j] = 1000000010;

  for (i = totalV; i >= 1; i--) {
    if (minWeight(n, i) <= W)
      break;
  }
  cout << i;
  return 0;
}
Problem p03164 (C++)

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

using namespace std;

using ll = long long;
const ll INF = 1e18 + 5;

int main(int argc, char **argv) {
  ll N = 0, W = 0;

  cin >> N >> W;
  vector<ll> weights(N);
  vector<ll> values(N);

  for (int i = 0; i < N; ++i) {
    cin >> weights[i] >> values[i];
  }
  vector<ll> dp(1000000, INF);

  dp[0] = 0;
  for (int item = 0; item < N; ++item) {
    for (ll value = dp.size() - 1; value >= 0; --value) {
      dp[value + values[item]] =
          min(dp[value] + weights[item], dp[value + values[item]]);
    }
  }
  ll m = 0;
  for (ll i = 0; i < dp.size(); ++i) {
    if (dp[i] <= W && i > m) {
      m = i;
    }
  }
  cout << m << "\n";
  return 0;
}

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

using namespace std;

using ll = long long;
const ll INF = 1e18 + 5;

int main(int argc, char **argv) {
  ll N = 0, W = 0;

  cin >> N >> W;
  vector<ll> weights(N);
  vector<ll> values(N);

  for (int i = 0; i < N; ++i) {
    cin >> weights[i] >> values[i];
  }
  vector<ll> dp(1000000, INF);

  dp[0] = 0;
  for (int item = 0; item < N; ++item) {
    for (ll value = dp.size() - 1; value >= 0; --value) {
      if (value + values[item] < dp.size()) {
        dp[value + values[item]] =
            min(dp[value] + weights[item], dp[value + values[item]]);
      }
    }
  }
  ll m = 0;
  for (ll i = 0; i < dp.size(); ++i) {
    if (dp[i] <= W && i > m) {
      m = i;
    }
  }
  cout << m << "\n";
  return 0;
}
Problem p03164 (C++)

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

#define MAXN 120
#define MAXV 100020
#define INF 110000000001

long long N, W;
long long w[MAXN], v[MAXN];

long long dp[MAXN][MAXV];

int main() {
  cin >> N >> W;
  for (long long i = 0; i < N; i++) {
    cin >> w[i] >> v[i];
  }
  for (long long i = 0; i < N; i++) {
    for (long long j = 0; j < MAXV; j++) {
      dp[i][j] = INF;
    }
  }
  dp[0][0] = 0;
  dp[0][v[0]] = w[0];
  for (long long i = 0; i < N; i++) {
    for (long long j = 0; j < MAXV; j++) {
      if (v[i] > j) {
        if (dp[i - 1][j] == INF) {
        } else {
          dp[i][j] = dp[i - 1][j];
        }
      } else {
        if (dp[i - 1][j] != INF && dp[i - 1][j - v[i]] != INF) {
          dp[i][j] = min(w[i] + dp[i - 1][j - v[i]], dp[i - 1][j]);
        } else if (dp[i - 1][j] != INF) {
          dp[i][j] = dp[i - 1][j];
        } else if (dp[i - 1][j - v[i]] != INF) {
          dp[i][j] = dp[i - 1][j - v[i]] + w[i];
        }
      }
    }
  }
  long long ans = 0;
  for (long long i = 1; i < MAXV; i++) {
    if (dp[N - 1][i] <= W)
      ans = i;
  }
  cout << ans << endl;
}

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

#define MAXN 120
#define MAXV 100020
#define INF 110000000001

long long N, W;
long long w[MAXN], v[MAXN];

long long dp[MAXN][MAXV];

int main() {
  cin >> N >> W;
  for (long long i = 0; i < N; i++) {
    cin >> w[i] >> v[i];
  }
  for (long long i = 0; i < N; i++) {
    for (long long j = 0; j < MAXV; j++) {
      dp[i][j] = INF;
    }
  }
  dp[0][0] = 0;
  dp[0][v[0]] = w[0];
  for (long long i = 1; i < N; i++) {
    for (long long j = 0; j < MAXV; j++) {
      if (v[i] > j) {
        if (dp[i - 1][j] == INF) {
        } else {
          dp[i][j] = dp[i - 1][j];
        }
      } else {
        if (dp[i - 1][j] != INF && dp[i - 1][j - v[i]] != INF) {
          dp[i][j] = min(w[i] + dp[i - 1][j - v[i]], dp[i - 1][j]);
        } else if (dp[i - 1][j] != INF) {
          dp[i][j] = dp[i - 1][j];
        } else if (dp[i - 1][j - v[i]] != INF) {
          dp[i][j] = dp[i - 1][j - v[i]] + w[i];
        }
      }
    }
  }
  long long ans = 0;
  for (long long i = 1; i < MAXV; i++) {
    if (dp[N - 1][i] <= W)
      ans = i;
  }
  cout << ans << endl;
}
Problem p03164 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define ll long long
#define li long
#define pb push_back
#define mem(arr, x) memset(arr, x, sizeof(arr))

ll n, W;
ll w[101], v[101];
ll dp[101][100010];
ll totalV = 0;

ll minWeight(ll end, ll Val) {
  if (Val <= 0)
    return 0;
  if (end == 0)
    return 1000000010;
  // return INT_MAX;
  // 2147483647
  if (dp[end][Val] != 1000000010)
    return dp[end][Val];
  if (v[end - 1] > Val)
    return dp[end][Val] = minWeight(end - 1, Val);
  else
    return dp[end][Val] = min(w[end - 1] + minWeight(end - 1, Val - v[end - 1]),
                              minWeight(end - 1, Val));
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll i, j;
  cin >> n >> W;
  for (i = 0; i < n; i++) {
    cin >> w[i] >> v[i];
    totalV += v[i];
  }

  for (i = 0; i <= n; i++)
    for (j = 0; j <= totalV; j++)
      dp[i][j] = 1000000010;

  for (i = totalV; i >= 1; i--) {
    if (minWeight(n, i) <= W)
      break;
  }
  cout << i;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define ll long long
#define li long
#define pb push_back
#define mem(arr, x) memset(arr, x, sizeof(arr))

ll n, W;
ll w[101], v[101];
ll dp[101][100010];
ll totalV = 0;

ll minWeight(ll end, ll Val) {
  if (Val <= 0)
    return 0;
  if (end == 0)
    // return 1000000010;
    return INT_MAX;
  // 2147483647
  if (dp[end][Val] != 1000000010)
    return dp[end][Val];
  if (v[end - 1] > Val)
    return dp[end][Val] = minWeight(end - 1, Val);
  else
    return dp[end][Val] = min(w[end - 1] + minWeight(end - 1, Val - v[end - 1]),
                              minWeight(end - 1, Val));
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll i, j;
  cin >> n >> W;
  for (i = 0; i < n; i++) {
    cin >> w[i] >> v[i];
    totalV += v[i];
  }

  for (i = 0; i <= n; i++)
    for (j = 0; j <= totalV; j++)
      dp[i][j] = 1000000010;

  for (i = totalV; i >= 1; i--) {
    if (minWeight(n, i) <= W)
      break;
  }
  cout << i;
  return 0;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <limits.h>
#include <vector>
using namespace std;

const long long inf = 1e12;

int main() {
  int n, t;
  cin >> n >> t;
  long long pricesum = 0;

  vector<int> w(n), p(n);
  for (int i = 0; i < n; i++) {
    cin >> w[i] >> p[i];
    pricesum += p[i];
  }
  vector<long long> dp(pricesum + 1, inf);

  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = pricesum; j >= 0; j--) {
      dp[j] = min(w[i - 1] + dp[j - p[i - 1]], dp[j]);
    }
  }
  long long ans = LLONG_MIN;
  long long ansprice = 0;
  for (int j = 1; j <= pricesum; j++) {
    if ((dp[j] <= t) && (ans + dp[j] > ans)) {
      ans = ans + dp[j];
      ansprice = j;
    }
  }

  cout << ansprice << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <limits.h>
#include <vector>
using namespace std;

const long long inf = 1e12;

int main() {
  int n, t;
  cin >> n >> t;
  long long pricesum = 0;

  vector<int> w(n), p(n);
  for (int i = 0; i < n; i++) {
    cin >> w[i] >> p[i];
    pricesum += p[i];
  }
  vector<long long> dp(pricesum + 1, inf);

  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = pricesum; j >= p[i - 1]; j--) {
      dp[j] = min(w[i - 1] + dp[j - p[i - 1]], dp[j]);
    }
  }
  long long ans = LLONG_MIN;
  long long ansprice = 0;
  for (int j = 1; j <= pricesum; j++) {
    if ((dp[j] <= t) && (ans + dp[j] > ans)) {
      ans = ans + dp[j];
      ansprice = j;
    }
  }

  cout << ansprice << endl;
  return 0;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
// #pragma GCC optimize "trapv"
#include <bits/stdc++.h>
#define ll long long int
#define fab(a, b, i) for (int i = a; i < b; i++)
#define pb push_back
#define db double
#define mp make_pair
#define endl "\n"
#define f first
#define se second
#define all(x) x.begin(), x.end()
#define MOD 1000000007
#define BIG 1e10
#define quick                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)
using namespace std;
ll dp[100005][105];
ll a[105], b[105], n, w;
ll recurse(ll val, int i) {
  if (val <= 0)
    return 0;

  if (i == n)
    return BIG;
  if (dp[val][i] != -1)
    return dp[val][i];
  dp[val][i] = min(recurse(val, i + 1), a[i] + recurse(val - b[i], i + 1));

  return dp[val][i];
}
int main() {
  quick;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  // ll n,w;
  cin >> n >> w;

  fab(0, n, i) cin >> a[i] >> b[i];
  memset(dp, -1, sizeof(dp));
  for (int i = 100002; i >= 0; i--) {
    // cout<<recurse(i,0)<<endl;
    if (recurse(i, 0) <= w) {
      cout << i;
      return 0;
    }
  }

  return 0;
}

Fixed submission:
// #pragma GCC optimize "trapv"
#include <bits/stdc++.h>
#define ll long long int
#define fab(a, b, i) for (int i = a; i < b; i++)
#define pb push_back
#define db double
#define mp make_pair
#define endl "\n"
#define f first
#define se second
#define all(x) x.begin(), x.end()
#define MOD 1000000007
#define BIG 1e10
#define quick                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)
using namespace std;
ll dp[100005][105];
ll a[105], b[105], n, w;
ll recurse(ll val, int i) {
  if (val <= 0)
    return 0;

  if (i == n)
    return BIG;
  if (dp[val][i] != -1)
    return dp[val][i];
  dp[val][i] = min(recurse(val, i + 1), a[i] + recurse(val - b[i], i + 1));

  return dp[val][i];
}
int main() {
  quick;

  // ll n,w;
  cin >> n >> w;

  fab(0, n, i) cin >> a[i] >> b[i];
  memset(dp, -1, sizeof(dp));
  for (int i = 100002; i >= 0; i--) {
    // cout<<recurse(i,0)<<endl;
    if (recurse(i, 0) <= w) {
      cout << i;
      return 0;
    }
  }

  return 0;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <iostream>
#include <string.h>
#include <vector>
#define int int64_t
#define endl "\n"
#define INF INT_MAX - 2
using namespace std;

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

int n, capacity;
int knapsack(vector<int> &weight, vector<int> &price, int maxProfit) {
  vector<vector<int>> dp(102, vector<int>(1000, 0));
  // base case initiliazation
  for (int N = 0; N <= n; N++) {
    for (int profit = 0; profit <= maxProfit; profit++) {
      if (N == 0 && profit == 0) {
        dp[N][profit] = 0;
      } else if (N == 0) {
        dp[N][profit] = INF;
      } else if (price[N - 1] <= profit) {
        dp[N][profit] = min(weight[N - 1] + dp[N - 1][profit - price[N - 1]],
                            dp[N - 1][profit]);
      } else {
        dp[N][profit] = dp[N - 1][profit];
      }
    }
  }
  int ans = 0;
  for (int profit = 0; profit <= maxProfit; profit++) {
    if (dp[n][profit] <= capacity && dp[n][profit] != INF) {
      ans = profit;
    }
  }

  return ans;
}
void Solution() {
  cin >> n >> capacity;
  vector<int> weight(n);
  vector<int> price(n);
  int maxProfit = 0;
  for (int i = 0; i < n; i++) {
    cin >> weight[i] >> price[i];
    maxProfit += price[i];
  }

  cout << knapsack(weight, price, maxProfit);
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  // io();
  Solution();
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <iostream>
#include <string.h>
#include <vector>
#define int int64_t
#define endl "\n"
#define INF INT_MAX - 2
using namespace std;

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

int n, capacity;
int knapsack(vector<int> &weight, vector<int> &price, int maxProfit) {
  vector<vector<int>> dp(102, vector<int>(100000, 0));
  // base case initiliazation
  for (int N = 0; N <= n; N++) {
    for (int profit = 0; profit <= maxProfit; profit++) {
      if (N == 0 && profit == 0) {
        dp[N][profit] = 0;
      } else if (N == 0) {
        dp[N][profit] = INF;
      } else if (price[N - 1] <= profit) {
        dp[N][profit] = min(weight[N - 1] + dp[N - 1][profit - price[N - 1]],
                            dp[N - 1][profit]);
      } else {
        dp[N][profit] = dp[N - 1][profit];
      }
    }
  }
  int ans = 0;
  for (int profit = 0; profit <= maxProfit; profit++) {
    if (dp[n][profit] <= capacity && dp[n][profit] != INF) {
      ans = profit;
    }
  }

  return ans;
}
void Solution() {
  cin >> n >> capacity;
  vector<int> weight(n);
  vector<int> price(n);
  int maxProfit = 0;
  for (int i = 0; i < n; i++) {
    cin >> weight[i] >> price[i];
    maxProfit += price[i];
  }

  cout << knapsack(weight, price, maxProfit);
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  // io();
  Solution();
}
Problem p03164 (C++)

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

const int mx = 1e3 + 10;
ll w[200], v[200], dp[mx][200], n, W;

ll find_max(ll val, ll index) {
  if (index == n)
    return val == 0 ? 0 : INT_MAX;

  if (~dp[val][index])
    return dp[val][index];

  ll ans = find_max(val, index + 1);

  if (v[index] <= val) {
    ans = min(ans, find_max(val - v[index], index + 1) + w[index]);
  }
  return dp[val][index] = ans;
}
ll calc(ll mxx) {
  for (ll i = mxx; i >= 1; i--) {
    if (find_max(i, 0) <= W) {
      return i;
    }
  }
  return 0;
}

void solve() {
  ll mxx = 0;
  cin >> n >> W;

  memset(dp, -1, sizeof dp);

  for (int i = 0; i < n; i++) {
    cin >> w[i] >> v[i];
    mxx += v[i];
  }
  cout << calc(mxx) << endl;
}

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

  int t = 1;
  // cin>>t;

  while (t--) {
    solve();
  }
}


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

const int mx = 100009;
ll w[mx], v[mx], dp[mx][200], n, W;

ll find_max(ll val, ll index) {
  if (index == n)
    return val == 0 ? 0 : INT_MAX;

  if (~dp[val][index])
    return dp[val][index];

  ll ans = find_max(val, index + 1);

  if (v[index] <= val) {
    ans = min(ans, find_max(val - v[index], index + 1) + w[index]);
  }
  return dp[val][index] = ans;
}
ll calc(ll mxx) {
  for (ll i = mxx; i >= 1; i--) {
    if (find_max(i, 0) <= W) {
      return i;
    }
  }
  return 0;
}

void solve() {
  ll mxx = 0;
  cin >> n >> W;

  memset(dp, -1, sizeof dp);

  for (int i = 0; i < n; i++) {
    cin >> w[i] >> v[i];
    mxx += v[i];
  }
  cout << calc(mxx) << endl;
}

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

  int t = 1;
  // cin>>t;

  while (t--) {
    solve();
  }
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int N, W, w[101], v[102];
long long dp[102][10003];

long long rec(int pos, int val) {
  // printf("%d %d\n", pos, val);
  if (dp[pos][val] != -1) {
    return dp[pos][val];
  }
  long long ans = 2000000000000;
  if (pos != 0) {
    ans = rec(pos - 1, val);
  }
  if (v[pos] <= val && pos != 0) {
    ans = min(ans, w[pos] + rec(pos - 1, val - v[pos]));
  } else if (pos == 0 && v[pos] == val) {
    ans = w[pos];
  } else if (pos == 0 && val == 0) {
    ans = 0;
  }
  dp[pos][val] = ans;
  // printf("%lld\n", ans);
  return ans;
}

int main() {
  scanf("%d %d", &N, &W);
  for (int i = 0; i < N; ++i) {
    scanf("%d %d", &w[i], &v[i]);
  }
  memset(dp, -1, sizeof(dp));
  for (int i = N * 1000 + 1; i >= 0; --i) {
    for (int j = N - 1; j >= 0; --j) {
      if (rec(j, i) <= W) {
        printf("%d", i);
        return 0;
      }
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int N, W, w[101], v[102];
long long dp[102][100003];

long long rec(int pos, int val) {
  // printf("%d %d\n", pos, val);
  if (dp[pos][val] != -1) {
    return dp[pos][val];
  }
  long long ans = 2000000000000;
  if (pos != 0) {
    ans = rec(pos - 1, val);
  }
  if (v[pos] <= val && pos != 0) {
    ans = min(ans, w[pos] + rec(pos - 1, val - v[pos]));
  } else if (pos == 0 && v[pos] == val) {
    ans = w[pos];
  } else if (pos == 0 && val == 0) {
    ans = 0;
  }
  dp[pos][val] = ans;
  // printf("%lld\n", ans);
  return ans;
}

int main() {
  scanf("%d %d", &N, &W);
  for (int i = 0; i < N; ++i) {
    scanf("%d %d", &w[i], &v[i]);
  }
  memset(dp, -1, sizeof(dp));
  for (int i = N * 1000 + 1; i >= 0; --i) {
    for (int j = N - 1; j >= 0; --j) {
      if (rec(j, i) <= W) {
        printf("%d", i);
        return 0;
      }
    }
  }
}
Problem p03164 (C++)

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

using namespace std;

#define fore(i, s, e) for (int i = s; i < e; i++)

const int N = 100;
const int V = 10005;

typedef long long ll;

int n, w;

ll dp[N][V];

ll ww[N], v[V];

int main() {
  cin >> n >> w;
  int s = 0;
  fore(i, 1, n + 1) {
    cin >> ww[i] >> v[i];
    s += v[i];
  }
  fore(i, 0, s + 1) dp[0][i] = 1e17;
  fore(i, 0, n + 1) dp[i][0] = 0;

  fore(i, 1, n + 1) {
    fore(j, 1, s + 1) {
      dp[i][j] = dp[i - 1][j];
      if (v[i] <= j)
        dp[i][j] = min(dp[i][j], dp[i - 1][j - v[i]] + ww[i]);
    }
  }
  int res = 0;
  fore(j, 0, s + 1) if (dp[n][j] <= w) res = max(res, j);
  cout << res << endl;
}

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

using namespace std;

#define fore(i, s, e) for (int i = s; i < e; i++)

const int N = 200;
const int V = 2e5;

typedef long long ll;

int n, w;

ll dp[N][V];

ll ww[N], v[V];

int main() {
  cin >> n >> w;
  int s = 0;
  fore(i, 1, n + 1) {
    cin >> ww[i] >> v[i];
    s += v[i];
  }
  fore(i, 0, s + 1) dp[0][i] = 1e17;
  fore(i, 0, n + 1) dp[i][0] = 0;

  fore(i, 1, n + 1) {
    fore(j, 1, s + 1) {
      dp[i][j] = dp[i - 1][j];
      if (v[i] <= j)
        dp[i][j] = min(dp[i][j], dp[i - 1][j - v[i]] + ww[i]);
    }
  }
  int res = 0;
  fore(j, 0, s + 1) if (dp[n][j] <= w) res = max(res, j);
  cout << res << endl;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long n, k;
long long w[109], v[109];
long long dp[109][100009];
int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> w[i] >> v[i];
  for (int i = 0; i < n; i++) {
    for (int j = 1; j < 100009; j++) {
      dp[i][j] = 1000000000000009;
    }
  }
  dp[0][v[0]] = w[0];
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < 100009; j++) {
      if (j - v[0] >= 0)
        dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - v[i]] + w[i]);
      else
        dp[i][j] = dp[i - 1][j];
    }
  }
  for (int i = 100001; i >= 0; i--) {
    if (dp[n - 1][i] <= k) {
      cout << i << endl;
      exit(0);
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long n, k;
long long w[109], v[109];
long long dp[109][100009];
int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> w[i] >> v[i];
  for (int i = 0; i < n; i++) {
    for (int j = 1; j < 100009; j++) {
      dp[i][j] = 1000000000000009;
    }
  }
  dp[0][v[0]] = w[0];
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < 100009; j++) {
      if (j - v[i] >= 0)
        dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - v[i]] + w[i]);
      else
        dp[i][j] = dp[i - 1][j];
    }
  }
  for (int i = 100001; i >= 0; i--) {
    if (dp[n - 1][i] <= k) {
      cout << i << endl;
      exit(0);
    }
  }
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):

/**
 *    Coded by : lucky_21
 *               --------Lokesh Singh
 **/

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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <class T>
using oset =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define F first
#define S second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define pii pair<int, int>
#define all(x) x.begin(), x.end()
#define fix fixed << setprecision(10)
#define rep(i, a, b) for (int i = int(a); i <= int(b); i++)
#define repb(i, b, a) for (int i = int(b); i >= int(a); i--)
#define FastIO ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)

typedef double db;
typedef long long ll;

const int N = 1e5 + 5;
const int mod = 1e9 + 7;

ll n, W, V, w[N], v[N], dp[N][105];
ll rec(int val, int i) {
  if (i > n)
    return (val <= 0 ? 0 : 1e9);
  ll &cache = dp[val][i];
  if (cache != -1)
    return cache;
  rec(val - 1, i);
  cache = rec(val, i + 1);
  cache = min(cache, w[i] + rec(val - v[i], i + 1));
  return cache;
}
signed main() {
  FastIO;
  cin >> n >> W;
  rep(i, 1, n) cin >> w[i] >> v[i], V += v[i];
  memset(dp, -1, sizeof dp);
  rec(V, 1);
  int ans = 0;
  rep(i, 1, V) { rep(j, 1, n) if (dp[i][j] != -1 and dp[i][j] <= W) ans = i; }
  cout << ans;
  return 0;
}


Fixed submission:

/**
 *    Coded by : lucky_21
 *               --------Lokesh Singh
 **/

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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <class T>
using oset =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define F first
#define S second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define pii pair<int, int>
#define all(x) x.begin(), x.end()
#define fix fixed << setprecision(10)
#define rep(i, a, b) for (int i = int(a); i <= int(b); i++)
#define repb(i, b, a) for (int i = int(b); i >= int(a); i--)
#define FastIO ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)

typedef double db;
typedef long long ll;

const int N = 1e5 + 5;
const int mod = 1e9 + 7;

ll n, W, V, w[N], v[N], dp[N][105];
ll rec(int val, int i) {
  if (val == 0)
    return 0;
  if (i > n or val < 0)
    return 1e9;
  ll &cache = dp[val][i];
  if (cache != -1)
    return cache;
  rec(val - 1, i);
  cache = rec(val, i + 1);
  cache = min(cache, w[i] + rec(val - v[i], i + 1));
  return cache;
}
signed main() {
  FastIO;
  cin >> n >> W;
  rep(i, 1, n) cin >> w[i] >> v[i], V += v[i];
  memset(dp, -1, sizeof dp);
  rec(V, 1);
  int ans = 0;
  rep(i, 1, V) { rep(j, 1, n) if (dp[i][j] != -1 and dp[i][j] <= W) ans = i; }
  cout << ans;
  return 0;
}
Problem p03164 (C++)

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

long long N, maxW;
long long dp[102][100002];
struct asdfklj {
  long long val, w;
} arr[102];

long long knapsack(long long i, long long current) {
  if (current < 0)
    return INT_MAX;
  if (i == N) {
    if (current == 0)
      return 0;
    return INT_MAX;
  }
  long long &ret = dp[i][current];
  return ret = min(arr[i].w + knapsack(i + 1, current - arr[i].val),
                   knapsack(i + 1, current));
}

int main() {
  cin >> N >> maxW;
  memset(dp, -1, sizeof(dp));
  for (long long i = 0; i < N; i++)
    cin >> arr[i].w >> arr[i].val;
  for (long long i = 100000;; i--)
    if (knapsack(0, i) <= maxW)
      printf("%d", i), exit(0);
}


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

long long N, maxW;
long long dp[102][100002];
struct asdfklj {
  long long val, w;
} arr[102];

long long knapsack(long long i, long long current) {
  if (current < 0)
    return INT_MAX;
  if (i == N) {
    if (current == 0)
      return 0;
    return INT_MAX;
  }
  long long &ret = dp[i][current];
  if (ret != -1)
    return ret;
  return ret = min(arr[i].w + knapsack(i + 1, current - arr[i].val),
                   knapsack(i + 1, current));
}

int main() {
  cin >> N >> maxW;
  memset(dp, -1, sizeof(dp));
  for (long long i = 0; i < N; i++)
    cin >> arr[i].w >> arr[i].val;
  for (long long i = 100000;; i--)
    if (knapsack(0, i) <= maxW)
      printf("%d", i), exit(0);
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#define loopi(x, y) for (int i = x; i < (y); i++)
#define rloopi(x, y) for (int i = x; i >= (y); i--)
#define rloopj(x, y) for (int j = x; j >= (y); j--)
#define loopj(x, y) for (int j = x; j < (y); j++)

#define nl cout << "\n";
#define ll long long int
using namespace std;
#define mod 1000000007

ll power(ll a, ll p) {
  if (p == 0)
    return 1;

  ll t = power(a, p / 2) % mod;

  if (p % 2 == 0)
    return t * t % mod;
  return (a * t % mod) * t % mod;
}

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

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

  int n, W;
  cin >> n >> W;
  vector<int> w(n);
  vector<int> v(n);
  ll max_val = 0;

  loopi(0, n) {
    cin >> w[i] >> v[i];
    max_val += v[i];
  }

  vector<int> dp(max_val + 1, 1000000001);
  dp[0] = 0;
  loopj(0, n) {
    rloopi(max_val, v[j]) { dp[i] = min(dp[i - v[j]] + w[j], dp[i]); }
  }

  int ans;
  rloopi(max_val, 0) {
    if (dp[i] <= W) {
      ans = i;
      break;
    }
  }

  cout << ans;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#define loopi(x, y) for (int i = x; i < (y); i++)
#define rloopi(x, y) for (int i = x; i >= (y); i--)
#define rloopj(x, y) for (int j = x; j >= (y); j--)
#define loopj(x, y) for (int j = x; j < (y); j++)

#define nl cout << "\n";
#define ll long long int
using namespace std;
#define mod 1000000007

ll power(ll a, ll p) {
  if (p == 0)
    return 1;

  ll t = power(a, p / 2) % mod;

  if (p % 2 == 0)
    return t * t % mod;
  return (a * t % mod) * t % mod;
}

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

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

  int n, W;
  cin >> n >> W;
  vector<int> w(n);
  vector<int> v(n);
  ll max_val = 0;

  loopi(0, n) {
    cin >> w[i] >> v[i];
    max_val += v[i];
  }

  vector<int> dp(max_val + 1, 1000000001);
  dp[0] = 0;
  loopj(0, n) {
    rloopi(max_val, v[j]) { dp[i] = min(dp[i - v[j]] + w[j], dp[i]); }
  }

  int ans;
  rloopi(max_val, 0) {
    if (dp[i] <= W) {
      ans = i;
      break;
    }
  }

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

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

using namespace std;

typedef long long ll;

const ll inf = 1LL << 60;

int w[110], v[110];
long long dp[10010];

int main() {
  ios_base::sync_with_stdio(false);

  int n, W;
  cin >> n >> W;

  int V = 0;

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

    V += v[i];
  }

  /// dp[v] = minimum weight that i can achieve with value = v

  for (int i = 0; i <= V; i++)
    dp[i] = inf;
  dp[0] = 0;

  for (int i = 0; i < n; i++) {
    for (int j = V - v[i]; j >= 0; j--) {
      dp[j + v[i]] = min(dp[j + v[i]], dp[j] + w[i] * 1LL);
    }
  }

  int ans = 0;

  for (int i = 0; i <= V; i++) {
    if (dp[i] <= W) {
      ans = i;
    }
  }

  cout << ans << endl;
}

/*
 * E
 **/


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

using namespace std;

typedef long long ll;

const ll inf = 1LL << 60;

int w[110], v[110];
ll dp[100010];

int main() {
  ios_base::sync_with_stdio(false);

  int n, W;
  cin >> n >> W;

  int V = 0;

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

    V += v[i];
  }

  /// dp[v] = minimum weight that i can achieve with value = v

  for (int i = 0; i <= V; i++)
    dp[i] = inf;
  dp[0] = 0;

  for (int i = 0; i < n; i++) {
    for (int j = V - v[i]; j >= 0; j--) {
      dp[j + v[i]] = min(dp[j + v[i]], dp[j] + w[i] * 1LL);
    }
  }

  int ans = 0;

  for (int i = 0; i <= V; i++) {
    if (dp[i] <= W) {
      ans = i;
    }
  }

  cout << ans << endl;
}

/*
 * E
 **/
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
// https://atcoder.jp/contests/dp/tasks/dp_e
#include <bits/stdc++.h>
#define loop(i, a, b) for (i = (a); i < (b); i++)
#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(), (v).end()
#define fr first
#define sc second
#define mk make_pair
#define endl '\n'
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<ll, ll> pl;
typedef pair<int, int> pi;
const int mx = 1e5 + 10;
ll n, w, ans = 0;
ll arr[101][2];
ll dp[100][mx];
void go(ll idx) {
  if (idx == n + 1)
    return;
  for (ll i = 0; i < mx; i++) {
    dp[idx][i] = dp[idx - 1][i];
    if (i >= arr[idx][1]) {
      dp[idx][i] = min(dp[idx][i], dp[idx - 1][i - arr[idx][1]] + arr[idx][0]);
    }
    if (dp[idx][i] <= w) {
      // cout<<dp[idx][i]<<' ';
      // cout<<i<<' '<<idx<<endl;
      ans = max(ans, i);
    }
  }
  go(idx + 1);
}
void solve() {
  cin >> n >> w;
  for (ll i = 0; i < 100; i++)
    for (ll j = 0; j < mx; j++)
      dp[i][j] = 1e18;
  dp[0][0] = 0;
  for (ll i = 1; i <= n; i++) {
    cin >> arr[i][0] >> arr[i][1]; // w and val
  }
  go(1);
  cout << ans << endl;
}
int main() {
  int t;
  t = 1;
  while (t--)
    solve();
}

Fixed submission:
// https://atcoder.jp/contests/dp/tasks/dp_e
#include <bits/stdc++.h>
#define loop(i, a, b) for (i = (a); i < (b); i++)
#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(), (v).end()
#define fr first
#define sc second
#define mk make_pair
#define endl '\n'
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<ll, ll> pl;
typedef pair<int, int> pi;
const int mx = 1e5 + 10;
ll n, w, ans = 0;
ll arr[102][2];
ll dp[102][mx];
void go(ll idx) {
  if (idx == n + 1)
    return;
  for (ll i = 0; i < mx; i++) {
    dp[idx][i] = dp[idx - 1][i];
    if (i >= arr[idx][1]) {
      dp[idx][i] = min(dp[idx][i], dp[idx - 1][i - arr[idx][1]] + arr[idx][0]);
    }
    if (dp[idx][i] <= w) {
      // cout<<dp[idx][i]<<' ';
      // cout<<i<<' '<<idx<<endl;
      ans = max(ans, i);
    }
  }
  go(idx + 1);
}
void solve() {
  cin >> n >> w;
  for (ll i = 0; i < 100; i++)
    for (ll j = 0; j < mx; j++)
      dp[i][j] = 1e18;
  dp[0][0] = 0;
  for (ll i = 1; i <= n; i++) {
    cin >> arr[i][0] >> arr[i][1]; // w and val
  }
  go(1);
  cout << ans << endl;
}
int main() {
  int t;
  t = 1;
  while (t--)
    solve();
}
Problem p03164 (C++)

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

/*
#pragma GCC optimize("-Ofast")
//#pragma GCC optimize("trapv")
#pragma GCC
target("sse,sse2,sse3,ssse3,sse4,sse4.2,popcnt,abm,mmx,avx2,tune=native")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-funroll-loops")*/

#define I inline void
#define S struct
#define vi vector<int>
#define vii vector<pair<int, int>>
#define pii pair<int, int>
#define pll pair<ll, ll>

using namespace std;
using ll = long long;
using ld = long double;

const int N = 1e5 + 7, mod = 1e9 + 7;
const ll inf = 1e11;
// How interesting!

int n, w;
ll dp[N];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // freopen("in.in" , "r" , stdin) ;

  cin >> n >> w;
  for (int j = 1; j < N; j++) {
    dp[j] = inf;
  }

  for (int i = 1; i <= n; i++) {
    ll x, y;
    cin >> x >> y;
    for (int j = N - 1; ~j; j--) {
      dp[j + y] = min(dp[j + y], dp[j] + x);
    }
  }
  int ans = 0;
  for (int i = 0; i < N; i++) {
    if (dp[i] <= w)
      ans = i;
  }
  cout << ans;
  return 0;
}

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

/*
#pragma GCC optimize("-Ofast")
//#pragma GCC optimize("trapv")
#pragma GCC
target("sse,sse2,sse3,ssse3,sse4,sse4.2,popcnt,abm,mmx,avx2,tune=native")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-funroll-loops")*/

#define I inline void
#define S struct
#define vi vector<int>
#define vii vector<pair<int, int>>
#define pii pair<int, int>
#define pll pair<ll, ll>

using namespace std;
using ll = long long;
using ld = long double;

const int N = 1e5 + 7, mod = 1e9 + 7;
const ll inf = 1e11;
// How interesting!

int n, w;
ll dp[N];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // freopen("in.in" , "r" , stdin) ;

  cin >> n >> w;
  for (int j = 1; j < N; j++) {
    dp[j] = inf;
  }

  for (int i = 1; i <= n; i++) {
    ll x, y;
    cin >> x >> y;
    for (int j = N - y - 1; ~j; j--) {
      dp[j + y] = min(dp[j + y], dp[j] + x);
    }
  }
  int ans = 0;
  for (int i = 0; i < N; i++) {
    if (dp[i] <= w)
      ans = i;
  }
  cout << ans;
  return 0;
}