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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second
#define VAL 100000
#define ll long long
#define INF 10000000000000000LL
#define pll pair<long long, long long>
const int MAX = 1e5 + 10;
const int MOD = 1e9 + 7;
const int TOT_PRIMES = 19;
const int MAX_A = 70;
using namespace std;
int main() {
  // ios_base::sync_with_stdio(false);
  //  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("inp.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  ll n, w;
  cin >> n >> w;
  ll tot = 0;
  ll cost[n + 1], wt[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> wt[i] >> cost[i];
    tot = tot + cost[i];
  }
  ll dp[n + 1][tot + 1];
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= tot; j++) {
      dp[i][j] = INF;
    }
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= tot; j++) {
      if (j == 0)
        dp[i][j] = 0;
      else if (i == 1) {
        if (j == cost[i])
          dp[i][j] = wt[i];
        else
          dp[i][j] = INF;
      } else {
        if (j - cost[i] >= 0)
          dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - cost[i]] + wt[i]);
        else
          dp[i][j] = dp[i - 1][j];
      }
    }
  }
  for (int i = tot; i >= 0; i--) {
    //	cout<<dp[n][i]<<" ";
    if (dp[n][i] <= w) {
      cout << i;
      return 0;
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second
#define VAL 100000
#define ll long long
#define INF 10000000000000000LL
#define pll pair<long long, long long>
const int MAX = 1e5 + 10;
const int MOD = 1e9 + 7;
const int TOT_PRIMES = 19;
const int MAX_A = 70;
using namespace std;
int main() {
  // ios_base::sync_with_stdio(false);
  //  cin.tie(NULL);
  //	#ifndef ONLINE_JUDGE
  //	freopen("inp.txt" ,"r", stdin );
  //	freopen("out.txt", "w", stdout);
  //	#endif
  ll n, w;
  cin >> n >> w;
  ll tot = 0;
  ll cost[n + 1], wt[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> wt[i] >> cost[i];
    tot = tot + cost[i];
  }
  ll dp[n + 1][tot + 1];
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= tot; j++) {
      dp[i][j] = INF;
    }
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= tot; j++) {
      if (j == 0)
        dp[i][j] = 0;
      else if (i == 1) {
        if (j == cost[i])
          dp[i][j] = wt[i];
        else
          dp[i][j] = INF;
      } else {
        if (j - cost[i] >= 0)
          dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - cost[i]] + wt[i]);
        else
          dp[i][j] = dp[i - 1][j];
      }
    }
  }
  for (int i = tot; i >= 0; i--) {
    //	cout<<dp[n][i]<<" ";
    if (dp[n][i] <= w) {
      cout << i;
      return 0;
    }
  }
}
Problem p03164 (C++)

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

using namespace std;

#define all(c) (c).begin(), (c).end()
#define sz(c) (static_cast<int>(c.size()))
#define endl "\n"

using ld = long double;
using ll = long long;

inline ll addm(ll __a, ll __b, ll __m);
inline ll subm(ll __a, ll __b, ll __m);
inline ll mulm(ll __a, ll __b, ll __m);
ll powm(ll __a, ll __b, ll __m);
ll inv(ll __x, ll __m);
// }}}

const ll INFL = numeric_limits<ll>::max() / 2;
const ll INF = numeric_limits<int>::max() / 2;
const ll MOD = 1e9 + 7;

const int V = 100500;
ll dp[V];
void solve() {
  int n, W;
  cin >> n >> W;
  fill(dp + 1, dp + V, INFL);
  for (int i = 0; i < n; i++) {
    int w, v;
    cin >> w >> v;
    for (int j = V - 1; j >= 0; j--) {
      if (dp[j - v] + w <= W)
        dp[j] = min(dp[j], dp[j - v] + w);
    }
  }
  for (int i = V - 1; i >= 0; i--) {
    if (dp[i] <= W) {
      cout << i << endl;
      return;
    }
  }
}

// main {{{
int main() {
  cin.sync_with_stdio(0);
  cin.tie(NULL);
  solve();
  return 0;
}

inline ll addm(ll __a, ll __b, ll __m = MOD) { return ((__a + __b) % __m); }
inline ll subm(ll __a, ll __b, ll __m = MOD) {
  return (((__a - __b) % __m + __m) % __m);
}
inline ll mulm(ll __a, ll __b, ll __m = MOD) { return ((__a * __b) % __m); }
ll powm(ll __a, ll __b, ll __m = MOD) {
  ll ret = (!__b) ? 1 : powm(__a, __b / 2, __m);
  return (!__b) ? 1 : mulm(mulm(ret, ret, __m), (__b % 2) ? __a : 1, __m);
}
ll inv(ll __x, ll __m = MOD) { return powm(__x, __m - 2, __m); }
// }}}


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

using namespace std;

#define all(c) (c).begin(), (c).end()
#define sz(c) (static_cast<int>(c.size()))
#define endl "\n"

using ld = long double;
using ll = long long;

inline ll addm(ll __a, ll __b, ll __m);
inline ll subm(ll __a, ll __b, ll __m);
inline ll mulm(ll __a, ll __b, ll __m);
ll powm(ll __a, ll __b, ll __m);
ll inv(ll __x, ll __m);
// }}}

const ll INFL = numeric_limits<ll>::max() / 2;
const ll INF = numeric_limits<int>::max() / 2;
const ll MOD = 1e9 + 7;

const int V = 100500;
ll dp[V];
void solve() {
  int n, W;
  cin >> n >> W;
  fill(dp + 1, dp + V, INFL);
  for (int i = 0; i < n; i++) {
    int w, v;
    cin >> w >> v;
    for (int j = V - 1; j >= v; j--) {
      if (dp[j - v] + w <= W)
        dp[j] = min(dp[j], dp[j - v] + w);
    }
  }
  for (int i = V - 1; i >= 0; i--) {
    if (dp[i] <= W) {
      cout << i << endl;
      return;
    }
  }
}

// main {{{
int main() {
  cin.sync_with_stdio(0);
  cin.tie(NULL);
  solve();
  return 0;
}

