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

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

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define Rep(i, sta, n) for (int i = sta; i < (n); ++i)
#define all(x) (x).begin(), (x).end()

using namespace std;
typedef long long ll;
const int mod = 1000000007;
const int INF = 1e9;
const ll LINF = 1e18;

ll dp[101][100100];

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

  vector<ll> w(n);
  vector<int> v(n);
  int MAXV = 0;
  rep(i, n) {
    cin >> w[i] >> v[i];
    MAXV += v[i];
  }
  rep(i, n + 1) rep(j, MAXV + 1) { dp[i][j] = LINF; }
  dp[0][0] = 0;
  rep(i, n + 1) {
    rep(j, MAXV + 1) {
      dp[i + 1][j] = dp[i][j];
      if (j - v[i] >= 0)
        dp[i + 1][j] = min(dp[i + 1][j], dp[i][j - v[i]] + w[i]);
    }
  }
  int ans = 0;
  rep(i, n + 1) {
    rep(j, MAXV + 1) {
      // cout << "i: " << i << " j: " << j<< " dp: " << dp[i][j] << endl;
      if (dp[i][j] <= W)
        ans = max(ans, j);
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define Rep(i, sta, n) for (int i = sta; i < (n); ++i)
#define all(x) (x).begin(), (x).end()

using namespace std;
typedef long long ll;
const int mod = 1000000007;
const int INF = 1e9;
const ll LINF = 1e18;

ll dp[105][100100];

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

  vector<ll> w(n);
  vector<int> v(n);
  int MAXV = 0;
  rep(i, n) {
    cin >> w[i] >> v[i];
    MAXV += v[i];
  }
  rep(i, n + 1) rep(j, MAXV + 1) { dp[i][j] = LINF; }
  dp[0][0] = 0;
  rep(i, n + 1) {
    rep(j, MAXV + 1) {
      dp[i + 1][j] = dp[i][j];
      if (j - v[i] >= 0)
        dp[i + 1][j] = min(dp[i + 1][j], dp[i][j - v[i]] + w[i]);
    }
  }
  int ans = 0;
  rep(i, n + 1) {
    rep(j, MAXV + 1) {
      // cout << "i: " << i << " j: " << j<< " dp: " << dp[i][j] << endl;
      if (dp[i][j] <= W)
        ans = max(ans, j);
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03164 (C++)

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

ll INF = 100100100100100100;
int main() {
  // input
  int n;
  int w;
  cin >> n >> w;
  vector<int> weights(n + 1);
  vector<int> values(n + 1);

  ll max_value_sum = 0;
  ll max_weight_sum = 0;
  rep(i, n) {
    cin >> weights[i + 1]; // weight
    cin >> values[i + 1];  // values
    max_value_sum += values[i + 1];
    max_weight_sum += weights[i + 1];
  }

  // dp
  vector<vector<ll>> dp(n + 1, vector<ll>(max_value_sum + 1));
  rep(i, n + 1) rep(v, max_value_sum + 1) dp[i][v] = INF;

  dp[0][0] = 0;

  for (int i = 0; i < n; i++) {
    for (ll v = 0; v <= max_value_sum; v++) {
      if (values[i] <= v)
        dp[i + 1][v] = min(dp[i][v], dp[i][v - values[i + 1]] + weights[i + 1]);
      else
        dp[i + 1][v] = dp[i][v];
    }
  }

  // output
  ll ans = 0;
  for (ll v = 0; v <= max_value_sum; v++) {
    if (dp[n][v] <= w) {
      ans = max(ans, (ll)v);
    }
  }

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

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, x, n) for (int i = x; i < (n); i++)
#define ALL(n) begin(n), end(n)
using namespace std;
using P = pair<int, int>;
using ll = long long;

ll INF = 100100100100100100;
int main() {
  // input
  int n;
  int w;
  cin >> n >> w;
  vector<int> weights(n + 1);
  vector<int> values(n + 1);

  ll max_value_sum = 0;
  ll max_weight_sum = 0;
  rep(i, n) {
    cin >> weights[i + 1]; // weight
    cin >> values[i + 1];  // values
    max_value_sum += values[i + 1];
    max_weight_sum += weights[i + 1];
  }

  // dp
  vector<vector<ll>> dp(n + 1, vector<ll>(max_value_sum + 1));
  rep(i, n + 1) rep(v, max_value_sum + 1) dp[i][v] = INF;

  dp[0][0] = 0;

  for (int i = 0; i < n; i++) {
    for (ll v = 0; v <= max_value_sum; v++) {
      if (values[i + 1] <= v)
        dp[i + 1][v] = min(dp[i][v], dp[i][v - values[i + 1]] + weights[i + 1]);
      else
        dp[i + 1][v] = dp[i][v];
    }
  }

  // output
  ll ans = 0;
  for (ll v = 0; v <= max_value_sum; v++) {
    if (dp[n][v] <= w) {
      ans = max(ans, (ll)v);
    }
  }

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 100100100;
const ll INFLL = 100100100100100100;
const int MOD = (int)1e9 + 7;
const ll MODLL = (ll)1e9 + 7;
const double EPS = 1e-9;

int main() {

  int n, w;
  cin >> n >> w;
  vector<ll> c(n);
  vector<ll> v(n);
  REP(i, n) cin >> c[i] >> v[i];

  vector<vector<ll>> dp(n + 1, vector<ll>(100100, INFLL));
  dp[0][0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 100100; j++) {
      if (dp[i][j] != INFLL) {
        dp[i + 1][j] = dp[i][j];
      }
      if (dp[i][j - v[i]] != INFLL && j - v[i] >= 0) {
        dp[i + 1][j] = min(dp[i + 1][j], dp[i][j - v[i]] + c[i]);
      }
    }
  }

  int ans = 0;
  for (int j = 0; j < 100100; j++) {
    if (dp[n][j] <= w) {
      ans = j;
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 100100100;
const ll INFLL = 100100100100100100;
const int MOD = (int)1e9 + 7;
const ll MODLL = (ll)1e9 + 7;
const double EPS = 1e-9;

int main() {

  int n, w;
  cin >> n >> w;
  vector<ll> c(n);
  vector<ll> v(n);
  REP(i, n) cin >> c[i] >> v[i];

  vector<vector<ll>> dp(n + 1, vector<ll>(100100, INFLL));
  dp[0][0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 100100; j++) {
      if (dp[i][j] != INFLL) {
        dp[i + 1][j] = dp[i][j];
      }
      if (j - v[i] >= 0 && dp[i][j - v[i]] != INFLL) {
        dp[i + 1][j] = min(dp[i + 1][j], dp[i][j - v[i]] + c[i]);
      }
    }
  }

  int ans = 0;
  for (int j = 0; j < 100100; j++) {
    if (dp[n][j] <= w) {
      ans = j;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define N 100
#define V 1000
int v[N + 1], w[N + 1], dp[2][N * V + 1], Sum[N + 1];
signed main() {
  int n, m, i;
  scanf("%lld%lld", &n, &m);
  for (i = 1; i <= n; i++)
    scanf("%lld%lld", w + i, v + i), Sum[i] = Sum[i - 1] + v[i];
  memset(dp, 0x3f, sizeof(dp));
  dp[0][0] = 0;
  for (i = 1; i <= n; i++)
    for (int j = 0; j <= Sum[i]; j++)
      dp[i & 1][j] = min(dp[i - 1 & 1][j], dp[i - 1 & 1][j - v[i]] + w[i]);
  for (i = Sum[n];; i--)
    if (dp[n & 1][i] <= m)
      return printf("%lld\n", i), 0;
}
/*1
3 8
3 30
4 50
5 60
*/
/*2
1 1000000000
1000000000 10
*/
/*3
6 15
6 5
5 6
6 4
6 6
3 5
7 2
*/

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define N 100
#define V 1000
int v[N + 1], w[N + 1], dp[2][N * V + 1], Sum[N + 1];
signed main() {
  int n, m, i;
  scanf("%lld%lld", &n, &m);
  for (i = 1; i <= n; i++)
    scanf("%lld%lld", w + i, v + i), Sum[i] = Sum[i - 1] + v[i];
  memset(dp, 0x3f, sizeof(dp));
  dp[0][0] = 0;
  for (i = 1; i <= n; i++)
    for (int j = 0; j <= Sum[i]; j++)
      dp[i & 1][j] =
          min(dp[i - 1 & 1][j],
              (j >= v[i] ? dp[i - 1 & 1][j - v[i]] + w[i] : 0x3f3f3f3f));
  for (i = Sum[n];; i--)
    if (dp[n & 1][i] <= m)
      return printf("%lld\n", i), 0;
}
/*1
3 8
3 30
4 50
5 60
*/
/*2
1 1000000000
1000000000 10
*/
/*3
6 15
6 5
5 6
6 4
6 6
3 5
7 2
*/
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define smax(a, b) ((a) < (b) ? ((a) = (b), true) : false)
#define smin(a, b) ((a) > (b) ? ((a) = (b), true) : false)
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
using namespace std;
typedef long long ll;

const ll MAX = 1e18;
ll n, w;
ll item[100][2];
ll dp[100][100001];
int main() {
  cin >> n >> w;
  ll tval = 0;
  for (int i = 1; i <= n; i++) {
    // value, weight
    cin >> item[i][1] >> item[i][0];
    tval += item[i][0];
  }

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

  dp[0][0] = 0;

  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= tval; j++) {
      dp[i][j] = dp[i - 1][j];
    }
    for (int j = 0; j <= tval; j++) {
      if (dp[i - 1][j] != MAX) {
        dp[i][j + item[i][0]] =
            min(dp[i][j + item[i][0]], dp[i - 1][j] + item[i][1]);
      }
    }
  }

  ll ans = MAX;
  for (int i = 0; i <= tval; i++) {
    if (dp[n][i] <= w)
      ans = i;
  }

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

Fixed submission:
#include <bits/stdc++.h>
#define smax(a, b) ((a) < (b) ? ((a) = (b), true) : false)
#define smin(a, b) ((a) > (b) ? ((a) = (b), true) : false)
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
using namespace std;
typedef long long ll;

const ll MAX = 1e18;
ll n, w;
ll item[101][2];
ll dp[101][100001];
int main() {
  cin >> n >> w;
  ll tval = 0;
  for (int i = 1; i <= n; i++) {
    // value, weight
    cin >> item[i][1] >> item[i][0];
    tval += item[i][0];
  }

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

  dp[0][0] = 0;

  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= tval; j++) {
      dp[i][j] = dp[i - 1][j];
    }
    for (int j = 0; j <= tval; j++) {
      if (dp[i - 1][j] != MAX) {
        dp[i][j + item[i][0]] =
            min(dp[i][j + item[i][0]], dp[i - 1][j] + item[i][1]);
      }
    }
  }

  ll ans = MAX;
  for (int i = 0; i <= tval; 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 lp(i, a, b) for (int i = a; i < b; i++)
#define ll long long
#define ff first
#define ss second

const int maxn = 10;
const int maxv = 100;
const int inf = 1e9 + 10;

using namespace std;

ll dp[maxn * maxv];
pair<ll, ll> v[maxn];
int ans;

int main() {
  lp(i, 1, maxn * maxv) dp[i] = inf;
  dp[0] = 0;

  int n, w;

  scanf("%d%d", &n, &w);
  lp(i, 0, n) scanf("%lld%lld", &v[i].ff, &v[i].ss);

  lp(i, 0, n) {
    for (int j = ans + v[i].ss; j >= v[i].ss; j--) {
      if (dp[j - v[i].ss] + v[i].ff <= w) {
        dp[j] = min(dp[j], dp[j - v[i].ss] + v[i].ff), ans = max(ans, j);
      }
    }
  }
  printf("%d\n", ans);
}

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

#define lp(i, a, b) for (int i = a; i < b; i++)
#define ll long long
#define ff first
#define ss second

const int maxn = 105;
const int maxv = 1005;
const int inf = 1e9 + 10;

using namespace std;

ll dp[maxn * maxv];
pair<ll, ll> v[maxn];
int ans;

int main() {
  lp(i, 1, maxn * maxv) dp[i] = inf;
  dp[0] = 0;

  int n, w;

  scanf("%d%d", &n, &w);
  lp(i, 0, n) scanf("%lld%lld", &v[i].ff, &v[i].ss);

  lp(i, 0, n) {
    for (int j = ans + v[i].ss; j >= v[i].ss; j--) {
      if (dp[j - v[i].ss] + v[i].ff <= w) {
        dp[j] = min(dp[j], dp[j - v[i].ss] + v[i].ff), ans = max(ans, j);
      }
    }
  }
  printf("%d\n", ans);
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define ll long long int
#define ull unsigned long long int
#define ld long double
#define mod 1000000007
#define inf 1000000000000000007
#define eps 0.000001
#define pi 3.141592653589793
#define pii pair<int, int>
#define pdd pair<ld, ld>
#define pll pair<ll, ll>
#define ff first
#define ss second
#define vii vector<int>
#define vpl vector<pll>
#define vll vector<ll>
#define sti stack<int>
#define stll stack<ll>
#define mseti set<ll>
#define msetd multiset<ll, greater<ll>>
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define stp setprecision(20) // use fixed before stp
#define endl '\n'
int main() {
  FAST
#ifndef ONLINE_JUDGE
      freopen("input.txt", "rt", stdin);
  freopen("output.txt", "wt", stdout);
#endif
  ll n, w;
  cin >> n >> w;
  pll p[n];
  for (ll i = 0; i < n; i++) {
    cin >> p[i].ff >> p[i].ss;
  }
  ll dp[100001];
  for (ll i = 1; i <= 100000; i++) {
    dp[i] = inf;
  }
  dp[0] = 0;
  for (ll i = 0; i < n; i++) {
    for (ll j = (100000 - p[i].ss); j >= 0; j--) {
      if ((dp[j] + p[i].ff) < dp[j + p[i].ss]) {
        dp[j + p[i].ss] = (dp[j] + p[i].ff);
      }
    }
  }
  ll ans = 0;
  for (ll i = 100000; i > 0; i--) {
    if (dp[i] <= w) {
      ans = i;
      break;
    }
  }
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define ll long long int
#define ull unsigned long long int
#define ld long double
#define mod 1000000007
#define inf 1000000000000000007
#define eps 0.000001
#define pi 3.141592653589793
#define pii pair<int, int>
#define pdd pair<ld, ld>
#define pll pair<ll, ll>
#define ff first
#define ss second
#define vii vector<int>
#define vpl vector<pll>
#define vll vector<ll>
#define sti stack<int>
#define stll stack<ll>
#define mseti set<ll>
#define msetd multiset<ll, greater<ll>>
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define stp setprecision(20) // use fixed before stp
#define endl '\n'
int main() {
  FAST ll n, w;
  cin >> n >> w;
  pll p[n];
  for (ll i = 0; i < n; i++) {
    cin >> p[i].ff >> p[i].ss;
  }
  ll dp[100001];
  for (ll i = 1; i <= 100000; i++) {
    dp[i] = inf;
  }
  dp[0] = 0;
  for (ll i = 0; i < n; i++) {
    for (ll j = (100000 - p[i].ss); j >= 0; j--) {
      if ((dp[j] + p[i].ff) < dp[j + p[i].ss]) {
        dp[j + p[i].ss] = (dp[j] + p[i].ff);
      }
    }
  }
  ll ans = 0;
  for (ll i = 100000; i > 0; i--) {
    if (dp[i] <= w) {
      ans = i;
      break;
    }
  }
  cout << ans;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define inf (ll)1e10 + 1
#define d(x) cerr << #x << "=" << x << endl;

#define MAX 1000001

ll n, dp[MAX], W, maxv, v, w;

int main(void) {
  cin >> n >> W;
  rep(i, MAX) dp[i] = inf;
  dp[0] = 0;
  rep(i, n) {
    cin >> w >> v;
    for (int j = MAX - 1; j >= 0; j--)
      dp[j + v] = min(dp[j + v], dp[j] + w);
  }
  rep(i, MAX) if (dp[i] <= W) maxv = i;
  cout << maxv << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define inf (ll)1e10 + 1
#define d(x) cerr << #x << "=" << x << endl;

#define MAX 1000001

ll n, dp[MAX], W, maxv, v, w;

int main(void) {
  cin >> n >> W;
  rep(i, MAX) dp[i] = inf;
  dp[0] = 0;
  rep(i, n) {
    cin >> w >> v;
    for (int j = MAX - v; j >= 0; j--)
      dp[j + v] = min(dp[j + v], dp[j] + w);
  }
  rep(i, MAX) if (dp[i] <= W) maxv = i;
  cout << maxv << endl;
  return 0;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = 1e9 + 5;
const ll INFLL = 1e18l + 5;

int main() {
  int n, W;
  cin >> n >> W;
  vector<ll> dp(100001, INFLL); // dp[i] min weight at value = i
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    int w, v;
    cin >> w >> v;

    for (int va = W - w; va >= 0; --va) {
      dp[va + v] = min(dp[va + v], dp[va] + w);
    }
  }

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

  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = 1e9 + 5;
const ll INFLL = 1e18l + 5;

int main() {
  int n, W;
  cin >> n >> W;
  vector<ll> dp(100001, INFLL); // dp[i] min weight at value = i
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    int w, v;
    cin >> w >> v;

    for (int va = 100001 - v; va >= 0; --va) {
      dp[va + v] = min(dp[va + v], dp[va] + w);
    }
  }

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

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <memory.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long
#define ppi pair<int, int>
#define mp make_pair
#define inf 2e16
const int N = 2e5 + 11, K = 20, S = 1e3 + 11;
const int mod = 1e9 + 7;
ll dp[N][101];
int weg[N], v[N];
void clear() {
  for (int i = 0; i < N; i++)
    for (int j = 0; j < 101; j++)
      dp[i][j] = inf;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("inp.txt", "r", stdin);
#endif
  clear();
  int n, w;
  cin >> n >> w;
  for (int i = 0; i < n; i++)
    scanf("%d%d", weg + i, v + i);
  int mx = (w >= weg[0] ? v[0] : 0);
  dp[v[0]][0] = weg[0];
  for (int i = 1; i < n; i++) {
    dp[v[i]][i] = weg[i];
    for (int j = 0; j < N; j++) {
      dp[j][i] = min(dp[j][i], dp[j][i - 1]);
      if (j >= v[i]) {
        dp[j][i] = min(dp[j][i], dp[j - v[i]][i - 1] + weg[i]);
      }
      if (dp[j][i] <= w) {
        mx = max(mx, j);
      }
    }
  }
  cout << mx;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <memory.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long
#define ppi pair<int, int>
#define mp make_pair
#define inf 2e16
const int N = 2e5 + 11, K = 20, S = 1e3 + 11;
const int mod = 1e9 + 7;
ll dp[N][101];
int weg[N], v[N];
void clear() {
  for (int i = 0; i < N; i++)
    for (int j = 0; j < 101; j++)
      dp[i][j] = inf;
}
int main() {
#ifndef ONLINE_JUDGE
  // freopen("inp.txt", "r", stdin);
#endif
  clear();
  int n, w;
  cin >> n >> w;
  for (int i = 0; i < n; i++)
    scanf("%d%d", weg + i, v + i);
  int mx = (w >= weg[0] ? v[0] : 0);
  dp[v[0]][0] = weg[0];
  for (int i = 1; i < n; i++) {
    dp[v[i]][i] = weg[i];
    for (int j = 0; j < N; j++) {
      dp[j][i] = min(dp[j][i], dp[j][i - 1]);
      if (j >= v[i]) {
        dp[j][i] = min(dp[j][i], dp[j - v[i]][i - 1] + weg[i]);
      }
      if (dp[j][i] <= w) {
        mx = max(mx, j);
      }
    }
  }
  cout << mx;
  return 0;
}
Problem p03164 (C++)

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

#define int long long

using namespace std;

int d[101][100001];

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

  //  freopen("input.txt", "r", stdin);
  //  freopen("output.txt", "w", stdout);

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

  for (int i = 0; i <= n; i++)
    for (int j = 1; j <= 100000; j++)
      d[i][j] = INT_MAX;

  for (int i = 1; i <= n; i++) {
    int a, b;
    cin >> a >> b;
    for (int j = 1; j <= 100000; j++)
      d[i][j] = min(d[i - 1][j], d[i - 1][j - b] + a);
  }

  for (int j = 100000; j >= 0; j--) {
    if (d[n][j] <= w) {
      cout << j;
      return 0;
    }
  }

  return 0;
}


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

#define int long long

using namespace std;

int d[101][100001];

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

  //  freopen("input.txt", "r", stdin);
  //  freopen("output.txt", "w", stdout);

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

  for (int i = 0; i <= n; i++)
    for (int j = 1; j <= 100000; j++)
      d[i][j] = INT_MAX;

  for (int i = 1; i <= n; i++) {
    int a, b;
    cin >> a >> b;
    for (int j = 1; j <= 100000; j++)
      d[i][j] = min(d[i - 1][j], (j - b >= 0 ? d[i - 1][j - b] + a : INT_MAX));
  }

  for (int j = 100000; j >= 0; j--) {
    if (d[n][j] <= w) {
      cout << j;
      return 0;
    }
  }

  return 0;
}
Problem p03164 (C++)

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

const int MAX = 10005;
vector<long long int> S(MAX, 10000000000), A(MAX), B(MAX);
int N, W;

void DP(int u) {
  for (int j = 1; j <= u; j++) {
    for (int i = N * 1000; i >= 0; i--) {
      if (i == 0 || (S[i] != 10000000000 && S[i] + A[j] <= W))
        S[B[j] + i] = min(S[B[j] + i], S[i] + A[j]);
    }
  }
  int ans = 0;
  for (int i = 0; i <= N * 1000; i++) {
    if (S[i] != 10000000000)
      ans = i;
  }
  cout << ans << endl;
}

int main() {

  cin >> N >> W;
  for (int i = 0; i < N; i++) {
    cin >> A[i + 1] >> B[i + 1];
  }
  S[0] = 0;
  DP(N);
}


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

const int MAX = 100005;
vector<long long int> S(MAX, 10000000000), A(MAX), B(MAX);
int N, W;

void DP(int u) {
  for (int j = 1; j <= u; j++) {
    for (int i = N * 1000; i >= 0; i--) {
      if (i == 0 || (S[i] != 10000000000 && S[i] + A[j] <= W))
        S[B[j] + i] = min(S[B[j] + i], S[i] + A[j]);
    }
  }
  int ans = 0;
  for (int i = 0; i <= N * 1000; i++) {
    if (S[i] != 10000000000)
      ans = i;
  }
  cout << ans << endl;
}

int main() {

  cin >> N >> W;
  for (int i = 0; i < N; i++) {
    cin >> A[i + 1] >> B[i + 1];
  }
  S[0] = 0;
  DP(N);
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define rep2(i, x, n) for (int i = x, i##_len = (n); i < i##_len; ++i)
#define all(n) begin(n), end(n)
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
vi dir = {-1, 0, 1, 0, -1, -1, 1, 1, -1};

int main() {
  int n, w;
  cin >> n >> w;
  vi wei(n), val(n);
  rep(i, n) cin >> wei[i] >> val[i];
  int vmax = n * 1000;
  vector<vi> dp(n + 1, vi(vmax + 1, w + 1));
  dp[0][0] = 0;
  rep(i, n) rep(j, vmax + 1) {
    dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
    dp[i + 1][j + val[i]] = min(dp[i + 1][j + val[i]], dp[i][j] + wei[i]);
  }
  int ans = 0;
  rep(j, vmax + 1) if (dp[n][j] <= w) ans = j;
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define rep2(i, x, n) for (int i = x, i##_len = (n); i < i##_len; ++i)
#define all(n) begin(n), end(n)
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
vi dir = {-1, 0, 1, 0, -1, -1, 1, 1, -1};

int main() {
  int n, w;
  cin >> n >> w;
  vi wei(n), val(n);
  rep(i, n) cin >> wei[i] >> val[i];
  int vmax = n * 1000;
  vector<vi> dp(n + 1, vi(vmax + 1, w + 1));
  dp[0][0] = 0;
  rep(i, n) rep(j, vmax + 1) {
    dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
    if (j + val[i] <= vmax)
      dp[i + 1][j + val[i]] = min(dp[i + 1][j + val[i]], dp[i][j] + wei[i]);
  }
  int ans = 0;
  rep(j, vmax + 1) if (dp[n][j] <= w) ans = j;
  cout << ans << endl;
}
Problem p03164 (C++)

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

#define ll long long

using namespace std;

vector<pair<int, int>> A;

ll relaxed(int i, ll cap) {
  ll result = 0;
  while (i < A.size()) {
    if (A[i].first <= cap) {
      cap -= A[i].first;
      result += A[i].second;
      i++;
    } else {
      // result += (A[i].second * cap + A[i].first - 1) / A[i].first;
      result += A[i].second;
      break;
    }
  }
  return result;
}

ll backtrack(int i, ll cap) {
  if (i >= A.size())
    return 0;
  if (A[i].first > cap)
    return backtrack(i + 1, cap);
  ll a = A[i].second + backtrack(i + 1, cap - A[i].first);
  ll b = relaxed(i + 1, cap);
  if (a >= b)
    return a;
  return max(a, backtrack(i + 1, cap));
}

int main() {
  std::ios::sync_with_stdio(false);
  ll n, w, a, b;
  cin >> n >> w;
  A.resize(n);
  for (int i = 0; i < n; i++)
    cin >> A[i].first >> A[i].second;
  sort(A.begin(), A.end(), [](const pair<ll, ll> &x, const pair<ll, ll> &y) {
    if (x.second * y.first == y.second * x.first)
      return x.first < y.first;
    return x.second * y.first > y.second * x.first;
  });
  cout << backtrack(0, w) << "\n";

  return 0;
}


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

#define ll long long

using namespace std;

vector<pair<int, int>> A;

ll relaxed(int i, ll cap) {
  ll result = 0;
  while (i < A.size()) {
    if (A[i].first <= cap) {
      cap -= A[i].first;
      result += A[i].second;
      i++;
    } else {
      result += (A[i].second * cap + A[i].first - 1) / A[i].first;
      // result += A[i].second;
      break;
    }
  }
  return result;
}

ll backtrack(int i, ll cap) {
  if (i >= A.size())
    return 0;
  if (A[i].first > cap)
    return backtrack(i + 1, cap);
  ll a = A[i].second + backtrack(i + 1, cap - A[i].first);
  ll b = relaxed(i + 1, cap);
  if (a >= b)
    return a;
  return max(a, backtrack(i + 1, cap));
}

int main() {
  std::ios::sync_with_stdio(false);
  ll n, w, a, b;
  cin >> n >> w;
  A.resize(n);
  for (int i = 0; i < n; i++)
    cin >> A[i].first >> A[i].second;
  sort(A.begin(), A.end(), [](const pair<ll, ll> &x, const pair<ll, ll> &y) {
    if (x.second * y.first == y.second * x.first)
      return x.first < y.first;
    return x.second * y.first > y.second * x.first;
  });
  cout << backtrack(0, w) << "\n";

  return 0;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
inline void chmin(int &a, int b) {
  if (a > b)
    a = b;
}
const int MAX_V = 1e7 + 10;
const int INF = 1e9 + 10;

int main() {
  int n, w;
  cin >> n >> w;
  vector<int> we(n), va(n);
  rep(i, n) cin >> we[i] >> va[i];
  vector<vector<int>> dp(n + 1, vector<int>(MAX_V, INF));
  dp[0][0] = 0;
  rep(i, n) {
    rep(j, MAX_V) {
      chmin(dp[i + 1][j], dp[i][j]);
      if (j >= va[i]) {
        chmin(dp[i + 1][j], dp[i][j - va[i]] + we[i]);
      }
    }
  }
  int ans = 0;
  rep(i, MAX_V) {
    if (dp[n][i] <= w)
      ans = i;
  }
  cout << ans << endl;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
inline void chmin(int &a, int b) {
  if (a > b)
    a = b;
}
const int MAX_V = 1e5 + 10;
const int INF = 1e9 + 10;

int main() {
  int n, w;
  cin >> n >> w;
  vector<int> we(n), va(n);
  rep(i, n) cin >> we[i] >> va[i];
  vector<vector<int>> dp(n + 1, vector<int>(MAX_V, INF));
  dp[0][0] = 0;
  rep(i, n) {
    rep(j, MAX_V) {
      chmin(dp[i + 1][j], dp[i][j]);
      if (j >= va[i]) {
        chmin(dp[i + 1][j], dp[i][j - va[i]] + we[i]);
      }
    }
  }
  int ans = 0;
  rep(i, MAX_V) {
    if (dp[n][i] <= w)
      ans = i;
  }
  cout << ans << endl;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long int
#define double long double
#define mod 1000000007
#define w(t)                                                                   \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define f(i, a, n) for (int i = a; i < n; i++)
#define pb push_back
#define pob pop_back
#define mk make_pair
#define ARMY                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
using namespace std;
vector<string> v;
int dp[101][100001];
int solve(int n, int *v, int *w, int wt) {
  for (int i = 0; i <= 100000; i++)
    dp[1][i] = 1e9 + 1;
  dp[1][0] = 0;
  dp[1][v[0]] = w[0];
  for (int i = 2; i <= n; i++) {
    for (int j = 0; j <= 100000; j++) {
      dp[i][j] = dp[i - 1][j];
      if (v[i - 1] > j)
        continue;
      dp[i][j] = min(w[i - 1] + dp[i - 1][j - v[i - 1]], dp[i][j]);
    }
  }
  int ans = 0;
  f(i, 0, 100001) if (dp[n][i] <= wt) ans = i;
  return ans;
}
int32_t main() {
  ARMY w(t) {
    int n, wt;
    cin >> n >> wt;
    int a[n], w[n];
    f(i, 0, n) cin >> w[i] >> a[i];
    cout << solve(n, a, w, wt);
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define int long long int
#define double long double
#define mod 1000000007
#define w(t)                                                                   \
  int t = 1;                                                                   \
  while (t--)
#define f(i, a, n) for (int i = a; i < n; i++)
#define pb push_back
#define pob pop_back
#define mk make_pair
#define ARMY                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
using namespace std;
vector<string> v;
int dp[101][100001];
int solve(int n, int *v, int *w, int wt) {
  for (int i = 0; i <= 100000; i++)
    dp[1][i] = 1e9 + 1;
  dp[1][0] = 0;
  dp[1][v[0]] = w[0];
  for (int i = 2; i <= n; i++) {
    for (int j = 0; j <= 100000; j++) {
      dp[i][j] = dp[i - 1][j];
      if (v[i - 1] > j)
        continue;
      dp[i][j] = min(w[i - 1] + dp[i - 1][j - v[i - 1]], dp[i][j]);
    }
  }
  int ans = 0;
  f(i, 0, 100001) if (dp[n][i] <= wt) ans = i;
  return ans;
}
int32_t main() {
  ARMY w(t) {
    int n, wt;
    cin >> n >> wt;
    int a[n], w[n];
    f(i, 0, n) cin >> w[i] >> a[i];
    cout << solve(n, a, w, wt);
  }
}
Problem p03164 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll a[4][1000000];
ll n, w;
ll dpdp[101][1000000];
ll inf = 1000000000000000;
ll dp(ll x, ll m) {
  // cout << x << m<<" ";
  if (m <= 0) {
    dpdp[x][m] = 0;
    return 0;
  }
  if (x >= n) {
    dpdp[x][m] = inf;
    return inf;
  }
  if (dpdp[x][m] != inf) {

    // cout << "naklsjdf";
    return dpdp[x][m];
  }

  dpdp[x][m] = min(dp(x + 1, m - a[1][x]) + a[0][x], dp(x + 1, m));

  // cout << dpdp[x][m]<<"\n";

  return dpdp[x][m];
}
int main() {
  // ios_base::sync_with_stdio(false);
  // cin.tie(NULL);
  cin >> n >> w;
  for (int i = 0; i < n; ++i) {
    cin >> a[0][i] >> a[1][i];
  }
  for (int i = 0; i < 101; ++i) {
    for (int j = 0; j < 100001; ++j) {
      dpdp[i][j] = inf;
    }
  }
  ll ans = 0;
  for (int i = 0; i < 100001; ++i) {
    if (dp(0, i) <= w) {
      ans = i;
    }
    // cout << dp(0, i)<<" ";
  }
  cout << ans << "\n";

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll a[4][1000000];
ll n, w;
ll dpdp[101][1000000];
ll inf = 1000000000000000;
ll dp(ll x, ll m) {
  // cout << x << m<<" ";
  if (m <= 0) {
    dpdp[x][m] = 0;
    return 0;
  }
  if (x >= n) {
    dpdp[x][m] = 2 * inf;
    return 2 * inf;
  }
  if (dpdp[x][m] != inf) {

    // cout << "naklsjdf";
    return dpdp[x][m];
  }

  dpdp[x][m] = min(dp(x + 1, m - a[1][x]) + a[0][x], dp(x + 1, m));

  // cout << dpdp[x][m]<<"\n";

  return dpdp[x][m];
}
int main() {
  // ios_base::sync_with_stdio(false);
  // cin.tie(NULL);
  cin >> n >> w;
  for (int i = 0; i < n; ++i) {
    cin >> a[0][i] >> a[1][i];
  }
  for (int i = 0; i < 101; ++i) {
    for (int j = 0; j < 100001; ++j) {
      dpdp[i][j] = inf;
    }
  }
  ll ans = 0;
  for (int i = 0; i < 100001; ++i) {
    if (dp(0, i) <= w) {
      ans = i;
    }
    // cout << dp(0, i)<<" ";
  }
  cout << ans << "\n";

  return 0;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, i, j, sum = 0, ans = INT_MIN;
  long long int w;
  long long int a[105][2];
  long long int dp[105][10005];
  cin >> n >> w;
  for (i = 1; i <= n; i++) {
    cin >> a[i][0] >> a[i][1];
    sum += a[i][1];
  }
  for (i = 0; i <= n; i++) {
    for (j = 0; j <= sum; j++)
      dp[i][j] = LLONG_MAX - INT_MAX;
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= sum; j++) {
      if (j <= a[i][1]) {
        dp[i][j] = min(dp[i - 1][j], a[i][0]);
      } else {
        if (dp[i - 1][j - a[i][1]] == INT_MAX)
          dp[i][j] = LLONG_MAX - INT_MAX;
        else {
          dp[i][j] = min(dp[i - 1][j], a[i][0] + dp[i - 1][j - a[i][1]]);
        }
      }
      if (dp[i][j] <= w)
        ans = max(ans, j);
    }
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, i, j, sum = 0, ans = INT_MIN;
  long long int w;
  long long int a[105][2];
  long long int dp[105][100005];
  cin >> n >> w;
  for (i = 1; i <= n; i++) {
    cin >> a[i][0] >> a[i][1];
    sum += a[i][1];
  }
  for (i = 0; i <= n; i++) {
    for (j = 0; j <= sum; j++)
      dp[i][j] = LLONG_MAX - INT_MAX;
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= sum; j++) {
      if (j <= a[i][1]) {
        dp[i][j] = min(dp[i - 1][j], a[i][0]);
      } else {
        if (dp[i - 1][j - a[i][1]] == INT_MAX)
          dp[i][j] = LLONG_MAX - INT_MAX;
        else {
          dp[i][j] = min(dp[i - 1][j], a[i][0] + dp[i - 1][j - a[i][1]]);
        }
      }
      if (dp[i][j] <= w)
        ans = max(ans, j);
    }
  }
  cout << ans;
  return 0;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iomanip>
#include <math.h>
#define rep(i, x) for (ll i = 0; i < x; i++)
#define all(a) (a).begin(), (a).end()
using ll = long long;
using ld = long double;
using namespace std;
const ll INF = 1001001001;
const ll mod = 1000000007;
typedef pair<ll, ll> P;
using graph = vector<vector<ll>>;
ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  if (a % b == 0)
    return b;
  else
    return gcd(b, a % b);
}
bool isp(ll n) {
  bool res = true;
  if (n == 1)
    return false;
  else {
    for (ll i = 2; i * i <= n; i++) {
      if (n % i == 0) {
        res = false;
        break;
      }
    }
    return res;
  }
}
// printf("%.10f\n", N);
/*
ll memo[100000];
ll fibo(ll n){
    if(memo[n] != 0)return memo[n];
    if(n <= 1)return n;
    else return memo[n] = fibo(n - 1)+ fibo(n - 2);
}*/

int main() {
  ll N, W;
  cin >> N >> W;
  vector<ll> weight(N);
  vector<ll> value(N);
  ll wa = 0;
  rep(i, N) {
    cin >> weight[i] >> value[i];
    wa += value[i];
  }
  vector<vector<ll>> memo(N + 1, vector<ll>(wa + 1, INF));
  rep(i, N + 1) { memo[i][0] = 0; }
  rep(i, N) {
    rep(j, wa + 1) {
      if (memo[i + 1][j] < value[i]) {
        memo[i + 1][j] = min(memo[i][j], memo[i][0] + weight[i]);
      } else {
        memo[i + 1][j] = min(memo[i][j], memo[i][j - value[i]] + weight[i]);
      }
    }
  }
  ll ans = 0;
  rep(i, wa + 1) {
    if (memo[N][i] <= W)
      ans = i;
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#include <iomanip>
#include <math.h>
#define rep(i, x) for (ll i = 0; i < x; i++)
#define all(a) (a).begin(), (a).end()
using ll = long long;
using ld = long double;
using namespace std;
const ll INF = 1001001001;
const ll mod = 1000000007;
typedef pair<ll, ll> P;
using graph = vector<vector<ll>>;
ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  if (a % b == 0)
    return b;
  else
    return gcd(b, a % b);
}
bool isp(ll n) {
  bool res = true;
  if (n == 1)
    return false;
  else {
    for (ll i = 2; i * i <= n; i++) {
      if (n % i == 0) {
        res = false;
        break;
      }
    }
    return res;
  }
}
// printf("%.10f\n", N);
/*
ll memo[100000];
ll fibo(ll n){
    if(memo[n] != 0)return memo[n];
    if(n <= 1)return n;
    else return memo[n] = fibo(n - 1)+ fibo(n - 2);
}*/

int main() {
  ll N, W;
  cin >> N >> W;
  vector<ll> weight(N);
  vector<ll> value(N);
  ll wa = 0;
  rep(i, N) {
    cin >> weight[i] >> value[i];
    wa += value[i];
  }
  vector<vector<ll>> memo(N + 1, vector<ll>(wa + 1, INF));
  rep(i, N + 1) { memo[i][0] = 0; }
  rep(i, N) {
    rep(j, wa + 1) {
      if (j < value[i]) {
        memo[i + 1][j] = min(memo[i][j], memo[i][0] + weight[i]);
      } else {
        memo[i + 1][j] = min(memo[i][j], memo[i][j - value[i]] + weight[i]);
      }
    }
  }
  ll ans = 0;
  rep(i, wa + 1) {
    if (memo[N][i] <= W)
      ans = i;
  }
  cout << ans << endl;
}
Problem p03164 (C++)

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

void chmin(ll &a, ll b) {
  if (a > b)
    a = b;
}

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

  int sum_v = accumulate(v.begin(), v.end(), 0);
  vector<vector<ll>> dp(N + 1, vector<ll>(sum_v + 1, 1e9));
  dp[0][0] = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= sum_v; j++) {
      chmin(dp[i + 1][j + v[i]], dp[i][j] + w[i]);
      chmin(dp[i + 1][j], dp[i][j]);
    }
  }

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

  cout << ans << endl;

  return 0;
}

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

void chmin(ll &a, ll b) {
  if (a > b)
    a = b;
}

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

  int sum_v = accumulate(v.begin(), v.end(), 0);
  vector<vector<ll>> dp(N + 1, vector<ll>(sum_v + 1, 1e9));
  dp[0][0] = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= sum_v; j++) {
      if (j + v[i] <= sum_v + 1)
        chmin(dp[i + 1][j + v[i]], dp[i][j] + w[i]);
      chmin(dp[i + 1][j], dp[i][j]);
    }
  }

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

  cout << ans << endl;

  return 0;
}
Problem p03164 (C++)

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

using namespace std;
const int MAXN = 1e5 + 5;
typedef long long ll;
int we[MAXN], val[MAXN];
ll mem[105][MAXN];
ll n, wmax;
ll ans = 0;
ll solve(int i, ll v) {
  if (v < 0)
    return 1e18;
  if (v == 0)
    return 0;
  if (i == n)
    return 1e18;
  ll &ret = mem[i][v];
  ret = min(solve(i + 1, v), solve(i + 1, v - val[i]) + we[i]);

  return ret;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n >> wmax;
  for (int i = 0; i < n; i++)
    cin >> we[i] >> val[i];
  memset(mem, -1, sizeof(mem));

  for (int i = 1; i <= 1e5; ++i) {
    if (solve(0, (ll)i) <= wmax)
      ans = i;
  }
  cout << ans;

  return 0;
}

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

using namespace std;
const int MAXN = 1e5 + 5;
typedef long long ll;
int we[MAXN], val[MAXN];
ll mem[105][MAXN];
ll n, wmax;
ll ans = 0;
ll solve(int i, ll v) {
  if (v < 0)
    return 1e18;
  if (v == 0)
    return 0;
  if (i == n)
    return 1e18;
  ll &ret = mem[i][v];
  if (ret != -1)
    return ret;
  ret = min(solve(i + 1, v), solve(i + 1, v - val[i]) + we[i]);

  return ret;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n >> wmax;
  for (int i = 0; i < n; i++)
    cin >> we[i] >> val[i];
  memset(mem, -1, sizeof(mem));

  for (int i = 1; i <= 1e5; ++i) {
    if (solve(0, (ll)i) <= wmax)
      ans = i;
  }
  cout << ans;

  return 0;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long n, m;
long long w[110], v[110];
long long f[1010];
long long value;
int main() {
  memset(f, 0x3f, sizeof(f));
  scanf("%lld%lld", &n, &m);
  for (long long i = 1; i <= n; i++) {
    scanf("%lld%lld", &w[i], &v[i]);
    value += v[i];
  }
  f[0] = 0;
  for (long long i = 1; i <= n; i++) {
    for (long long j = value; j >= v[i]; j--) {
      f[j] = min(f[j], f[j - v[i]] + w[i]);
    }
  }
  for (long long i = value; i >= 1; i--) {
    if (f[i] <= m) {
      printf("%lld", i);
      return 0;
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long n, m;
long long w[110], v[110];
long long f[100010];
long long value;
int main() {
  memset(f, 0x3f, sizeof(f));
  scanf("%lld%lld", &n, &m);
  for (long long i = 1; i <= n; i++) {
    scanf("%lld%lld", &w[i], &v[i]);
    value += v[i];
  }
  f[0] = 0;
  for (long long i = 1; i <= n; i++) {
    for (long long j = value; j >= v[i]; j--) {
      f[j] = min(f[j], f[j - v[i]] + w[i]);
    }
  }
  for (long long i = value; i >= 1; i--) {
    if (f[i] <= m) {
      printf("%lld", i);
      return 0;
    }
  }
  return 0;
}
Problem p03164 (C++)

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

ll dp[10000];
long long N = 0, W = 0, V = 0, wi[10000], vi[10000];

int main() {

  cin >> N >> W;
  for (int i = 1; i <= N; i++) {
    cin >> wi[i] >> vi[i];
    V += vi[i];
  }

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

  for (int item = 1; item <= N; item++) {
    int w = wi[item], v = vi[item];
    for (int i = V; i >= v; i--) {
      dp[i] = min(dp[i], dp[i - v] + w);
    }
  }

  for (int i = V; i >= 0; i--) {
    if (dp[i] <= W) {
      cout << i << endl;
      break;
    }
  }

  return 0;
}

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

ll dp[1000000];
long long N = 0, W = 0, V = 0, wi[1000000], vi[1000000];

int main() {

  cin >> N >> W;
  for (int i = 1; i <= N; i++) {
    cin >> wi[i] >> vi[i];
    V += vi[i];
  }

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

  for (int item = 1; item <= N; item++) {
    int w = wi[item], v = vi[item];
    for (int i = V; i >= v; i--) {
      dp[i] = min(dp[i], dp[i - v] + w);
    }
  }

  for (int i = V; i >= 0; i--) {
    if (dp[i] <= W) {
      cout << i << endl;
      break;
    }
  }

  return 0;
}
Problem p03164 (C++)

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

////////////// Prewritten code follows. Look down for solution. ////////////////

#define fs first
#define sc second
#define pb push_back
#define len(x) ((int)(x).size())
#define fastio ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long long ll;

const ll LINF = 1e18;
const int INF = 1e9;
const int MOD = 1e9 + 7;

/// command for char arrays with spaces -> scanf(" %[^\n]", text);

////////////////////////// Solution starts below. //////////////////////////////

int main() {

  fastio;
  int n;
  ll p;
  cin >> n >> p;
  int max_value = 0;
  vector<int> valor(n);
  vector<ll> peso(n);
  for (int i = 1; i <= n; i++) {
    cin >> peso[i] >> valor[i];
    max_value += valor[i];
  }
  vector<ll> dp(max_value + 1, LINF);
  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = max_value - valor[i]; j >= 0; j--) {
      dp[j + valor[i]] = min(dp[j + valor[i]], dp[j] + peso[i]);
    }
  }
  int ans = 0;
  for (int i = max_value; i >= 0; i--) {
    if (dp[i] <= p) {
      ans = i;
      break;
    }
  }
  cout << ans << '\n';
  return 0;
}

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

////////////// Prewritten code follows. Look down for solution. ////////////////

#define fs first
#define sc second
#define pb push_back
#define len(x) ((int)(x).size())
#define fastio ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long long ll;

const ll LINF = 1e18;
const int INF = 1e9;
const int MOD = 1e9 + 7;

/// command for char arrays with spaces -> scanf(" %[^\n]", text);

////////////////////////// Solution starts below. //////////////////////////////

int main() {

  fastio;
  int n;
  ll p;
  cin >> n >> p;
  int max_value = 0;
  vector<int> valor(n + 1);
  vector<ll> peso(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> peso[i] >> valor[i];
    max_value += valor[i];
  }
  vector<ll> dp(max_value + 1, LINF);
  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = max_value - valor[i]; j >= 0; j--) {
      dp[j + valor[i]] = min(dp[j + valor[i]], dp[j] + peso[i]);
    }
  }
  int ans = 0;
  for (int i = max_value; i >= 0; i--) {
    if (dp[i] <= p) {
      ans = i;
      break;
    }
  }
  cout << ans << '\n';
  return 0;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define test()                                                                 \
  ll t;                                                                        \
  cin >> t;                                                                    \
  while (t--)
#define MOD 1000000007
#define SPEED                                                                  \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0)

int main() {
  SPEED;
  // CODE
  ll n, W;
  cin >> n >> W;
  ll w[n], v[n];
  ll ans = 0;
  for (ll i = 0; i < n; i++)
    cin >> w[i] >> v[i];
  ll dp[2][100001];
  for (ll i = 0; i < 2; i++) {
    for (ll j = 0; j < 100001; j++) {
      dp[i][j] = 1e9;
    }
  }
  dp[0][0] = 0;
  for (ll i = 1; i <= n; i++) {
    for (ll j = 0; j < 100001; j++) {
      if (dp[0][j] != 10e9) {
        dp[1][j] = min(dp[1][j], dp[0][j]);
        dp[1][j + v[i - 1]] = min(dp[1][j + v[i - 1]], dp[0][j] + w[i - 1]);
      }
    }
    for (ll j = 0; j < 100001; j++) {
      dp[0][j] = dp[1][j];
    }
  }
  for (ll i = 0; i < 100001; i++) {
    if (dp[1][i] <= W) {
      ans = i;
    }
  }
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define test()                                                                 \
  ll t;                                                                        \
  cin >> t;                                                                    \
  while (t--)
#define MOD 1000000007
#define SPEED                                                                  \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0)

int main() {
  SPEED;
  // CODE
  ll n, W;
  cin >> n >> W;
  ll w[n], v[n];
  ll ans = 0;
  for (ll i = 0; i < n; i++)
    cin >> w[i] >> v[i];
  ll dp[2][100001];
  for (ll i = 0; i < 2; i++) {
    for (ll j = 0; j < 100001; j++) {
      dp[i][j] = 10e9;
    }
  }
  dp[0][0] = 0;
  for (ll i = 1; i <= n; i++) {
    for (ll j = 0; j < 100001; j++) {
      if (dp[0][j] != 10e9) {
        dp[1][j] = min(dp[1][j], dp[0][j]);
        dp[1][j + v[i - 1]] = min(dp[1][j + v[i - 1]], dp[0][j] + w[i - 1]);
      }
    }
    for (ll j = 0; j < 100001; j++) {
      dp[0][j] = dp[1][j];
    }
  }
  for (ll i = 0; i < 100001; i++) {
    if (dp[1][i] <= W) {
      ans = i;
    }
  }
  cout << ans;
  return 0;
}