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

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

Int a[N];
Int dp[N][N2];
Int sum[N];
Int w[N], v[N];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, W;
  for (int i = 0; i <= n; i++)
    for (int j = 1; j <= 100000; j++)
      dp[i][j] = (1 << 30);
  cin >> n >> W;
  for (int i = 1; i <= n; i++)
    cin >> w[i] >> v[i];
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= 100000; j++) {
      // if(j<v[i]){dp[i][j]=min(w[i],dp[i-1][j]);continue;}
      dp[i][j] = min((dp[i - 1][j - v[i]] + w[i]), dp[i - 1][j]);
    }
  }
  Int t = 0;
  for (Int i = 1; i <= 100000; i++)
    if (dp[n][i] <= W)
      t = max(t, i);
  cout << t << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define Int long long
#define N 102
#define N2 100100

Int a[N];
Int dp[N][N2];
Int sum[N];
Int w[N], v[N];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, W;
  for (int i = 0; i <= n; i++)
    for (int j = 1; j <= 100000; j++)
      dp[i][j] = (1 << 30);
  cin >> n >> W;
  for (int i = 1; i <= n; i++)
    cin >> w[i] >> v[i];
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= 100000; j++) {
      if (j <= v[i]) {
        dp[i][j] = min(w[i], dp[i - 1][j]);
        continue;
      }
      dp[i][j] = min((dp[i - 1][j - v[i]] + w[i]), dp[i - 1][j]);
    }
  }
  Int t = 0;
  for (Int i = 1; i <= 100000; i++)
    if (dp[n][i] <= W)
      t = max(t, i);
  cout << t << endl;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, N) for (ll(i) = 0; (i) < (N); (i)++)