inline ll addm(ll __a, ll __b, ll __m = MOD) { return ((__a + __b) % __m); }
inline ll subm(ll __a, ll __b, ll __m = MOD) {
  return (((__a - __b) % __m + __m) % __m);
}
inline ll mulm(ll __a, ll __b, ll __m = MOD) { return ((__a * __b) % __m); }
ll powm(ll __a, ll __b, ll __m = MOD) {
  ll ret = (!__b) ? 1 : powm(__a, __b / 2, __m);
  return (!__b) ? 1 : mulm(mulm(ret, ret, __m), (__b % 2) ? __a : 1, __m);
}
ll inv(ll __x, ll __m = MOD) { return powm(__x, __m - 2, __m); }
// }}}
Problem p03164 (C++)

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

const int mnx = 1e6 + 9;
const int mod = 1e9 + 7;

ll n, w;
ll a[mnx];
ll b[mnx];
ll dp[mnx];

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

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

  fill(dp, dp + mnx, mod);

  dp[0] = 0;

  for (int i = 1; i <= n; i++) {
    for (int j = 200000; j >= 0; j--) {
      if (dp[j - b[i]] != mod)
        dp[j] = min(dp[j], dp[j - b[i]] + a[i]);
    }
  }

  ll ans = 0;

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

  cout << ans << '\n';

  return 0;
}

/*
3
10 40 70
20 50 80
30 60 90

10  40  70
90  120 120
150 180 210
*/


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

const int mnx = 1e6 + 9;
const int mod = 1e9 + 7;

ll n, w;
ll a[mnx];
ll b[mnx];
ll dp[mnx];

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

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

  fill(dp, dp + mnx, mod);

  dp[0] = 0;

  for (int i = 1; i <= n; i++) {
    for (int j = 200000; j >= 0; j--) {
      if (dp[j] != mod)
        dp[j + b[i]] = min(dp[j + b[i]], dp[j] + a[i]);
    }
  }

  ll ans = 0;

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

  cout << ans << '\n';

  return 0;
}

/*
3
10 40 70
20 50 80
30 60 90

10  40  70
90  120 120
150 180 210
*/
Problem p03164 (C++)

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

int main() {

  int N, W;
  cin >> N >> W;

  vector<long long> weight(N), value(N);
  long long sum = 0;
  for (int i = 0; i < N; i++) {
    cin >> weight.at(i) >> value.at(i);
    sum += value.at(i);
  }

  long long dp[110][1100];

  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= sum; j++) {
      dp[i][j] = 1LL << 60;
    }
  }

  dp[0][0] = 0;

  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= sum; j++) {
      if (j >= value[i]) {
        dp[i + 1][j] = min(dp[i][j], dp[i][j - value[i]] + weight[i]);
      } else {
        dp[i + 1][j] = dp[i][j];
      }
    }
  }

  long long ans = 0;
  for (long long i = 0; i <= sum; i++) {
    if (dp[N][i] <= W)
      ans = max(ans, i);
  }

  cout << ans << endl;
}

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

int main() {

  int N, W;
  cin >> N >> W;

  vector<long long> weight(N), value(N);
  long long sum = 0;
  for (int i = 0; i < N; i++) {
    cin >> weight.at(i) >> value.at(i);
    sum += value.at(i);
  }

  long long dp[110][sum + 1];

  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= sum; j++) {
      dp[i][j] = 1LL << 60;
    }
  }

  dp[0][0] = 0;

  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= sum; j++) {
      if (j >= value[i]) {
        dp[i + 1][j] = min(dp[i][j], dp[i][j - value[i]] + weight[i]);
      } else {
        dp[i + 1][j] = dp[i][j];
      }
    }
  }

  long long ans = 0;
  for (long long i = 0; i <= sum; i++) {
    if (dp[N][i] <= W)
      ans = max(ans, i);
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define s(x) (x).begin(), (x).end();
using namespace std;
typedef long long ll;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;

int main() {

  int N, W;
  cin >> N >> W;

  ll weight[110], varue[110];
  rep(i, N) cin >> weight[i] >> varue[i];
  ll max_v = 110000;

  ll dp[110][max_v];
  rep(i, max_v) { rep(j, 110000) dp[i][j] = 1000000; }

  dp[0][0] = 0;

  rep(i, N) {
    rep(j, max_v) {
      if (j >= varue[i])
        chmin(dp[i + 1][j], dp[i][j - varue[i]] + weight[i]);

      chmin(dp[i + 1][j], dp[i][j]);
    }
  }
  ll ans = 0;
  rep(i, max_v) {
    if (dp[N][i] <= W)
      ans = i;
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define s(x) (x).begin(), (x).end();
using namespace std;
typedef long long ll;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;

int main() {

  int N, W;
  cin >> N >> W;

  ll weight[110], varue[110];
  rep(i, N) cin >> weight[i] >> varue[i];
  ll max_v = 110000;

  ll dp[110][max_v];
  rep(i, 110) { rep(j, max_v) dp[i][j] = INF; }

  dp[0][0] = 0;

  rep(i, N) {
    rep(j, max_v) {
      if (j >= varue[i])
        chmin(dp[i + 1][j], dp[i][j - varue[i]] + weight[i]);

      chmin(dp[i + 1][j], dp[i][j]);
    }
  }
  ll 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 <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

const long long kInf = (long long)1e13;

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

  vector<vector<long long>> dp(n + 1, vector<long long>(sum + 1, kInf));
  for (int i = 0; i <= n; ++i) {
    dp[i][0] = 0;
  }

  for (int val = 1; val <= sum; ++val) {
    for (int i = 1; i <= n; ++i) {
      dp[i][val] = min(dp[i - 1][val], dp[i - 1][val - v[i - 1]] + w[i - 1]);
    }
  }

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


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

const long long kInf = (long long)1e13;

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

  vector<vector<long long>> dp(n + 1, vector<long long>(sum + 1, kInf));
  for (int i = 0; i <= n; ++i) {
    dp[i][0] = 0;
  }

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

  for (int val = sum; val >= 0; --val) {
    if (dp[n][val] <= W) {
      cout << val << endl;
      break;
    }
  }
}
Problem p03164 (C++)

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

using namespace std;

const int MAXN = 110;
int N, W;
long long int A[MAXN], P[MAXN];
map<long long int, long long int> dp;
deque<pair<long long int, long long int>> buffer;
long long int calcDP() {
  for (int i = 0; i < N; i++) {
    map<long long int, long long int>::reverse_iterator it = dp.rbegin();
    for (; it != dp.rend(); it++) {
      long long int weight = it->first;
      long long int value = it->second;
      if (weight + P[i] <= W && dp[weight] + A[i] > dp[weight + P[i]])
        buffer.push_back({weight + P[i], dp[weight] + A[i]});
    }
    dp[P[i]] = A[i];
    while (!buffer.empty()) {
      dp[buffer.front().first] = buffer.front().second;
      buffer.pop_front();
    }
  }
  long long int sol = 0;
  for (auto adj : dp) {
    sol = max(sol, adj.second);
  }
  return sol;
}

int main() {
  cin >> N >> W;
  for (int i = 0; i < N; i++)
    cin >> P[i] >> A[i];
  cout << calcDP();
}

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

using namespace std;

const int MAXN = 110;
int N, W;
long long int A[MAXN], P[MAXN];
map<long long int, long long int> dp;
deque<pair<long long int, long long int>> buffer;
long long int calcDP() {
  for (int i = 0; i < N; i++) {
    map<long long int, long long int>::reverse_iterator it = dp.rbegin();
    for (; it != dp.rend(); it++) {
      long long int weight = it->first;
      long long int value = it->second;
      if (weight + P[i] <= W && dp[weight] + A[i] > dp[weight + P[i]])
        buffer.push_back({weight + P[i], dp[weight] + A[i]});
    }
    dp[P[i]] = A[i];
    while (!buffer.empty()) {
      dp[buffer.front().first] = buffer.front().second;
      buffer.pop_front();
    }
    map<long long int, long long int>::iterator firstIt = dp.begin();
    firstIt++;
    map<long long int, long long int>::iterator secondIt = dp.begin();
    for (; firstIt != dp.end();) {
      if (firstIt->second <= secondIt->second) {
        map<long long int, long long int>::iterator tmp = firstIt;
        firstIt++;
        dp.erase(tmp);
      } else
        firstIt++, secondIt++;
    }
  }
  long long int sol = 0;
  for (auto adj : dp) {
    sol = max(sol, adj.second);
  }
  return sol;
}

int main() {
  cin >> N >> W;
  for (int i = 0; i < N; i++)
    cin >> P[i] >> A[i];
  cout << calcDP();
}
Problem p03164 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int n;
long long int w[200], v[200];
long long int dp[200][100006];
long long int sum = 0, W;

long long int knapsack(int i, int v1) {
  if (v1 <= 0)
    return 0;

  if (i == n) {
    return INT_MAX;
  }

  //    long long int res1=0,res2=0;
  //
  //    if(dp[i][v1]!=-1)
  //        return dp[i][v1];
  //    if(v1-v[i]>=0)
  //        res1=w[i]+knapsack(i+1,v1-v[i]);
  //    res2=knapsack(i+1,v1);
  // cout<<i<<" "<<res1<<" "<<res2<<" ";
  // cout<<min(res1,res2)<<endl;
  return dp[i][v1] =
             min(w[i] + knapsack(i + 1, v1 - v[i]), knapsack(i + 1, v1));
}

int main() {

  memset(dp, -1, sizeof dp);
  cin >> n >> W;
  for (int i = 0; i < n; i++) {
    cin >> w[i] >> v[i];
    sum += v[i];
  }
  // cout<<knapsack(0,0)<<endl;
  for (int i = sum; i >= 0; i--) {

    long long int k1 = knapsack(0, i);
    // cout<<k1<<endl;
    if (k1 <= W) {
      cout << i << endl;
      break;
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n;
long long int w[200], v[200];
long long int dp[200][100006];
long long int sum = 0, W;

long long int knapsack(int i, int v1) {
  if (v1 <= 0)
    return 0;

  if (i == n) {
    return INT_MAX;
  }

  //    long long int res1=0,res2=0;
  //
  if (dp[i][v1] != -1)
    return dp[i][v1];
  //    if(v1-v[i]>=0)
  //        res1=w[i]+knapsack(i+1,v1-v[i]);
  //    res2=knapsack(i+1,v1);
  // cout<<i<<" "<<res1<<" "<<res2<<" ";
  // cout<<min(res1,res2)<<endl;
  return dp[i][v1] =
             min(w[i] + knapsack(i + 1, v1 - v[i]), knapsack(i + 1, v1));
}

int main() {

  memset(dp, -1, sizeof dp);
  cin >> n >> W;
  for (int i = 0; i < n; i++) {
    cin >> w[i] >> v[i];
    sum += v[i];
  }
  // cout<<knapsack(0,0)<<endl;
  for (int i = sum; i >= 0; i--) {

    long long int k1 = knapsack(0, i);
    // cout<<k1<<endl;
    if (k1 <= W) {
      cout << i << endl;
      break;
    }
  }
  return 0;
}
Problem p03164 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
#define all(x) (x).begin(), (x).end()
#define endl '\n'
const ll inf = LLONG_MAX;
long double pi = M_PI;
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
void YES() { cout << "YES" << endl; }
void NO() { cout << "NO" << endl; }

int main() {
  ll n, w;
  cin >> n >> w;
  vll ww(n), v(n);
  for (ll i = 0; i < n; i++)
    cin >> ww[i] >> v[i];

  vector<vll> dp(n, vll(1e5 + 1, inf));
  dp[0][0] = 0;
  for (ll i = 1; i <= 1e5; i++) {
    if (i <= v[0])
      dp[0][i] = ww[0];
    else
      break;
  }
  for (ll i = 1; i < n; i++) {
    dp[i] = dp[i - 1];
    for (ll j = 1; j <= 1e5; j++) {
      if (dp[i - 1][max(0ll, j - v[i])] < inf) {
        dp[i][j] = min(dp[i][j], dp[i - 1][max(0ll, j - v[i])] + ww[i]);
      }
    }
  }
  for (ll i = 0; i <= 1e5; i++) {
    if (dp[n - 1][i] > w) {
      cout << i - 1 << endl;
      return 0;
    }
  }
  while (1) {
    cout << 0 << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
#define all(x) (x).begin(), (x).end()
#define endl '\n'
const ll inf = LLONG_MAX;
long double pi = M_PI;
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
void YES() { cout << "YES" << endl; }
void NO() { cout << "NO" << endl; }

int main() {
  ll n, w;
  cin >> n >> w;
  vll ww(n), v(n);
  for (ll i = 0; i < n; i++)
    cin >> ww[i] >> v[i];

  vector<vll> dp(n, vll(1e5 + 1, inf));
  dp[0][0] = 0;
  for (ll i = 1; i <= 1e5; i++) {
    if (i <= v[0])
      dp[0][i] = ww[0];
    else
      break;
  }
  for (ll i = 1; i < n; i++) {
    dp[i] = dp[i - 1];
    for (ll j = 1; j <= 1e5; j++) {
      if (dp[i - 1][max(0ll, j - v[i])] < inf) {
        dp[i][j] = min(dp[i][j], dp[i - 1][max(0ll, j - v[i])] + ww[i]);
      }
    }
  }
  for (ll i = 0; i <= 1e5; i++) {
    if (dp[n - 1][i] > w) {
      cout << i - 1 << endl;
      return 0;
    }
  }
  cout << 1e5 << endl;
}
Problem p03164 (C++)

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

ll max(ll a, ll b) { return (a > b) ? a : b; }
ll min(ll a, ll b) { return (a < b) ? a : b; }
void memset(int *arr, int n, int value) {
  for (int i = 0; i < n; i++) {
    arr[i] = value;
  }
}

void knapsack(ll *weight, ll *values, ll n, ll W, int val_sum, ll **dp) {
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < val_sum; j++) {
      dp[i][j] =
          min(dp[i - 1][j], dp[i - 1][j - values[i - 1]] + weight[i - 1]);
    }
  }
  ll ans = inf;
  for (int i = 0; i < val_sum; i++) {
    if (dp[n][i] <= W) {
      ans = i;
    }
  }
  /*for(int i= 0 ; i<=n ; i++)
  {
      for(int j = 0; j<=val_sum ; j++)
      {
          cout<<dp[i][j]<<" ";
      }
      cout<<endl;
  }*/
  cout << ans << endl;
}

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

  ll n, W;
  cin >> n >> W;
  ll *weight = new ll[n];
  ll *values = new ll[n];
  for (int i = 0; i < n; i++) {
    cin >> weight[i] >> values[i];
  }
  int val_sum = 0;
  for (int i = 0; i < n; i++) {
    val_sum += values[i];
  }
  val_sum += 1;
  ll **dp = new ll *[n + 1];
  for (int i = 0; i <= n; i++) {
    dp[i] = new ll[val_sum];
    for (int k = 0; k < val_sum; k++) {
      dp[i][k] = inf;
    }
  }
  dp[0][0] = 0;
  for (int i = 0; i <= n; i++) {
    dp[i][0] = 0;
  }
  for (int i = 1; i < val_sum; i++) {
    dp[0][i] = inf;
  }
  knapsack(weight, values, n, W, val_sum, dp);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define mod 1000000007
#define inf 1e18L
using namespace std;
typedef long long int ll;

ll max(ll a, ll b) { return (a > b) ? a : b; }
ll min(ll a, ll b) { return (a < b) ? a : b; }
void memset(int *arr, int n, int value) {
  for (int i = 0; i < n; i++) {
    arr[i] = value;
  }
}

void knapsack(ll *weight, ll *values, ll n, ll W, int val_sum, ll **dp) {
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < val_sum; j++) {
      dp[i][j] = dp[i - 1][j];
      if (values[i - 1] <= j) {
        dp[i][j] =
            min(dp[i - 1][j], dp[i - 1][j - values[i - 1]] + weight[i - 1]);
      }
    }
  }
  ll ans = inf;
  for (int i = 0; i < val_sum; i++) {
    if (dp[n][i] <= W) {
      ans = i;
    }
  }
  /*for(int i= 0 ; i<=n ; i++)
  {
      for(int j = 0; j<=val_sum ; j++)
      {
          cout<<dp[i][j]<<" ";
      }
      cout<<endl;
  }*/
  cout << ans << endl;
}

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

  ll n, W;
  cin >> n >> W;
  ll *weight = new ll[n];
  ll *values = new ll[n];
  for (int i = 0; i < n; i++) {
    cin >> weight[i] >> values[i];
  }
  int val_sum = 0;
  for (int i = 0; i < n; i++) {
    val_sum += values[i];
  }
  val_sum += 1;
  ll **dp = new ll *[n + 1];
  for (int i = 0; i <= n; i++) {
    dp[i] = new ll[val_sum];
    for (int k = 0; k < val_sum; k++) {
      dp[i][k] = inf;
    }
  }
  dp[0][0] = 0;
  for (int i = 0; i <= n; i++) {
    dp[i][0] = 0;
  }
  for (int i = 1; i < val_sum; i++) {
    dp[0][i] = inf;
  }
  knapsack(weight, values, n, W, val_sum, dp);
  return 0;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
ll dp[102][1002];
ll n, w;
ll weight[100002], val[100002];
ll solve(ll r, ll idx) {
  if (idx == n)
    return r == 0 ? 0 : INT_MAX;

  if (dp[idx][r] != -1)
    return dp[idx][r];

  ll ans = solve(r, idx + 1);
  if (val[idx] <= r)
    ans = min(ans, weight[idx] + solve(r - val[idx], idx + 1));

  return dp[idx][r] = ans;
}
ll s(ll v_max) {
  for (ll i = v_max; i >= 0; i--) {
    if (solve(i, 0) <= w)
      return i;
  }
  return 0;
}
int main() {

  cin >> n >> w;
  ll max_value = 0;
  for (ll i = 0; i < n; i++) {
    cin >> weight[i] >> val[i];
    max_value += val[i];
  }

  memset(dp, -1, sizeof(dp));
  cout << s(max_value);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
ll dp[102][100002];
ll n, w;
ll weight[100002], val[100002];
ll solve(ll r, ll idx) {
  if (idx == n)
    return r == 0 ? 0 : INT_MAX;

  if (dp[idx][r] != -1)
    return dp[idx][r];

  ll ans = solve(r, idx + 1);
  if (val[idx] <= r)
    ans = min(ans, weight[idx] + solve(r - val[idx], idx + 1));

  return dp[idx][r] = ans;
}
ll s(ll v_max) {
  for (ll i = v_max; i >= 0; i--) {
    if (solve(i, 0) <= w)
      return i;
  }
  return 0;
}
int main() {

  cin >> n >> w;
  ll max_value = 0;
  for (ll i = 0; i < n; i++) {
    cin >> weight[i] >> val[i];
    max_value += val[i];
  }

  memset(dp, -1, sizeof(dp));
  cout << s(max_value);
  return 0;
}
Problem p03164 (C++)

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

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

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

  int sum_val = 0;
  for (int i : value)
    sum_val += i;

  vector<int> dp(sum_val + 1, 1e9);
  dp[0] = 0;

  for (int i = 0; i < n; ++i) {
    for (int j = sum_val - value[i]; j >= 0; --j) {
      dp[j + value[i]] = min(dp[j + value[i]], dp[j] + weight[i]);
    }
  }

  int ans = 0;

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

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

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

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

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

  int sum_val = 0;
  for (int i : value)
    sum_val += i;

  vector<int> dp(sum_val + 1, 1e9);
  dp[0] = 0;

  for (int i = 0; i < n; ++i) {
    for (int j = sum_val - value[i]; j >= 0; --j) {
      dp[j + value[i]] = min(dp[j + value[i]], dp[j] + weight[i]);
    }
  }

  int ans = 0;

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

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define maxx 100100
long long dp[100][maxx], peso[maxx], valor[maxx];

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

  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= v + 10; j++) {
      dp[i][j] = 1e11;
      if (j == 0)
        dp[i][j] = 0;
    }
  }

  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    for (long long j = 1; j <= v; j++) {
      if (j - valor[i] >= 0 && (dp[i - 1][j - valor[i]] + peso[i]) <= w) {
        ans = max(j, ans);
        dp[i][j] = min(peso[i] + dp[i - 1][j - valor[i]], dp[i - 1][j]);
      } else
        dp[i][j] = dp[i - 1][j];
    }
  }
  /*
  for(int i = 0; i <= n; i++){
          for(int j = 0; j <= v; j++){
                  cout << dp[i][j] << " ";
          }
          cout << endl;
  }*/
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define maxx 100100
long long dp[200][maxx], peso[200], valor[200];

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

  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= v + 10; j++) {
      dp[i][j] = 1e11;
      if (j == 0)
        dp[i][j] = 0;
    }
  }

  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    for (long long j = 1; j <= v; j++) {
      if (j - valor[i] >= 0 && (dp[i - 1][j - valor[i]] + peso[i]) <= w) {
        ans = max(j, ans);
        dp[i][j] = min(peso[i] + dp[i - 1][j - valor[i]], dp[i - 1][j]);
      } else
        dp[i][j] = dp[i - 1][j];
    }
  }
  /*
  for(int i = 0; i <= n; i++){
          for(int j = 0; j <= v; j++){
                  cout << dp[i][j] << " ";
          }
          cout << endl;
  }*/
  cout << ans << endl;

  return 0;
}
Problem p03164 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
#define _69e27                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
using namespace std;
const int N = 1e2 + 5, M = 1e5 + 5, oo = 0x3f3f3f3f, mod = 1e9 + 7;