const int mod = 1000000007;
const int INF = 1100000000;
int main() {
  int n, W;
  cin >> n >> W;
  vector<ll> w(n), v(n);
  rep(i, n) { cin >> w[i] >> v[i]; }
  vector<vector<ll>> dp(100, vector<ll>(100105, INF));
  dp[0][0] = 0;
  rep(i, n) {
    rep(j, 100100) {
      if (j - v[i] >= 0) {
        dp[i + 1][j] = min(dp[i + 1][j], dp[i][j - v[i]] + w[i]);
      }
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
    }
  }
  ll ans = 0;
  for (int i = 0; i < 100100; ++i) {
    if (dp[n][i] <= W)
      ans = i;
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, N) for (ll(i) = 0; (i) < (N); (i)++)
const int mod = 1000000007;
const int INF = 1100000000;
int main() {
  int n, W;
  cin >> n >> W;
  vector<ll> w(n), v(n);
  rep(i, n) { cin >> w[i] >> v[i]; }
  vector<vector<ll>> dp(110, vector<ll>(100105, INF));
  dp[0][0] = 0;
  rep(i, n) {
    rep(j, 100100) {
      if (j - v[i] >= 0) {
        dp[i + 1][j] = min(dp[i + 1][j], dp[i][j - v[i]] + w[i]);
      }
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
    }
  }
  ll ans = 0;
  for (int i = 0; i < 100100; ++i) {
    if (dp[n][i] <= W)
      ans = i;
  }
  cout << ans << endl;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 10010;
int n;
long long f[N], W, w[N], v[N], s = 0;

int main() {
  scanf("%d%lld", &n, &W);
  for (int i = 1; i <= n; i++) {
    scanf("%lld%lld", &w[i], &v[i]);
    s += v[i];
  }
  for (long long i = 0; i <= s; i++)
    f[i] = 1e15;
  f[0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = s; j >= v[i]; j--) {
      f[j] = min(f[j], f[j - v[i]] + w[i]);
    }
  }
  for (long long i = s; i >= 0; i--) {
    if (f[i] <= W) {
      printf("%lld\n", i);
      return 0;
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int n;
long long f[N], W, w[N], v[N], s = 0;

int main() {
  scanf("%d%lld", &n, &W);
  for (int i = 1; i <= n; i++) {
    scanf("%lld%lld", &w[i], &v[i]);
    s += v[i];
  }
  for (long long i = 0; i <= s; i++)
    f[i] = 1e15;
  f[0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = s; j >= v[i]; j--) {
      f[j] = min(f[j], f[j - v[i]] + w[i]);
    }
  }
  for (long long i = s; i >= 0; i--) {
    if (f[i] <= W) {
      printf("%lld\n", i);
      return 0;
    }
  }
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = int(a); i < int(b); i++)
#define rer(i, a, b) for (int i = int(a) - 1; i >= int(b); i--)
#define sz(v) (int)(v).size()
#define pb push_back
#define sc second
#define fr first
#define sor(v) sort(v.begin(), v.end())
#define rev(s) reverse(s.begin(), s.end())
#define lb(vec, a) lower_bound(vec.begin(), vec.end(), a)
#define ub(vec, a) upper_bound(vec.begin(), vec.end(), a)
#define uniq(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end())
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
const ll INF = 1e12;
const ll MOD = 1000000007;
ll dp[100][100001];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, W;
  cin >> N >> W;
  ll w[N], v[N];
  rep(i, 0, N) cin >> w[i] >> v[i];
  rep(i, 0, N) {
    rep(j, 0, 100001) { dp[i][j] = INF; }
  }
  dp[0][0] = 0;
  rep(i, 0, N) {
    rep(j, 0, 100001) {
      if (j < v[i])
        dp[i + 1][j] = dp[i][j];
      else
        dp[i + 1][j] = min(dp[i][j], dp[i][j - v[i]] + w[i]);
    }
  }
  ll ans = 0;
  rep(i, 1, 100001) {
    if (W >= dp[N][i])
      ans = i;
  }
  cout << ans << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = int(a); i < int(b); i++)
#define rer(i, a, b) for (int i = int(a) - 1; i >= int(b); i--)
#define sz(v) (int)(v).size()
#define pb push_back
#define sc second
#define fr first
#define sor(v) sort(v.begin(), v.end())
#define rev(s) reverse(s.begin(), s.end())
#define lb(vec, a) lower_bound(vec.begin(), vec.end(), a)
#define ub(vec, a) upper_bound(vec.begin(), vec.end(), a)
#define uniq(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end())
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
const ll INF = 1e12;
const ll MOD = 1000000007;
ll dp[101][100001];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, W;
  cin >> N >> W;
  ll w[N], v[N];
  rep(i, 0, N) cin >> w[i] >> v[i];
  rep(i, 0, N) {
    rep(j, 0, 100001) { dp[i][j] = INF; }
  }
  dp[0][0] = 0;
  rep(i, 0, N) {
    rep(j, 0, 100001) {
      if (j < v[i])
        dp[i + 1][j] = dp[i][j];
      else
        dp[i + 1][j] = min(dp[i][j], dp[i][j - v[i]] + w[i]);
    }
  }
  ll ans = 0;
  rep(i, 1, 100001) {
    if (W >= dp[N][i])
      ans = i;
  }
  cout << ans << "\n";
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
#ifdef _debug
#define dout(i) cout << #i << ' ' << i << ' '
#else
#define dout(i) //
#endif
using ll = long long;
using ull = unsigned long long;
using ul = unsigned;
using db = double;
const int maxn = 101;
const int maxw = 100001;
const int inf = 1000000001;
ll dp[maxw];
int n, lim;

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  cin >> n >> lim;
  fill(dp + 1, dp + lim + 1, inf);
  for (int w, v, i = 0; i < n; ++i) {
    cin >> w >> v;
    for (int j = maxw - 1; j >= v; --j)
      dp[j] = min(dp[j], dp[j - v] + w);
  }
  for (int i = maxw - 1; i >= 0; --i)
    if (dp[i] <= lim)
      return cout << i << '\n', 0;
}

Fixed submission:
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
#ifdef _debug
#define dout(i) cout << #i << ' ' << i << ' '
#else
#define dout(i) //
#endif
using ll = long long;
using ull = unsigned long long;
using ul = unsigned;
using db = double;
const int maxn = 101;
const int maxw = 100001;
const int inf = 1000000001;
ll dp[maxw];
int n, lim;

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  cin >> n >> lim;
  fill(dp + 1, dp + maxw, inf);
  for (int w, v, i = 0; i < n; ++i) {
    cin >> w >> v;
    for (int j = maxw - 1; j >= v; --j)
      dp[j] = min(dp[j], dp[j - v] + w);
  }
  for (int i = maxw - 1; i >= 0; --i)
    if (dp[i] <= lim)
      return cout << i << '\n', 0;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll N, W;
ll dp[100 * 1000 + 5];
ll max(ll a, ll b) {
  if (a > b)
    return a;
  return b;
}

// dp[w][i] = max(dp[w-wt[i]][i-1]+v[i], dp[w][i-1])

// dp[v] = min(wt[i] + dp[v-V[i]], dp[v])
ll wt[100 + 5], V[100 + 5];

ll MAXV = 100;
int main() {
  cin >> N >> W;
  for (int i = 0; i < N; i++) {
    cin >> wt[i] >> V[i];
    MAXV += V[i];
  }

  for (ll j = 0; j <= MAXV; j++)
    dp[j] = 1e18;

  dp[0] = 0;
  for (ll i = 0; i < N; i++) {
    for (ll v = MAXV; v >= V[i]; v--) {
      dp[v] = min(dp[v], wt[i] + dp[v - V[i]]);
    }
  }
  ll ans = 0;
  for (ll v = MAXV; v >= 1; v--) {
    if (dp[v] <= W) {
      ans = v;
      break;
    }
  }

  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll N, W;
ll dp[100 * 1000 + 5];
ll max(ll a, ll b) {
  if (a > b)
    return a;
  return b;
}

// dp[w][i] = max(dp[w-wt[i]][i-1]+v[i], dp[w][i-1])

// dp[v] = min(wt[i] + dp[v-V[i]], dp[v])
ll wt[100 + 5], V[100 + 5];

ll MAXV = 1;
int main() {
  cin >> N >> W;
  for (int i = 0; i < N; i++) {
    cin >> wt[i] >> V[i];
    MAXV += V[i];
  }

  for (ll j = 0; j <= MAXV; j++)
    dp[j] = 1e18;

  dp[0] = 0;
  for (ll i = 0; i < N; i++) {
    for (ll v = MAXV; v >= V[i]; v--) {
      dp[v] = min(dp[v], wt[i] + dp[v - V[i]]);
    }
  }
  ll ans = 0;
  for (ll v = MAXV; v >= 1; v--) {
    if (dp[v] <= W) {
      ans = v;
      break;
    }
  }

  cout << ans << endl;
}
Problem p03164 (C++)

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

int n;
long long w[110], v[110];
long long dp[110][100010];

#define INF 10000000000000
#define VMAX 100010

int main() {
  long long W;
  cin >> n >> W;

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

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

  dp[0][0] = 0;

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

  int ans = 0;
  for (int i = 0; i < VMAX; i++)
    if (dp[n][i] <= W)
      ans = i;

  cout << ans << endl;

  return 0;
}


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

int n;
long long w[110], v[110];
long long dp[110][100010];

#define INF 10000000000000
#define VMAX 100010

int main() {
  long long W;
  cin >> n >> W;

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

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

  dp[0][0] = 0;

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

  int ans = 0;
  for (int i = 0; i < VMAX; i++)
    if (dp[n][i] <= W)
      ans = i;

  cout << ans << endl;

  return 0;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 1e9 + 5;
int main() {
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  int n;
  cin >> n;
  ll W;
  cin >> W;
  vector<int> w(n + 1);
  vector<int> v(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> w[i];
    cin >> v[i];
  }
  ll sum_value = 0;
  for (int i = 1; i <= n; i++) {
    sum_value += v[i];
  }
  const ll INF = 1e18l + 5;
  ll dp[n + 1][sum_value + 1];
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= sum_value; j++) {
      dp[i][j] = INF;
    }
  }
  for (int i = 0; i <= n; i++) {
    dp[i][0] = 0;
  }
  dp[0][0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= sum_value; j++) {
      if (v[i] <= j)
        dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - v[i]] + w[i]);
      else
        dp[i][j] = dp[i - 1][j];
      // cout << dp[i][j] << " " << j << endl;
    }
  }
  /*for (int i=0; i<=n; i++) {
      for (int j=0; j<=sum_value; j++) {
          if (dp[i][j] == INF)
              cout << "I" << " ";
          else
              cout << dp[i][j] << " ";
      }
      cout << endl;
  }*/
  ll ans = 0;
  for (int i = 1; i <= sum_value; i++) {
    if (dp[n][i] <= W)
      ans = max(ans, (ll)i);
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 1e9 + 5;
int main() {

  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  int n;
  cin >> n;
  ll W;
  cin >> W;
  vector<int> w(n + 1);
  vector<int> v(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> w[i];
    cin >> v[i];
  }
  ll sum_value = 0;
  for (int i = 1; i <= n; i++) {
    sum_value += v[i];
  }
  const ll INF = 1e18l + 5;
  ll dp[n + 1][sum_value + 1];
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= sum_value; j++) {
      dp[i][j] = INF;
    }
  }
  for (int i = 0; i <= n; i++) {
    dp[i][0] = 0;
  }
  dp[0][0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= sum_value; j++) {
      if (v[i] <= j)
        dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - v[i]] + w[i]);
      else
        dp[i][j] = dp[i - 1][j];
      // cout << dp[i][j] << " " << j << endl;
    }
  }
  /*for (int i=0; i<=n; i++) {
      for (int j=0; j<=sum_value; j++) {
          if (dp[i][j] == INF)
              cout << "I" << " ";
          else
              cout << dp[i][j] << " ";
      }
      cout << endl;
  }*/
  ll ans = 0;
  for (int i = 1; i <= sum_value; i++) {
    if (dp[n][i] <= W)
      ans = max(ans, (ll)i);
  }
  cout << ans << endl;
  return 0;
}
Problem p03164 (C++)

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

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

  const int maxn = n * n * 1000 + 1;
  vector<vector<long long>> dp(n + 1, vector<long long>(maxn, 1e15));

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

  int res = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < maxn; j++) {
      if (dp[i][j] <= W) {
        res = max(res, j);
      }
    }
  }
  cout << res << endl;
}

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

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

  const int maxn = n * 1000 + 1;
  vector<vector<long long>> dp(n + 1, vector<long long>(maxn, 1e15));

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

  int res = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < maxn; j++) {
      if (dp[i][j] <= W) {
        res = max(res, j);
      }
    }
  }
  cout << res << endl;
}
Problem p03164 (C++)

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