int n, w, arrw[N], arrv[N], mem[N][M], ans;

int solve(int i, int v) {
  if (v == 0)
    return 0;
  if (i == n)
    return oo;
  int op1 = solve(i + 1, v);
  int op2 = oo;
  if (arrv[i] <= v)
    op2 = solve(i + 1, v - arrv[i]) + arrw[i];
  return mem[i][v] = min(op1, op2);
}

int main() {
  _69e27 memset(mem, -1, sizeof mem);
  cin >> n >> w;
  for (int i = 0; i < n; i++)
    cin >> arrw[i] >> arrv[i];
  for (int i = 1; i <= 100000; i++) {
    if (solve(0, i) <= w)
      ans = i;
  }
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define _69e27                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
using namespace std;
const int N = 1e2 + 5, M = 1e5 + 5, oo = 0x3f3f3f3f, mod = 1e9 + 7;

int n, w, arrw[N], arrv[N], mem[N][M], ans;

int solve(int i, int v) {
  if (v == 0)
    return 0;
  if (i == n)
    return oo;
  if (mem[i][v] != -1)
    return mem[i][v];
  int op1 = solve(i + 1, v);
  int op2 = oo;
  if (arrv[i] <= v)
    op2 = solve(i + 1, v - arrv[i]) + arrw[i];
  return mem[i][v] = min(op1, op2);
}

int main() {
  _69e27 memset(mem, -1, sizeof mem);
  cin >> n >> w;
  for (int i = 0; i < n; i++)
    cin >> arrw[i] >> arrv[i];
  for (int i = 1; i <= 100000; i++) {
    if (solve(0, i) <= w)
      ans = i;
  }
  cout << ans;
  return 0;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long INF = 1000000001, n, k, w[200], c[200], dp[101][100001], ans, sum,
          minn = 1000000001;
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> w[i] >> c[i];
    minn = min(minn, c[i]);
    sum += c[i];
  }
  dp[0][0] = 0;
  for (int i = 0; i <= n; i++) {
    for (int j = 1; j <= sum; j++)
      dp[i][j] = INF;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= sum; j++) {
      if (j >= minn) {
        if (dp[i - 1][j - c[i]] != INF && j - c[i] >= 0)
          dp[i][j] = min(dp[i][j], dp[i - 1][j - c[i]] + w[i]);
        if (dp[i - 1][j] != INF && dp[i - 1][j])
          dp[i][j] = min(dp[i][j], dp[i - 1][j]);
      }
    }
  }

  for (int i = 1; i <= sum; i++) {
    if (dp[n][i] != INF && dp[n][i] <= k)
      ans = i;
  }
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long INF = 1000000001, n, k, w[200], c[200], dp[101][100001], ans, sum,
          minn = 1000000001;
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> w[i] >> c[i];
    minn = min(minn, c[i]);
    sum += c[i];
  }
  dp[0][0] = 0;
  for (int i = 0; i <= n; i++) {
    for (int j = 1; j <= sum; j++)
      dp[i][j] = INF;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= sum; j++) {
      if (j >= minn) {
        if (j - c[i] >= 0 && dp[i - 1][j - c[i]] != INF)
          dp[i][j] = min(dp[i][j], dp[i - 1][j - c[i]] + w[i]);
        if (dp[i - 1][j] != INF && dp[i - 1][j])
          dp[i][j] = min(dp[i][j], dp[i - 1][j]);
      }
    }
  }

  for (int i = 1; i <= sum; i++) {
    if (dp[n][i] != INF && dp[n][i] <= k)
      ans = i;
  }
  cout << ans;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define ll long long