using namespace std;

long long N, G, i, j, mx;
long long p[1005], P, w[1005], a[1005], b[1005];

int main() {
  // ifstream cin("rucsac.in");
  cin >> N >> G;
  for (i = 1; i <= N; i++) {
    cin >> w[i] >> p[i];
    P += p[i];
  }
  for (i = 1; i <= P; i++)
    b[i] = 1000000000;
  // cout << P << " ";
  b[0] = 0;
  for (i = 1; i <= N; ++i) {
    for (j = 0; j <= P; ++j) {
      if (p[i] > j)
        a[j] = min(b[j], w[i]);
      else {
        a[j] = min(b[j], b[j - p[i]] + w[i]);
        if (a[j] <= G) {
          mx = max(mx, j);
        }
      }
    }
    // cout << "\n";

    for (j = 0; j <= P; ++j) {
      b[j] = a[j];
      //  if (a[j] >= 1000000000)cout << "inf ";
      //  else cout << a[j]<<" ";
      // cout<<"("<<j<<") ";
    }
    // cout<<"|||\n";
  }
  cout << mx;
}

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

using namespace std;

long long N, G, i, j, mx;
long long p[105], P, w[105], a[100005], b[100005];

int main() {
  // ifstream cin("rucsac.in");
  cin >> N >> G;
  for (i = 1; i <= N; i++) {
    cin >> w[i] >> p[i];
    P += p[i];
  }
  for (i = 1; i <= P; i++)
    b[i] = 1000000000;
  // cout << P << " ";
  b[0] = 0;
  for (i = 1; i <= N; ++i) {
    for (j = 0; j <= P; ++j) {
      if (p[i] > j)
        a[j] = min(b[j], w[i]);
      else {
        a[j] = min(b[j], b[j - p[i]] + w[i]);
        if (a[j] <= G) {
          mx = max(mx, j);
        }
      }
    }
    // cout << "\n";

    for (j = 0; j <= P; ++j) {
      b[j] = a[j];
      //  if (a[j] >= 1000000000)cout << "inf ";
      //  else cout << a[j]<<" ";
      // cout<<"("<<j<<") ";
    }
    // cout<<"|||\n";
  }
  cout << mx;
}
Problem p03164 (C++)

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

using namespace std;

int const maxsize = 100005;

int weight[maxsize], cost[maxsize];
long long int dp[103][maxsize];