#define vi vector<ll> v
#define vii vector<pair<ll, ll>> v
#define pb push_back
#define mod 1000000007
#define f(i, a, b) for (ll i = a; i < b; i++)
#define while_                                                                 \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define vmax 100000
using namespace std;

ll dp[101][vmax + 1];
ll maxP(ll W, ll n, ll w[], ll v[]) {

  for (int i = 0; i <= vmax; i++)
    dp[0][i] = 1e18;
  dp[0][0] = 0;
  for (ll an = 1; an <= n; an++) {
    for (ll av = 0; av <= vmax; av++) {
      if (v[an - 1] > av)
        dp[an][av] = dp[an - 1][av];
      else
        dp[an][av] =
            min(dp[an - 1][av], w[an - 1] + dp[an - 1][av - v[an - 1]]);
    }
  }
  ll ans = 0;
  f(i, 0, vmax + 1) if (dp[n][i] <= W) ans = i;
  return ans;
}

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

      ll n,
      W;
  cin >> n >> W;
  ll w[n], v[n];
  f(i, 0, n) cin >> w[i] >> v[i];
  cout << maxP(W, n, w, v);
}

Fixed submission:
#include <bits/stdc++.h>
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define ll long long
#define vi vector<ll> v
#define vii vector<pair<ll, ll>> v
#define pb push_back
#define mod 1000000007
#define f(i, a, b) for (ll i = a; i < b; i++)
#define while_                                                                 \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define vmax 100000
using namespace std;