int main() {
  int n, w;
  cin >> n >> w;
  for (int x = 1; x <= n; x++)
    cin >> weight[x] >> cost[x];
  memset(dp, 63, sizeof(dp));
  int ans = 0;
  // for(int x=0;x<=100000;x++) dp[0][x] = 0;
  // for(int x=0;x<=100;x++) dp[x][0] = 0;
  dp[0][0] = 0;
  for (int x = 1; x <= n; x++) {
    for (int i = 0; i < maxsize; i++) {
      dp[x][i] = dp[x - 1][i];
      if (i - cost[x - 1] >= 0) {
        dp[x][i] = min(dp[x][i], dp[x - 1][i - cost[x]] + weight[x]);
      }
      // dp[x][i] = min(d, dp[x][i]);
    }
  }
  for (int x = 0; x < maxsize; x++)
    if (dp[n][x] <= w)
      ans = max(ans, x);
  cout << ans;
  return 0;
}

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

using namespace std;

int const maxsize = 100005;

int weight[maxsize], cost[maxsize];
long long int dp[103][maxsize];

int main() {
  int n, w;
  cin >> n >> w;
  for (int x = 1; x <= n; x++)
    cin >> weight[x] >> cost[x];
  memset(dp, 63, sizeof(dp));
  int ans = 0;
  // for(int x=0;x<=100000;x++) dp[0][x] = 0;
  // for(int x=0;x<=100;x++) dp[x][0] = 0;
  dp[0][0] = 0;
  for (int x = 1; x <= n; x++) {
    for (int i = 0; i < maxsize; i++) {
      dp[x][i] = dp[x - 1][i];
      if (i - cost[x] >= 0) {
        dp[x][i] = min(dp[x - 1][i], dp[x - 1][i - cost[x]] + weight[x]);
      }
      // dp[x][i] = min(d, dp[x][i]);
    }
  }
  for (int x = 0; x < maxsize; x++)
    if (dp[n][x] <= w)
      ans = max(ans, x);
  cout << ans;
  return 0;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int n, all, w[105], v[105];
long long dp[100105];
int main() {
  memset(dp, 0x3f, sizeof(dp));
  dp[0] = 0;
  scanf("%d%d", &n, &all);
  for (int i = 1; i <= n; ++i)
    scanf("%d%d", &w[i], &v[i]);
  for (int i = 1; i <= n; ++i) {
    for (int j = 100000; j != -1; --j)
      dp[j + v[i]] = min(dp[j + v[i]], dp[j] + (long long)w[i]);
  }
  for (int i = 100000; i != -1; --i)
    if (dp[i] <= all) {
      printf("%d", i);
      break;
    }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int n, all, w[10000005], v[10000005];
long long dp[50010005];
int main() {
  memset(dp, 0x3f, sizeof(dp));
  dp[0] = 0;
  scanf("%d%d", &n, &all);
  for (int i = 1; i <= n; ++i)
    scanf("%d%d", &w[i], &v[i]);
  for (int i = 1; i <= n; ++i) {
    for (int j = 100000; j != -1; --j)
      dp[j + v[i]] = min(dp[j + v[i]], dp[j] + (long long)w[i]);
  }
  for (int i = 100000; i != -1; --i)
    if (dp[i] <= all) {
      printf("%d", i);
      break;
    }
  return 0;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const ll MOD = (ll)(1e9 + 7);
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); (i)++)

int N;
ll W, w[200], v[200];

ll memo[200][200 * 2000 + 1];
ll solve(int n, int V) {
  if (V < 0)
    return INT_MAX;
  if (n == N) {
    if (V == 0)
      return 0LL;
    else
      return INT_MAX;
  }
  if (memo[n][V] != -1)
    return memo[n][V];
  return memo[n][V] = min(solve(n + 1, V), solve(n + 1, V - v[n]) + w[n]);
}

signed main() {
  scanf("%d%d", &W, &N);
  assert(1 <= W && W <= 1000000000);
  assert(1 <= N && N <= 200);
  rep(i, N) {
    scanf("%d%d", w + i, v + i);
    assert(1 <= w[i] && w[i] <= 1000000000);
    assert(1 <= v[i] && v[i] <= 2000);
  }
  memset(memo, -1, sizeof(memo));
  for (int i = 400000; i >= 0; i--) {
    if (solve(0, i) <= W) {
      printf("%d\n", i);
      break;
    }
  }
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const ll MOD = (ll)(1e9 + 7);
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); (i)++)

int N;
ll W, w[200], v[200];

ll memo[200][200 * 2000 + 1];
ll solve(int n, int V) {
  if (V < 0)
    return INT_MAX;
  if (n == N) {
    if (V == 0)
      return 0LL;
    else
      return INT_MAX;
  }
  if (memo[n][V] != -1)
    return memo[n][V];
  return memo[n][V] = min(solve(n + 1, V), solve(n + 1, V - v[n]) + w[n]);
}

signed main() {
  scanf("%d%d", &N, &W);
  assert(1 <= W && W <= 1000000000);
  assert(1 <= N && N <= 200);
  rep(i, N) {
    scanf("%d%d", w + i, v + i);
    assert(1 <= w[i] && w[i] <= 1000000000);
    assert(1 <= v[i] && v[i] <= 2000);
  }
  memset(memo, -1, sizeof(memo));
  for (int i = 400000; i >= 0; i--) {
    if (solve(0, i) <= W) {
      printf("%d\n", i);
      break;
    }
  }
}
Problem p03164 (C++)

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

using namespace std;

int n, m, V;
int w[110];
int v[110];
int f[1010]; // f[i]表示达到i价值的最优w

int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> w[i] >> v[i];
    V += v[i];
  }
  memset(f, 0x3f, sizeof(f));
  f[0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = V; j >= v[i]; j--) {
      f[j] = min(f[j], f[j - v[i]] + w[i]);
    }
  }
  for (int i = V; i >= 1; i--) {
    if (f[i] && f[i] <= m) {
      printf("%d", i);
      return 0;
    }
  }
  return 0;
}

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

using namespace std;

int n, m, V;
int w[1100];
int v[1100];
int f[1010000]; // f[i]表示达到i价值的最优w