ll dp[101][vmax + 1];
ll maxP(ll W, ll n, ll w[], ll v[]) {

  for (int i = 0; i <= vmax; i++)
    dp[0][i] = 1e18;
  dp[0][0] = 0;
  for (ll an = 1; an <= n; an++) {
    for (ll av = 0; av <= vmax; av++) {
      if (v[an - 1] > av)
        dp[an][av] = dp[an - 1][av];
      else
        dp[an][av] =
            min(dp[an - 1][av], w[an - 1] + dp[an - 1][av - v[an - 1]]);
    }
  }
  ll ans = 0;
  f(i, 0, vmax + 1) if (dp[n][i] <= W) ans = i;
  return ans;
}

int main() {
  /*#ifndef ONLINE_JUDGE
          freopen("input.txt", "r", stdin);
          freopen("output.txt", "w", stdout);
  #endif*/
  fastio

      ll n,
      W;
  cin >> n >> W;
  ll w[n], v[n];
  f(i, 0, n) cin >> w[i] >> v[i];
  cout << maxP(W, n, w, v);
}
Problem p03164 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
const ll MOD = 1000000007;
const long long INF = 1LL << 60;
const double pi = acos(-1.0);

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

vector<ll> v;
vector<ll> w;
vector<vector<ll>> table;

ll dp(ll n, ll sv) {
  if (sv <= 0)
    return 0;
  if (n < 0) {
    return INF;
  }
  if (table.at(n).at(sv) < INF)
    return table.at(n).at(sv);
  chmin(table.at(n).at(sv), dp(n - 1, sv - v.at(n)) + w.at(n));
  chmin(table.at(n).at(sv), dp(n - 1, sv));
  return table.at(n).at(sv);
}
int main() {
  ll N, W;
  cin >> N >> W;
  v = vector<ll>(N);
  w = vector<ll>(N);
  rep(i, N) { cin >> w.at(i) >> v.at(i); }
  table = vector<vector<ll>>(N, vector<ll>(100001, INF));
  for (int sv = 100000; sv >= 0; sv--) {
    if (dp(N - 1, sv) <= W) {
      cout << sv << endl;
      break;
    }
  }
  return 0;
}


Fixed submission:
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
const ll MOD = 1000000007;
const long long INF = 1LL << 60;
const double pi = acos(-1.0);

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

vector<ll> v;
vector<ll> w;
vector<vector<ll>> table;