int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> w[i] >> v[i];
    V += v[i];
  }
  memset(f, 0x3f, sizeof(f));
  f[0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = V; j >= v[i]; j--) {
      f[j] = min(f[j], f[j - v[i]] + w[i]);
    }
  }
  for (int i = V; i >= 1; i--) {
    if (f[i] && f[i] <= m) {
      printf("%d", i);
      return 0;
    }
  }
  return 0;
}
Problem p03164 (C++)

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

using namespace std;

const long long maxv = 100000;
const long long oo = LLONG_MAX;
long long N, W;
unsigned long long w[101], v[101];
unsigned long long F[101][maxv + 1];

void Enter() {
  // freopen("INPUT.TXT","r",stdin);
  //  freopen("OUTPUT.TXT","w",stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> N >> W;
  for (long long i = 1; i <= N; ++i)
    cin >> w[i] >> v[i];
}

int main() {
  Enter();
  memset(F, 0, sizeof(F));
  for (long long i = 0; i <= N; ++i) {
    for (long long j = 0; j <= maxv; ++j) {
      F[i][j] = oo;
    }
  }
  F[0][0] = 0;
  for (long long i = 0; i <= N; ++i) {
    for (long long j = 0; j <= maxv; ++j) {
      if (F[i][j] == oo)
        continue;
      if (F[i][j] + w[i + 1] <= W)
        F[i + 1][j + v[i + 1]] =
            min(F[i][j] + w[i + 1], F[i + 1][j + v[i + 1]]);
      F[i + 1][j] = min(F[i + 1][j], F[i][j]);
    }
  }
  for (long long j = maxv; j >= 1; --j) {
    if (F[N][j] != oo) {
      cout << j;
      break;
    }
  }
  return 0;
}


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

using namespace std;

const long long maxv = 100000;
const long long oo = LLONG_MAX;
long long N, W;
unsigned long long w[103], v[103];
unsigned long long F[103][maxv + 1];

void Enter() {
  // freopen("INPUT.TXT","r",stdin);
  //  freopen("OUTPUT.TXT","w",stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> N >> W;
  for (long long i = 1; i <= N; ++i)
    cin >> w[i] >> v[i];
}

int main() {
  Enter();
  memset(F, 0, sizeof(F));
  for (long long i = 0; i <= N; ++i) {
    for (long long j = 0; j <= maxv; ++j) {
      F[i][j] = oo;
    }
  }
  F[0][0] = 0;
  for (long long i = 0; i <= N; ++i) {
    for (long long j = 0; j <= maxv; ++j) {
      if (F[i][j] == oo)
        continue;
      if (F[i][j] + w[i + 1] <= W)
        F[i + 1][j + v[i + 1]] =
            min(F[i][j] + w[i + 1], F[i + 1][j + v[i + 1]]);
      F[i + 1][j] = min(F[i + 1][j], F[i][j]);
    }
  }
  for (long long j = maxv; j >= 1; --j) {
    if (F[N][j] != oo) {
      cout << j;
      break;
    }
  }
  return 0;
}
Problem p03164 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma gcc optimize(o3)
#include <bits/stdc++.h>
#define IO                                                                     \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define S second
#define F first
#define ll long long
#define ld long double
#define U unsigned
#define pi acos(-1)

using namespace std;

const ll N = 1e2 + 5, M = 1e5 + 5;

ll w[N], v[N], vis[N][M], vid;
ll n, W, mxV, V;
pair<ll, ll> dp[N][M];

pair<ll, ll> solve(ll ind, ll curV) {
  if (curV > mxV)
    return {-1e9, 1e9};
  if (vis[ind][curV])
    return dp[ind][curV];
  if (ind == n)
    return {0, 0};
  pair<ll, ll> ret = max(make_pair(solve(ind + 1, curV + v[ind]).F + v[ind],
                                   solve(ind + 1, curV + v[ind]).S - w[ind]),
                         solve(ind + 1, curV));
  return dp[ind][curV] = ret;
}

int main() {
  cin >> n >> W;
  for (int i = 0; i < n; i++) {
    cin >> w[i] >> v[i];
    mxV += v[i];
  }
  vid++;
  ll s = 0, e = mxV, mid, best = 0;
  for (int i = 0; i < mxV; i++) {
    pair<ll, ll> xx = solve(0, i);
    if (abs(xx.S) <= W) {
      best = max(best, xx.F);
    }
  }
  cout << best;
  return 0;
}


Fixed submission:
#pragma gcc optimize(o3)
#include <bits/stdc++.h>
#define IO                                                                     \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define S second
#define F first
#define ll long long
#define ld long double
#define U unsigned
#define pi acos(-1)

using namespace std;

const ll N = 1e2 + 5, M = 1e5 + 5;

ll w[N], v[N], vis[N][M], vid;
ll n, W, mxV, V;
pair<ll, ll> dp[N][M];

pair<ll, ll> solve(ll ind, ll curV) {
  if (curV > mxV)
    return {-1e9, 1e9};
  if (vis[ind][curV])
    return dp[ind][curV];
  vis[ind][curV] = 1;
  if (ind == n)
    return {0, 0};
  pair<ll, ll> ret = max(make_pair(solve(ind + 1, curV + v[ind]).F + v[ind],
                                   solve(ind + 1, curV + v[ind]).S - w[ind]),
                         solve(ind + 1, curV));
  return dp[ind][curV] = ret;
}