ll dp(ll n, ll sv) {
  if (sv <= 0)
    return 0;
  if (n < 0) {
    return INF;
  }
  if (table.at(n).at(sv) < INF)
    return table.at(n).at(sv);
  chmin(table.at(n).at(sv), dp(n - 1, sv - v.at(n)) + w.at(n));
  chmin(table.at(n).at(sv), dp(n - 1, sv));
  chmin(table.at(n).at(sv), (ll)1000000001);
  return table.at(n).at(sv);
}
int main() {
  ll N, W;
  cin >> N >> W;
  v = vector<ll>(N);
  w = vector<ll>(N);
  rep(i, N) { cin >> w.at(i) >> v.at(i); }
  table = vector<vector<ll>>(N, vector<ll>(100001, INF));
  for (int sv = 100000; sv >= 0; sv--) {
    if (dp(N - 1, sv) <= W) {
      cout << sv << endl;
      break;
    }
  }
  return 0;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int INF = (int)1e9 + 7;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  const int MAXV = (int)1e3;
  int N, W;
  cin >> N >> W;
  vector<int> w(N), v(N);
  for (int i = 0; i < N; i++)
    cin >> w[i] >> v[i];

  vector<vector<int>> dp(N + 1, vector<int>(N * MAXV + 1, INF));
  dp[0][0] = 0;
  for (int i = 0; i < N; i++)
    for (int val = 0; val <= N * MAXV; val++) {
      chmin(dp[i + 1][val], dp[i][val]);
      chmin(dp[i + 1][val + v[i]], dp[i][val] + w[i]);
    }
  int ans = 0;
  for (int val = 0; val <= N * MAXV; val++)
    if (dp[N][val] <= W) {
      chmax(ans, val);
    }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int INF = (int)1e9 + 7;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  const int MAXV = (int)1e3;
  int N, W;
  cin >> N >> W;
  vector<int> w(N), v(N);
  for (int i = 0; i < N; i++)
    cin >> w[i] >> v[i];

  vector<vector<int>> dp(N + 1, vector<int>((N + 1) * MAXV + 1, INF));
  dp[0][0] = 0;
  for (int i = 0; i < N; i++)
    for (int val = 0; val <= N * MAXV; val++) {
      chmin(dp[i + 1][val], dp[i][val]);
      chmin(dp[i + 1][val + v[i]], dp[i][val] + w[i]);
    }
  int ans = 0;
  for (int val = 0; val <= N * MAXV; val++)
    if (dp[N][val] <= W) {
      chmax(ans, val);
    }
  cout << ans << endl;
  return 0;
}
Problem p03164 (C++)

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

using namespace std;

int main() {
  const long long inf = 2000000000;
  int n, w;
  cin >> n >> w;
  int cost[n];
  int value[n];
  const int max_n = 100;
  const int max_v = 1000;
  vector<vector<long long>> dp(max_n + 2);

  for (int i = 0; i < n; ++i) {
    cin >> cost[i] >> value[i];
  }
  for (int i = 0; i <= n + 1; ++i) {
    dp[i].resize(max_n * max_v + 1);
  }

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < max_n * max_v + 1; ++j) {
      if (i == 0 && j == 0) {
        dp[0][0] = 0;
      } else {
        dp[i][j] = inf;
      }
    }
  }

  for (int i = 0; i <= n; ++i) {
    for (int j = 0; j < max_n * max_v + 1; ++j) {
      if (j - value[i] >= 0) {
        dp[i + 1][j] = min(dp[i][j], dp[i][j - value[i]] + cost[i]);
      } else {
        dp[i + 1][j] = dp[i][j];
      }
    }
  }

  int res;
  for (int i = 0; i < max_n * max_v + 1; ++i) {
    if (dp[n + 1][i] <= w) {
      res = i;
    }
  }
  cout << res << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

int main() {
  const long long inf = 2000000000;
  int n, w;
  cin >> n >> w;
  long long cost[n];
  long long value[n];
  const int max_n = 100;
  const int max_v = 1000;
  vector<vector<long long>> dp(max_n + 2);

  for (int i = 0; i < n; ++i) {
    cin >> cost[i] >> value[i];
  }
  for (int i = 0; i <= n + 1; ++i) {
    dp[i].resize(max_n * max_v + 1);
  }

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < max_n * max_v + 1; ++j) {
      if (i == 0 && j == 0) {
        dp[0][0] = 0;
      } else {
        dp[i][j] = inf;
      }
    }
  }

  for (int i = 0; i <= n; ++i) {
    for (int j = 0; j < max_n * max_v + 1; ++j) {
      if (j - value[i] >= 0) {
        dp[i + 1][j] = min(dp[i][j], dp[i][j - value[i]] + cost[i]);
      } else {
        dp[i + 1][j] = dp[i][j];
      }
    }
  }

  int res;
  for (int i = 0; i < max_n * max_v + 1; ++i) {
    if (dp[n + 1][i] <= w) {
      res = i;
    }
  }
  cout << res << endl;

  return 0;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (lli i = 0; i < (n); i++)