int main() {
  cin >> n >> W;
  for (int i = 0; i < n; i++) {
    cin >> w[i] >> v[i];
    mxV += v[i];
  }
  vid++;
  ll s = 0, e = mxV, mid, best = 0;
  for (int i = 0; i < mxV; i++) {
    pair<ll, ll> xx = solve(0, i);
    if (abs(xx.S) <= W) {
      best = max(best, xx.F);
    }
  }
  cout << best;
  return 0;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using piii = pair<pii, pii>;
const int INF = 1e9 + 7;
int main() {
  int N, W;
  int w[110], v[110];
  ll dp[110][10010]; // dp[i][j]で価値総和がjになる時の重さ総和min
  cin >> N >> W;
  for (int i = 0; i < 110; ++i) {
    for (int j = 0; j < 100100; ++j) {
      dp[i][j] = INF;
    }
  }
  dp[0][0] = 0;
  for (int i = 0; i < N; ++i) {
    cin >> w[i] >> v[i];
  }
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < 100100; ++j) {
      if (j >= v[i]) {
        dp[i + 1][j] = min(dp[i][j], dp[i][j - v[i]] + w[i]);
      } else {
        dp[i + 1][j] = dp[i][j];
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i <= 100100; ++i) {
    if (dp[N][i] <= W)
      ans = i;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using piii = pair<pii, pii>;
const int INF = 1e9 + 7;
int main() {
  int N;
  ll W;
  ll w[110], v[110];
  ll dp[110][100100]; // dp[i][j]で価値総和がjになる時の重さ総和min
  cin >> N >> W;
  for (int i = 0; i < 110; ++i) {
    for (int j = 0; j < 100100; ++j) {
      dp[i][j] = INF;
    }
  }
  dp[0][0] = 0;
  for (int i = 0; i < N; ++i) {
    cin >> w[i] >> v[i];
  }
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < 100100; ++j) {
      if (j >= v[i]) {
        dp[i + 1][j] = min(dp[i][j], dp[i][j - v[i]] + w[i]);
      } else {
        dp[i + 1][j] = dp[i][j];
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i <= 100100; ++i) {
    if (dp[N][i] <= W)
      ans = i;
  }
  cout << ans << endl;
  return 0;
}
Problem p03164 (C++)

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

long long dp[101][1003];

long long solve(int i, int value) {
  if (value <= 0)
    return 0;
  if (i == n)
    return 1000000000;
  if (dp[i][value] != -1)
    return dp[i][value];

  dp[i][value] = solve(i + 1, value);
  dp[i][value] = min(dp[i][value], solve(i + 1, value - v[i]) + w[i]);
  return dp[i][value];
}

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

  memset(dp, -1, sizeof dp);
  for (int i = v_sum; i >= 0; i--) {
    if (solve(0, i) <= C) {
      cout << i << endl;
      return 0;
    }
  }
}

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

long long dp[101][1000001];

long long solve(int i, int value) {
  if (value <= 0)
    return 0;
  if (i == n)
    return 1000000000;
  if (dp[i][value] != -1)
    return dp[i][value];

  dp[i][value] = solve(i + 1, value);
  dp[i][value] = min(dp[i][value], solve(i + 1, value - v[i]) + w[i]);
  return dp[i][value];
}

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

  memset(dp, -1, sizeof dp);
  for (int i = v_sum; i >= 0; i--) {
    if (solve(0, i) <= C) {
      cout << i << endl;
      return 0;
    }
  }
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long int
const int N = 1e3 + 1;
#define debug(x) cerr << "[(" << __LINE__ << ") " << (#x) << "]: " << x << endl;
const int inf = 1e15 + 7;

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, w;
  cin >> n >> w;

  int wt[n + 1], val[n + 1];
  int V(0);
  wt[0] = val[0] = 0;
  for (int i = 1; i <= n; i++) {
    cin >> wt[i] >> val[i];
    V += val[i];
  }

  std::vector<std::vector<int>> dp(n + 1, std::vector<int>(V + 2, inf));

  // dp(i , j) --> represents minimum weight required to get value j if i items
  // are availiable
  dp[0][0] = 0;
  for (int item = 1; item <= n; item++) {
    dp[item][0] = 0;
  }
  for (int item = 1; item <= n; item++) {
    for (int value = 0; value <= V; value++) {
      if (value == 0)
        dp[item][value] = 0;
      else if (value >= val[item - 1]) {
        dp[item][value] = min(wt[item] + dp[item - 1][value - val[item]],
                              dp[item - 1][value]);
      } else
        dp[item][value] = dp[item - 1][value];
    }
  }

  for (int i = V; i >= 0; i--) {
    if (dp[n][i] <= w) {
      cout << i << ' ';
      return 0;
    }
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long int
const int N = 1e3 + 1;
#define debug(x) cerr << "[(" << __LINE__ << ") " << (#x) << "]: " << x << endl;
const int inf = 1e15 + 7;

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, w;
  cin >> n >> w;

  int wt[n + 1], val[n + 1];
  int V(0);
  wt[0] = val[0] = 0;
  for (int i = 1; i <= n; i++) {
    cin >> wt[i] >> val[i];
    V += val[i];
  }

  std::vector<std::vector<int>> dp(n + 1, std::vector<int>(V + 2, inf));

  // dp(i , j) --> represents minimum weight required to get value j if i items
  // are availiable
  dp[0][0] = 0;
  for (int item = 1; item <= n; item++) {
    dp[item][0] = 0;
  }
  for (int item = 1; item <= n; item++) {
    for (int value = 0; value <= V; value++) {
      if (value == 0)
        dp[item][value] = 0;
      else if (value >= val[item]) {
        dp[item][value] = min(wt[item] + dp[item - 1][value - val[item]],
                              dp[item - 1][value]);
      } else
        dp[item][value] = dp[item - 1][value];
    }
  }

  for (int i = V; i >= 0; i--) {
    if (dp[n][i] <= w) {
      cout << i << ' ';
      return 0;
    }
  }

  return 0;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
#define MAX_N 110
#define MAX_V 100010

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const long long INF = 1LL << 60;

// Input
int N;
long long W, weight[MAX_N], value[MAX_N];

// DP table
long long dp[MAX_N][MAX_V];

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

  // Initialization
  for (int i = 0; i < MAX_N; i++)
    for (int j = 0; j < MAX_V; j++)
      dp[i][j] = INF;

  // Initial state
  dp[0][0] = 0;

  // Update
  for (int i = 0; i < N; i++) {
    for (int sum_v = 0; sum_v < MAX_V; sum_v++) {
      chmin(dp[i + 1][sum_v], dp[i][sum_v - value[i]] + weight[i]);
      chmin(dp[i + 1][sum_v], dp[i][sum_v]);
    }
  }

  // Ans
  long long res = 0;
  for (int sum_v = 0; sum_v < MAX_V; sum_v++) {
    if (dp[N][sum_v] <= W)
      res = sum_v;
  }
  cout << res << endl;
}


Fixed submission:
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
#define MAX_N 110
#define MAX_V 100010

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const long long INF = 1LL << 60;

// Input
int N;
long long W, weight[MAX_N], value[MAX_N];

// DP table
long long dp[MAX_N][MAX_V];

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

  // Initialization
  for (int i = 0; i < MAX_N; i++)
    for (int j = 0; j < MAX_V; j++)
      dp[i][j] = INF;

  // Initial state
  dp[0][0] = 0;

  // Update
  for (int i = 0; i < N; i++) {
    for (int sum_v = 0; sum_v < MAX_V; sum_v++) {
      if (sum_v - value[i] >= 0)
        chmin(dp[i + 1][sum_v], dp[i][sum_v - value[i]] + weight[i]);
      chmin(dp[i + 1][sum_v], dp[i][sum_v]);
    }
  }

  // Ans
  long long res = 0;
  for (int sum_v = 0; sum_v < MAX_V; sum_v++) {
    if (dp[N][sum_v] <= W)
      res = sum_v;
  }
  cout << res << endl;
}
Problem p03164 (C++)

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

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

using namespace std;

int N, V;
long long W, w[110], v[110];
long long dp[110][1010], ans;

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

  cin >> N >> W;

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

  for (int i = 0; i <= N; i++) {
    for (int sum_v = 1; sum_v <= V; sum_v++) {
      dp[i][sum_v] = 1000000010;
    }
  }

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

  ans = 0;

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

  cout << ans << "\n";
}


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

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

using namespace std;

int N, V;
long long W, w[110], v[110];
long long dp[110][100010], ans;

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

  cin >> N >> W;

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

  for (int i = 0; i <= N; i++) {
    for (int sum_v = 1; sum_v <= V; sum_v++) {
      dp[i][sum_v] = 1000000010;
    }
  }

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

  ans = 0;

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

  cout << ans << "\n";
}
Problem p03164 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <cstring>
#include <iostream>
#define ll long long int
#define ii int
#define jmp cout << "\n"
#define vl vector<ll>
#define pb push_back
#define printv(v)                                                              \
  for (auto x : v)                                                             \
    cout << x << " ";                                                          \
  jmp;
#define endll "\n"
#define SORT(v) sort(v.begin(), v.end())
#define REV(x) reverse(x.begin(), x.end())
#define ff first
#define ss second
#define iin insert
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);

using namespace std;
const ll N = 1e5 + 5;
const ll inf = 0x3f3f3f3f;
ll dp[N];
ll w[N], val[N];
void solve() {
  ll n, W;
  cin >> n >> W;
  // ll w[n], val[n];
  for (ll i = 0; i < n; i++)
    cin >> val[i] >> w[i];

  memset(dp, inf, sizeof(dp));
  dp[0] = 0;
  for (ll i = 0; i < n; i++) {
    for (ll j = 1e5; j >= w[i]; j--)
      dp[j] = min(dp[j], (dp[j - w[i]] + val[i]));
  }
  ll ans;
  for (ll i = 1e5; i >= 0; i--) {
    if (dp[i] <= W) {
      ans = i;
      break;
    }
  }
  cout << ans;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  // ll t;
  // cin >> t;
  // while (t--)
  solve();
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>
#define ll long long int
#define ii int
#define jmp cout << "\n"
#define vl vector<ll>
#define pb push_back
#define printv(v)                                                              \
  for (auto x : v)                                                             \
    cout << x << " ";                                                          \
  jmp;
#define endll "\n"
#define SORT(v) sort(v.begin(), v.end())
#define REV(x) reverse(x.begin(), x.end())
#define ff first
#define ss second
#define iin insert
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);

using namespace std;
const ll N = 1e5 + 5;
const ll inf = 0x3f3f3f3f;
ll dp[N];
ll w[N], val[N];
void solve() {
  ll n, W;
  cin >> n >> W;
  // ll w[n], val[n];
  for (ll i = 0; i < n; i++)
    cin >> val[i] >> w[i];

  memset(dp, inf, sizeof(dp));
  dp[0] = 0;
  for (ll i = 0; i < n; i++) {
    for (ll j = 1e5; j >= w[i]; j--)
      dp[j] = min(dp[j], (dp[j - w[i]] + val[i]));
  }
  ll ans;
  for (ll i = 1e5; i >= 0; i--) {
    if (dp[i] <= W) {
      ans = i;
      break;
    }
  }
  cout << ans;
}
int main() {

  solve();
  return 0;
}
Problem p03164 (C++)

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

using namespace std;

#define ll long long int