#define rrep(i, n) for (lli i = (n)-1; i >= 0; i--)
using namespace std;
using lli = long long int;
lli v[105], w[105];
lli dp[105][100005] = {};
// i まででjを達成するのに必要なWの最小値
int main() {
  lli n, W;
  cin >> n >> W;

  rep(i, n) cin >> w[i] >> v[i];
  rep(i, 105) rep(j, 100005) dp[i][j] = i == 0 && j == 0 ? 0 : 1e18;
  rep(i, n) {
    rep(j, 100005) {
      dp[i + 1][j] = min(dp[i][j], dp[i + 1][j]);
      if (j + v[i] < 100005)
        dp[i + 1][j + v[i]] = min(dp[i + 1][j + v[i]], dp[i][j] + w[i]);
    }
  }
  rrep(j, 1000005) {
    // cout << dp[n][j] << " ";
    if (dp[n][j] <= W) {
      cout << j << endl;
      return 0;
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (lli i = 0; i < (n); i++)
#define rrep(i, n) for (lli i = (n)-1; i >= 0; i--)
using namespace std;
using lli = long long int;
lli v[105], w[105];
lli dp[105][100005] = {};
// i まででjを達成するのに必要なWの最小値
int main() {
  lli n, W;
  cin >> n >> W;

  rep(i, n) cin >> w[i] >> v[i];
  rep(i, 105) rep(j, 100005) dp[i][j] = i == 0 && j == 0 ? 0 : 1e18;
  rep(i, n) {
    rep(j, 100005) {
      dp[i + 1][j] = min(dp[i][j], dp[i + 1][j]);
      if (j + v[i] < 100005)
        dp[i + 1][j + v[i]] = min(dp[i + 1][j + v[i]], dp[i][j] + w[i]);
    }
  }
  rrep(j, 100005) {
    // cout << dp[n][j] << " ";
    if (dp[n][j] <= W) {
      cout << j << endl;
      return 0;
    }
  }
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long n, W, ans, INF = 1e9 + 1, sum, dp[101][10001];
pair<long long, long long> v[101];
int main() {
  cin >> n >> W;
  for (int i = 1; i <= n; i++)
    cin >> v[i].first >> v[i].second, sum += v[i].second;
  sort(v + 1, v + 1 + n);
  for (int i = 0; i <= n; i++) {
    dp[i][0] = 0;
    for (int j = 1; j <= sum; j++)
      dp[i][j] = INF;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= sum; j++) {
      if (j >= v[1].second) {
        if (j >= v[i].second && dp[i - 1][j - v[i].second] != INF)
          dp[i][j] = min(dp[i][j], dp[i - 1][j - v[i].second] + v[i].first);
        if (dp[i - 1][j] && dp[i - 1][j] != INF)
          dp[i][j] = min(dp[i - 1][j], dp[i][j]);
      }
    }
  }
  for (int i = 1; i <= sum; i++) {
    if (dp[n][i] <= W && dp[n][i])
      ans = i;
  }
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long n, W, ans, INF = 1e9 + 1, sum, dp[101][100001];
pair<long long, long long> v[101];
int main() {
  cin >> n >> W;
  for (int i = 1; i <= n; i++)
    cin >> v[i].first >> v[i].second, sum += v[i].second;
  sort(v + 1, v + 1 + n);
  for (int i = 0; i <= n; i++) {
    dp[i][0] = 0;
    for (int j = 1; j <= sum; j++)
      dp[i][j] = INF;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= sum; j++) {
      if (j >= v[1].second) {
        if (j >= v[i].second && dp[i - 1][j - v[i].second] != INF)
          dp[i][j] = min(dp[i][j], dp[i - 1][j - v[i].second] + v[i].first);
        if (dp[i - 1][j] && dp[i - 1][j] != INF)
          dp[i][j] = min(dp[i - 1][j], dp[i][j]);
      }
    }
  }
  for (int i = 1; i <= sum; i++) {
    if (dp[n][i] <= W && dp[n][i])
      ans = i;
  }
  cout << ans;
}
Problem p03164 (C++)

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

int main() {
  ll n, wt;
  cin >> n >> wt;
  ll w[n + 5], v[n + 5];
  rep(i, 0, n) cin >> w[i] >> v[i];
  ll tot = 0;
  for (ll &i : v)
    tot += i;
  vi dp(tot + 5, 1e18 + 5);
  dp[0] = 0;
  rep(i, 0, n) rrep(val, (tot - v[i]), 0) dp[val + v[i]] =
      min(dp[val + v[i]], dp[val] + w[i]);
  ll ans = 0;
  rep(val, 0, (tot + 1)) if (dp[val] <= wt) ans = val;
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define rrep(i, a, b) for (ll i = a; i >= b; i--)
typedef long long int ll;
typedef vector<ll> vi;

int main() {
  ll n, wt;
  cin >> n >> wt;
  ll w[n], v[n];
  rep(i, 0, n) cin >> w[i] >> v[i];
  ll tot = 0;
  for (ll &i : v)
    tot += i;
  vi dp(tot + 5, 1e18 + 5);
  dp[0] = 0;
  rep(i, 0, n) rrep(val, (tot - v[i]), 0) dp[val + v[i]] =
      min(dp[val + v[i]], dp[val] + w[i]);
  ll ans = 0;
  rep(val, 0, (tot + 1)) if (dp[val] <= wt) ans = val;
  cout << ans << endl;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#ifdef PRAGMA
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#endif
#define fst first
#define snd second
#define fore(i, a, n) for (int i = a; i < n; i++)
#define pb push_back
#define mp make_pair
#define bs binary_search
#define ALL(s) s.begin(), s.end()
#define FIN                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define SZ(n) ((int)(n).size())
#define MAXI ((ll)1e16)
#define MINI ((ll)-1e16)
using namespace std;
typedef long long ll;

ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return a * (b / gcd(a, b)); }
ll n, w, res;
ll weight[101];
ll val[101];
ll dp[101][1001];
ll solve(ll i, ll value) {
  if (value < 0)
    return MAXI;
  if (i == -1 && (value < 0 || value > 0))
    return MAXI;
  if (i == -1)
    return 0;
  if (dp[i][value] != -1)
    return dp[i][value];
  dp[i][value] =
      min(weight[i] + solve(i - 1, value - val[i]), solve(i - 1, value));
  return dp[i][value];
}
int main() {
  FIN;
  cin >> n >> w;
  fore(i, 0, n) { cin >> weight[i] >> val[i]; }
  memset(dp, -1, sizeof(dp));
  for (int i = 100000; i > -1; i--) {
    if (solve(n - 1, i) <= w) {
      cout << i << endl;
      return 0;
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
#ifdef PRAGMA
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#endif
#define fst first
#define snd second
#define fore(i, a, n) for (int i = a; i < n; i++)
#define pb push_back
#define mp make_pair
#define bs binary_search
#define ALL(s) s.begin(), s.end()
#define FIN                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define SZ(n) ((int)(n).size())
#define MAXI ((ll)1e16)
#define MINI ((ll)-1e16)
using namespace std;
typedef long long ll;

ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return a * (b / gcd(a, b)); }
ll n, w, res;
ll weight[101];
ll val[101];
ll dp[101][100001];
ll solve(ll i, ll value) {
  if (value < 0)
    return MAXI;
  if (i == -1 && (value < 0 || value > 0))
    return MAXI;
  if (i == -1)
    return 0;
  if (dp[i][value] != -1)
    return dp[i][value];
  dp[i][value] =
      min(weight[i] + solve(i - 1, value - val[i]), solve(i - 1, value));
  return dp[i][value];
}
int main() {
  FIN;
  cin >> n >> w;
  fore(i, 0, n) { cin >> weight[i] >> val[i]; }
  memset(dp, -1, sizeof(dp));
  for (int i = 100000; i > -1; i--) {
    if (solve(n - 1, i) <= w) {
      cout << i << endl;
      return 0;
    }
  }
}