ll const MOD = 1000000007;
ll const INF = (long long int)1 << 58;

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

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

  vector<vector<ll>> in(n, vector<ll>(2));

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

  vector<vector<ll>> dp(n + 1, vector<ll>(n * (1000) + 1, INF));
  dp[0][0] = 0;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= n * 1000; j++) {
      dp[i + 1][j] = dp[i][j];
    }
    for (int j = 0; j <= n * 1000; j++) {
      dp[i + 1][j + in[i][1]] =
          min(dp[i + 1][j + in[i][1]], dp[i][j] + in[i][0]);
    }
  }

  ll ans = 0;

  for (int i = 0; i < n * 1000 + 1; i++) {
    if (dp[n][i] <= w) {
      ans = i;
    }
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

#define ll long long int

ll const MOD = 1000000007;
ll const INF = (long long int)1 << 58;

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

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

  vector<vector<ll>> in(n, vector<ll>(2));

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

  vector<vector<ll>> dp(n + 1, vector<ll>(n * (1000) + 1, INF));
  dp[0][0] = 0;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= n * 1000; j++) {
      dp[i + 1][j] = dp[i][j];
    }
    for (int j = 0; j <= n * 1000; j++) {
      if (j + in[i][1] <= n * 1000) {
        dp[i + 1][j + in[i][1]] =
            min(dp[i + 1][j + in[i][1]], dp[i][j] + in[i][0]);
      }
    }
  }

  ll ans = 0;

  for (int i = 0; i < n * 1000 + 1; i++) {
    if (dp[n][i] <= w) {
      ans = i;
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define drep(i, a, b) for (int(i) = (a); (i) >= (b); (i)--)
#define MAGICLINE ios_base::sync_with_stdio(0)
#define pb push_back
#define mp make_pair
#define se second
#define fs first
using namespace std;
typedef long long LL;
typedef long double LD;
typedef pair<int, int> PII;
typedef pair<LL, int> PLI;
typedef priority_queue<int> PQI;
typedef priority_queue<LL> PQL;
typedef priority_queue<PLI, vector<PLI>, greater<PLI>> dijQ;

const int MN = 1002;
const LL INF = 1e18 + 3;
LL T[MN][2];
int val[MN], weigh[MN];

void prefill(int m) {
  rep(i, 1, m) {
    T[i][0] = INF;
    T[i][1] = INF;
  }
}

void Move(int m) {
  rep(i, 0, m) {
    T[i][0] = min(T[i][0], T[i][1]);
    T[i][1] = INF;
  }
}

int main() {
  MAGICLINE;
  int n, m;
  scanf("%d %d", &n, &m);
  prefill(MN);
  rep(i, 1, n + 1) scanf("%d %d", &weigh[i], &val[i]);
  rep(j, 1, n + 1) {
    rep(i, 1, MN) if (T[i - val[j]][0] + weigh[j] <= m) T[i][1] =
        min(T[i][1], T[i - val[j]][0] + weigh[j]);
    Move(MN);
  }

  int ans = -1;
  rep(i, 0, MN) if (T[i][0] != INF) ans = i;
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define drep(i, a, b) for (int(i) = (a); (i) >= (b); (i)--)
#define MAGICLINE ios_base::sync_with_stdio(0)
#define pb push_back
#define mp make_pair
#define se second
#define fs first
using namespace std;
typedef long long LL;
typedef long double LD;
typedef pair<int, int> PII;
typedef pair<LL, int> PLI;
typedef priority_queue<int> PQI;
typedef priority_queue<LL> PQL;
typedef priority_queue<PLI, vector<PLI>, greater<PLI>> dijQ;

const int MN = 100002;
const LL INF = 1e18 + 3;
LL T[MN][2];
int val[MN], weigh[MN];

void prefill(int m) {
  rep(i, 1, m) {
    T[i][0] = INF;
    T[i][1] = INF;
  }
}

void Move(int m) {
  rep(i, 0, m) {
    T[i][0] = min(T[i][0], T[i][1]);
    T[i][1] = INF;
  }
}

int main() {
  MAGICLINE;
  int n, m;
  scanf("%d %d", &n, &m);
  prefill(MN);
  rep(i, 1, n + 1) scanf("%d %d", &weigh[i], &val[i]);
  rep(j, 1, n + 1) {
    rep(i, 1, MN) if (T[i - val[j]][0] + weigh[j] <= m) T[i][1] =
        min(T[i][1], T[i - val[j]][0] + weigh[j]);
    Move(MN);
  }

  int ans = -1;
  rep(i, 0, MN) if (T[i][0] != INF) ans = i;
  printf("%d\n", ans);
  return 0;
}
Problem p03164 (C++)

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

int main() {

  // fastio
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

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

  ll n, W;
  cin >> n >> W;
  ll weg[n];
  ll val[n];
  ll sum = 0;
  for (ll i = 0; i < n; i++) {
    cin >> weg[i] >> val[i];
    sum += val[i];
  }

  vector<vector<ll>> dp(n + 1, vector<ll>(sum + 1, 1000000000));
  dp[0][0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= sum; j++) {
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
      if (j >= val[i])
        dp[i + 1][j] = min(dp[i + 1][j], dp[i][j - val[i]] + weg[i]);
    }
  }
  /*
  for(int i=0;i<=n;i++){
          for(int j=0;j<=sum;j++){
                  cout<<dp[i][j]<<" ";
          }
          cout<<endl;
  }
  //*/
  ll ans = 0;
  for (int i = 0; i <= sum; i++) {
    if (dp[n][i] <= W) {
      ans = i;
    }
  }
  cout << ans << endl;

  return 0;
}

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

int main() {

  // fastio
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n, W;
  cin >> n >> W;
  ll weg[n];
  ll val[n];
  ll sum = 0;
  for (ll i = 0; i < n; i++) {
    cin >> weg[i] >> val[i];
    sum += val[i];
  }

  vector<vector<ll>> dp(n + 1, vector<ll>(sum + 1, 1000000000));
  dp[0][0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= sum; j++) {
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
      if (j >= val[i])
        dp[i + 1][j] = min(dp[i + 1][j], dp[i][j - val[i]] + weg[i]);
    }
  }
  /*
  for(int i=0;i<=n;i++){
          for(int j=0;j<=sum;j++){
                  cout<<dp[i][j]<<" ";
          }
          cout<<endl;
  }
  //*/
  ll ans = 0;
  for (int i = 0; i <= sum; i++) {
    if (dp[n][i] <= W) {
      ans = i;
    }
  }
  cout << ans << endl;

  return 0;
}