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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
const ll INF = 1e18L + 5;
int main() {
  ll n, W, sum = 0;
  cin >> n >> W;

  vector<ll> wt(n), v(n);
  for (ll i = 1; i <= n; i++) {
    cin >> wt[i] >> v[i];
    sum += v[i];
  }
  vector<ll> dp(sum + 1, INF);
  dp[0] = 0;
  for (ll i = 1; i <= n; i++) {
    for (ll j = sum; j >= v[i]; j--) {
      dp[j] = min(dp[j], wt[i] + dp[j - v[i]]);
    }
  }
  ll ans = 0;
  ll i;
  for (i = sum; i >= 0; i--) {
    if (dp[i] <= W) {
      cout << i << endl;
      break;
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
const ll INF = 1e18L + 5;
int main() {
  ll n, W, sum = 0;
  cin >> n >> W;

  vector<ll> wt(n + 1), v(n + 1);
  for (ll i = 1; i <= n; i++) {
    cin >> wt[i] >> v[i];
    sum += v[i];
  }
  vector<ll> dp(sum + 1, INF);
  dp[0] = 0;
  for (ll i = 1; i <= n; i++) {
    for (ll j = sum; j >= v[i]; j--) {
      dp[j] = min(dp[j], wt[i] + dp[j - v[i]]);
    }
  }
  ll ans = 0;
  ll i;
  for (i = sum; i >= 0; i--) {
    if (dp[i] <= W) {
      cout << i << endl;
      break;
    }
  }
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const ll INF = 1LL << 60;

ll dp[100][100009];

int main() {
  int n;
  ll w;
  cin >> n >> w;
  vector<ll> wight(n);
  vector<ll> val(n);
  rep(i, n) { cin >> wight[i] >> val[i]; }
  rep(i, n + 1) rep(j, 100001) dp[i][j] = INF;
  dp[0][0] = 0;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= 100000; j++) {
      if (j - val[i] >= 0)
        dp[i + 1][j] = min(dp[i][j], dp[i][j - val[i]] + wight[i]);
      else
        dp[i + 1][j] = dp[i][j];
    }
  }
  ll ans = 0;
  for (int g = 0; g <= 100000; g++) {
    if (dp[n][g] <= w)
      ans = max(ll(g), ans);
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const ll INF = 1LL << 60;

ll dp[105][100009];

int main() {
  int n;
  ll w;
  cin >> n >> w;
  vector<ll> wight(n);
  vector<ll> val(n);
  rep(i, n) { cin >> wight[i] >> val[i]; }
  rep(i, n + 1) rep(j, 100001) dp[i][j] = INF;
  dp[0][0] = 0;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= 100000; j++) {
      if (j - val[i] >= 0)
        dp[i + 1][j] = min(dp[i][j], dp[i][j - val[i]] + wight[i]);
      else
        dp[i + 1][j] = dp[i][j];
    }
  }
  ll ans = 0;
  for (int g = 0; g <= 100000; g++) {
    if (dp[n][g] <= w)
      ans = max(ll(g), ans);
  }
  cout << ans << endl;

  return 0;
}
Problem p03164 (C++)

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

using namespace std;

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

const long long INF = 1LL << 60;
const int MAX_V = pow(10, 3) * 100 + 1;

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

  int MAX_N = N;

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

  vector<vector<long long>> dp(MAX_N, vector<long long>(MAX_V, INF));

  dp[0][0] = 0;

  // DPループ
  for (int i = 0; i < N; ++i) {
    for (int sum_v = 0; sum_v < MAX_V; ++sum_v) {

      // i 番目の品物を選ぶ場合
      if (sum_v - value[i] >= 0)
        chmin(dp[i + 1][sum_v], dp[i][sum_v - value[i]] + weight[i]);

      // i 番目の品物を選ばない場合
      chmin(dp[i + 1][sum_v], dp[i][sum_v]);
    }
  }

  // 最適値の出力
  long long res = 0;
  for (int sum_v = 0; sum_v < MAX_V; ++sum_v) {
    if (dp[N][sum_v] <= W)
      res = sum_v;
  }
  cout << res << endl;
}


Fixed submission:
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

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

const long long INF = 1LL << 60;
const int MAX_V = pow(10, 3) * 100 + 1;

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

  int MAX_N = N + 1;

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

  vector<vector<long long>> dp(MAX_N, vector<long long>(MAX_V, INF));

  dp[0][0] = 0;

  // DPループ
  for (int i = 0; i < N; ++i) {
    for (int sum_v = 0; sum_v < MAX_V; ++sum_v) {

      // i 番目の品物を選ぶ場合
      if (sum_v - value[i] >= 0)
        chmin(dp[i + 1][sum_v], dp[i][sum_v - value[i]] + weight[i]);

      // i 番目の品物を選ばない場合
      chmin(dp[i + 1][sum_v], dp[i][sum_v]);
    }
  }

  // 最適値の出力
  long long res = 0;
  for (int sum_v = 0; sum_v < MAX_V; ++sum_v) {
    if (dp[N][sum_v] <= W)
      res = sum_v;
  }
  cout << res << endl;
}
Problem p03164 (C++)

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

using namespace std;

#define intt long long

const intt INF = 1e18L + 5;

int n, W;

int main() {

  cin >> n >> W;

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

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

  vector<intt> dp(V + 1, INF);
  // dp[i] -> the minimum total weight of knapsack with value exactly i
  dp[0] = 0;
  for (int k = 1; k <= n; k++) {
    for (int x = V; x >= 0; x--) {
      dp[x] = min(dp[x], w[k] + dp[x - v[k]]);
    }
  }

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

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

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

using namespace std;

#define intt long long

const intt INF = 1e18L + 5;

int n, W;

int main() {

  cin >> n >> W;

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

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

  vector<intt> dp(V + 1, INF);
  // dp[i] -> the minimum total weight of knapsack with value exactly i
  dp[0] = 0;
  for (int k = 1; k <= n; k++) {
    for (int x = V; x >= 0; x--) {
      if (x - v[k] >= 0) {
        dp[x] = min(dp[x], w[k] + dp[x - v[k]]);
      }
    }
  }

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

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

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

int w[110], v[110];
long long int dp[1100];
int INF = 1000000009;

int N, W;
int main() {
  for (int i = 0; i < 100001; i++)
    dp[i] = INF;
  cin >> N >> W;
  for (int i = 0; i < N; i++) {
    cin >> w[i] >> v[i];
  }
  dp[0] = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 100000; j >= v[i]; j--) {
      dp[j] = min(dp[j], dp[j - v[i]] + w[i]);
    }
  }
  int ans = 0;
  for (int i = 0; i < 100001; i++) {
    if (dp[i] <= W) {
      ans = max(ans, i);
    }
  }
  cout << ans << endl;
  return 0;
}

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

int w[110], v[110];
long long int dp[110000];
int INF = 1000000009;

int N, W;
int main() {
  for (int i = 0; i < 100001; i++)
    dp[i] = INF;
  cin >> N >> W;
  for (int i = 0; i < N; i++) {
    cin >> w[i] >> v[i];
  }
  dp[0] = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 100000; j >= v[i]; j--) {
      dp[j] = min(dp[j], dp[j - v[i]] + w[i]);
    }
  }
  int ans = 0;
  for (int i = 0; i < 100001; i++) {
    if (dp[i] <= W) {
      ans = max(ans, i);
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03164 (C++)

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

using namespace std;
typedef long long ll;

int main() {
  static ll INF = 1000000000000;
  int N, W;
  cin >> N >> W;
  vector<int> w(N);
  vector<int> v(N);
  vector<vector<ll>> dp(N + 1, vector<ll>(100009, INF));

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

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

  ll mv = 0;
  for (int i = 0; i < 100009; i++) {
    if (dp[N][i] <= W)
      mv = i;
  }
  cout << mv << endl;
}


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

using namespace std;
typedef long long ll;

int main() {
  static ll INF = 1000000000000;
  int N, W;
  cin >> N >> W;
  vector<int> w(N);
  vector<int> v(N);
  vector<vector<ll>> dp(N + 1, vector<ll>(200009, INF));

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

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

  ll mv = 0;
  for (int i = 0; i < 100009; i++) {
    if (dp[N][i] <= W)
      mv = i;
  }
  cout << mv << endl;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
using namespace std;
typedef long long ll;
const long long inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;

ll n, k;

struct node {
  ll w;
  ll v;
} a[100005];

ll dp[100005];

int main() {
  cin >> n >> k;
  ll sumv = 0;
  for (ll i = 1; i <= n; i++) {
    cin >> a[i].w >> a[i].v;
    sumv += a[i].v;
  }
  for (int i = 1; i <= 1e5; i++) {
    dp[i] = INF;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1e5; j >= 0; j--) {
      dp[j] = (dp[j] < (dp[j - a[i].v] + a[i].w) ? dp[j]
                                                 : (dp[j - a[i].v] + a[i].w));
    }
  }
  ll ans = 0;
  for (int i = 1e5; i >= 1; i--) {
    if (dp[i] <= k) {
      ans = i;
      break;
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
using namespace std;
typedef long long ll;
const long long inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;

ll n, k;

struct node {
  ll w;
  ll v;
} a[100005];

ll dp[100005];

int main() {
  cin >> n >> k;
  ll sumv = 0;
  for (ll i = 1; i <= n; i++) {
    cin >> a[i].w >> a[i].v;
    sumv += a[i].v;
  }
  for (int i = 1; i <= 1e5; i++) {
    dp[i] = INF;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1e5; j >= a[i].v; j--) {
      dp[j] = (dp[j] < (dp[j - a[i].v] + a[i].w) ? dp[j]
                                                 : (dp[j - a[i].v] + a[i].w));
    }
  }
  ll ans = 0;
  for (int i = 1e5; i >= 1; i--) {
    if (dp[i] <= k) {
      ans = i;
      break;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03164 (C++)

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

using namespace std;

#define all(a) a.begin(), a.end()
#define pi acos(-1)
#define eps 1e-6
#define inf 1e14

typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<vi> vvi;

const int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
const int dy[8] = {-1, 0, 1, -1, 1, 0, 1, -1};
const int maxn = 100;
const int mod = 1e9 + 7;
const int maxa = 1e4 + 1;

int mul(int a, int b) { return a * 1LL * b % mod; }
int add(int a, int b) { return a + b >= mod ? a + b - mod : a + b; }
int sub(int a, int b) { return a >= b ? a - b : a - b + mod; }
int pow(int a, int k) {
  int res = 1;
  while (k) {
    if (k % 2)
      res = mul(res, a);
    a = mul(a, a);
    k /= 2;
  }

  return res;
}

int inv(int a) { return pow(a, mod - 2); }

int W, n;
ll dp[maxa], cost[maxn], val[maxn], sum;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> W;
  fill(dp, dp + maxa, 1e9);
  for (int i = 0; i < n; ++i)
    cin >> cost[i] >> val[i], sum += val[i];

  dp[0] = 0;
  for (int i = 0; i < n; ++i)
    for (int v = sum - val[i]; v >= 0; --v)
      dp[v + val[i]] = min(dp[v + val[i]], dp[v] + cost[i]);

  for (int v = sum; v >= 0; --v)
    if (dp[v] <= W)
      return cout << v, 0;
}


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

using namespace std;

#define all(a) a.begin(), a.end()
#define pi acos(-1)
#define eps 1e-6
#define inf 1e14

typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<vi> vvi;

const int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
const int dy[8] = {-1, 0, 1, -1, 1, 0, 1, -1};
const int maxn = 100;
const int mod = 1e9 + 7;
const int maxa = 1e5 + 1;

int mul(int a, int b) { return a * 1LL * b % mod; }
int add(int a, int b) { return a + b >= mod ? a + b - mod : a + b; }
int sub(int a, int b) { return a >= b ? a - b : a - b + mod; }
int pow(int a, int k) {
  int res = 1;
  while (k) {
    if (k % 2)
      res = mul(res, a);
    a = mul(a, a);
    k /= 2;
  }

  return res;
}

int inv(int a) { return pow(a, mod - 2); }

int W, n;
ll dp[maxa], cost[maxn], val[maxn], sum;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> W;
  fill(dp, dp + maxa, 1e9);
  for (int i = 0; i < n; ++i)
    cin >> cost[i] >> val[i], sum += val[i];

  dp[0] = 0;
  for (int i = 0; i < n; ++i)
    for (int v = sum - val[i]; v >= 0; --v)
      dp[v + val[i]] = min(dp[v + val[i]], dp[v] + cost[i]);

  for (int v = sum; v >= 0; --v)
    if (dp[v] <= W)
      return cout << v, 0;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
#define int long long
#define endl '\n'
int mod = 1e9 + 7;
int mod2 = 998244353;
const int INF = 1e9;

signed main() {
  int n, W;
  cin >> n >> W;
  vector<int> w(n);
  vector<int> v(n);
  for (int i = 0; i < n; i++)
    cin >> w[i] >> v[i];
  vector<vector<int>> dp(n + 1, vector<int>(1e5 + 1, 1e13));
  dp[0][0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 1e5 + 1; j++) {
      dp[i + 1][j + v[i]] = min(dp[i + 1][j + v[i]], dp[i][j] + w[i]);
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
    }
  }
  int ans = 0;
  for (int i = 0; i < 1e5 + 1; i++) {
    if (dp[n][i] <= W)
      ans = max(ans, i);
  }
  cout << ans << endl;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define int long long
#define endl '\n'
int mod = 1e9 + 7;
int mod2 = 998244353;
const int INF = 1e9;

signed main() {
  int n, W;
  cin >> n >> W;
  vector<int> w(n);
  vector<int> v(n);
  for (int i = 0; i < n; i++)
    cin >> w[i] >> v[i];
  vector<vector<int>> dp(n + 1, vector<int>(1e5 + 1001, 1e13));
  dp[0][0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 1e5 + 1; j++) {
      dp[i + 1][j + v[i]] = min(dp[i + 1][j + v[i]], dp[i][j] + w[i]);
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
    }
  }
  int ans = 0;
  for (int i = 0; i < 1e5 + 1; i++) {
    if (dp[n][i] <= W)
      ans = max(ans, i);
  }
  cout << ans << endl;
}
Problem p03164 (C++)

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

const int MAX_N = 100;
const int MAX_V = 100100;

long long dp[MAX_N][MAX_V] = {
    0}; // N-1番目までで入れた容量の価値を最大にするもの
long long weight[MAX_N], value[MAX_V];
const long long INF = 1LL << 60;

int main() {
  long long N, W;
  cin >> N >> W;
  for (int i = 0; i < N; ++i) {
    cin >> weight[i] >> value[i];
  }
  for (int i = 0; i < MAX_N; ++i) {
    for (int j = 0; j < MAX_V; ++j) {
      dp[i][j] = INF;
    }
  }

  dp[0][0] = 0;

  for (int i = 0; i < N; ++i) {
    for (int sum_v = 0; sum_v < MAX_V; ++sum_v) {
      // i番目の商品を選ぶ
      if (sum_v - value[i] >= 0) {
        if (dp[i + 1][sum_v] > dp[i][sum_v - value[i]] + weight[i]) {
          dp[i + 1][sum_v] = dp[i][sum_v - value[i]] + weight[i];
        }
      }

      if (dp[i + 1][sum_v] > dp[i][sum_v]) {
        dp[i + 1][sum_v] = dp[i][sum_v];
      }
    }
  }

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

  return 0;
}

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

const int MAX_N = 110;
const int MAX_V = 100100;

long long dp[MAX_N][MAX_V] = {
    0}; // N-1番目までで入れた容量の価値を最大にするもの
long long weight[MAX_N], value[MAX_V];
const long long INF = 1LL << 60;

int main() {
  long long N, W;
  cin >> N >> W;
  for (int i = 0; i < N; ++i) {
    cin >> weight[i] >> value[i];
  }
  for (int i = 0; i < MAX_N; ++i) {
    for (int j = 0; j < MAX_V; ++j) {
      dp[i][j] = INF;
    }
  }

  dp[0][0] = 0;

  for (int i = 0; i < N; ++i) {
    for (int sum_v = 0; sum_v < MAX_V; ++sum_v) {
      // i番目の商品を選ぶ
      if (sum_v - value[i] >= 0) {
        if (dp[i + 1][sum_v] > dp[i][sum_v - value[i]] + weight[i]) {
          dp[i + 1][sum_v] = dp[i][sum_v - value[i]] + weight[i];
        }
      }

      if (dp[i + 1][sum_v] > dp[i][sum_v]) {
        dp[i + 1][sum_v] = dp[i][sum_v];
      }
    }
  }

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

  return 0;
}
Problem p03164 (C++)

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

#define MAX_N 100
#define MAX_V 1000
#define INF pow(10, 12)

using namespace std;

int main() {

  int N, W;
  cin >> N >> W;
  vector<long> w(N);
  vector<long> v(N);
  for (int i = 0; i < N; i++)
    cin >> w[i] >> v[i];

  vector<vector<long>> dp(MAX_N + 1, vector<long>(MAX_N * MAX_V + 1));
  dp[0] = vector<long>(MAX_N * MAX_V + 1, INF);
  dp[0][0] = 0;

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

  long res = 0;
  for (int i = 0; i < MAX_N * MAX_V; i++)
    if (dp[N][i] <= W)
      res = i;

  cout << res << endl;

  return 0;
}

Fixed submission:
#include <cmath>
#include <iostream>
#include <vector>

#define MAX_N 200
#define MAX_V 1000
#define INF pow(10, 12)

using namespace std;

int main() {

  int N, W;
  cin >> N >> W;
  vector<long> w(N);
  vector<long> v(N);
  for (int i = 0; i < N; i++)
    cin >> w[i] >> v[i];

  vector<vector<long>> dp(MAX_N + 1, vector<long>(MAX_N * MAX_V + 1));
  dp[0] = vector<long>(MAX_N * MAX_V + 1, INF);
  dp[0][0] = 0;

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

  long res = 0;
  for (int i = 0; i < MAX_N * MAX_V; i++)
    if (dp[N][i] <= W)
      res = i;

  cout << res << endl;

  return 0;
}
Problem p03164 (C++)

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

#define ll long long

using namespace std;

void solve() {
  int n, w_max;
  cin >> n >> w_max;

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

  // dp[i][k] -> menor peso possível utilizando até o item i atigindo valor
  //             maior ou igual a k
  vector<vector<ll>> dp(n, vector<ll>(100013, -1ll));

  function<ll(int, int)> calc_dp = [&](int i, int k) {
    if (i < 0) {
      if (k <= 0)
        return 0ll;
      else
        return 1000000123ll;
    }
    if (dp[i][k] != -1)
      return dp[i][k];

    dp[i][k] = min(w[i] + calc_dp(i - 1, k - v[i]), calc_dp(i - 1, k));

    return dp[i][k];
  };

  ll ans = 0ll;
  for (int i = 0; i < 100013; ++i) {
    int w = calc_dp(n - 1, i);
    if (w <= w_max)
      ans = i;
  }

  cout << ans << endl;
}

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

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

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

  for (int i = 0; i < t; ++i) {
    solve();
  }

  return 0;
}


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

#define ll long long

using namespace std;

void solve() {
  int n, w_max;
  cin >> n >> w_max;

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

  // dp[i][k] -> menor peso possível utilizando até o item i atigindo valor
  //             maior ou igual a k
  vector<vector<ll>> dp(n, vector<ll>(100013, -1ll));

  function<ll(int, int)> calc_dp = [&](int i, int k) {
    if (i < 0 || k < 0) {
      if (k <= 0)
        return 0ll;
      else
        return 1000000123ll;
    }
    if (dp[i][k] != -1)
      return dp[i][k];

    dp[i][k] = min(w[i] + calc_dp(i - 1, k - v[i]), calc_dp(i - 1, k));

    return dp[i][k];
  };

  ll ans = 0ll;
  for (int i = 0; i < 100013; ++i) {
    int w = calc_dp(n - 1, i);
    if (w <= w_max)
      ans = i;
  }

  cout << ans << endl;
}

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

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

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

  for (int i = 0; i < t; ++i) {
    solve();
  }

  return 0;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <utility>
#include <vector>

using namespace std;
typedef long long LL;
typedef pair<LL, LL> PII;
LL MOD = 1000000007;

int main() {

  int N, W;
  cin >> N >> W;
  // vector<int> v(N, 0), w(N, 0);
  int M = 100100;
  vector<vector<LL>> dp(N + 1, vector<LL>(M, W + 1));
  dp[0][0] = 0;
  for (int i = 1; i <= N; i++) {
    LL w, v;
    cin >> w >> v;
    for (int j = 0; j < M; j++)
      dp[i][j] = dp[i - 1][j];
    for (int j = 0; j < M; j++) {
      dp[i][j + v] = min(dp[i - 1][j] + w, dp[i][j + v]);
    }
  }
  LL ans = 0;
  for (int j = 0; j < M; j++)
    if (dp[N][j] <= W) {
      ans = j;
    }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <utility>
#include <vector>

using namespace std;
typedef long long LL;
typedef pair<LL, LL> PII;
LL MOD = 1000000007;

int main() {

  int N, W;
  cin >> N >> W;
  // vector<int> v(N, 0), w(N, 0);
  int M = 100100;
  vector<vector<LL>> dp(N + 1, vector<LL>(M, W + 1));
  dp[0][0] = 0;
  for (int i = 1; i <= N; i++) {
    LL w, v;
    cin >> w >> v;
    for (int j = 0; j < M; j++)
      dp[i][j] = dp[i - 1][j];
    for (int j = 0; j < M - v; j++) {
      dp[i][j + v] = min(dp[i - 1][j] + w, dp[i][j + v]);
    }
  }
  LL ans = 0;
  for (int j = 0; j < M; 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;
typedef long long ll;

const ll INF = 1e15;
ll n, W, dp[100001], c[101], w[101], ans;

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

  for (int i = 0; i <= 100000; ++i)
    dp[i] = INF;

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

  dp[0] = 0;
  for (int pos = 0; pos < n; ++pos) {
    if (w[pos] > W)
      continue;
    for (int cst = 100000; cst >= 0; --cst)
      dp[cst + c[pos]] = min(dp[cst + c[pos]], dp[cst] + w[pos]);
  }

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

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

const ll INF = 1e15;
ll n, W, dp[100001], c[101], w[101], ans;

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

  for (int i = 0; i <= 100000; ++i)
    dp[i] = INF;

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

  dp[0] = 0;
  for (int pos = 0; pos < n; ++pos) {
    if (w[pos] > W)
      continue;
    for (int cst = 100000; cst >= 0; --cst)
      if (cst + c[pos] <= 100000)
        dp[cst + c[pos]] = min(dp[cst + c[pos]], dp[cst] + w[pos]);
  }

  for (int i = 100000; i >= 0; --i)
    if (dp[i] <= W) {
      cout << i << endl;
      return 0;
    }
}
Problem p03164 (C++)

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

int main() {
  int64_t n, wlim;
  cin >> n >> wlim;
  vector<int64_t> ws(n), vs(n);
  for (int i = 0; i < n; i++) {
    cin >> ws[i] >> vs[i];
  }
  vector<vector<int64_t>> dp(n, vector<int64_t>(100001, 1LL << 60));
  dp[0][0] = 0;
  dp[0][vs[0]] = ws[0];
  for (int i = 1; i < n; i++) {
    for (int j = 0; j <= 100000; j++) {
      dp[i][j] = min(dp[i][j], dp[i - 1][j]);
      dp[i][j + vs[i]] = min(dp[i][j + vs[i]], dp[i - 1][j] + ws[i]);
    }
  }
  int64_t ans = -1;
  for (int64_t j = 100000; 0 <= j; j--) {
    if (dp[n - 1][j] <= wlim) {
      ans = max(ans, j);
    }
  }
  cout << ans << endl;
  return 0;
}

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

int main() {
  int64_t n, wlim;
  cin >> n >> wlim;
  vector<int64_t> ws(n), vs(n);
  for (int i = 0; i < n; i++) {
    cin >> ws[i] >> vs[i];
  }
  vector<vector<int64_t>> dp(n, vector<int64_t>(100001, 1LL << 60));
  dp[0][0] = 0;
  dp[0][vs[0]] = ws[0];
  for (int i = 1; i < n; i++) {
    for (int j = 0; j <= 100000; j++) {
      dp[i][j] = min(dp[i][j], dp[i - 1][j]);
      if (j + vs[i] <= 100000) {
        dp[i][j + vs[i]] = min(dp[i][j + vs[i]], dp[i - 1][j] + ws[i]);
      }
    }
  }
  int64_t ans = -1;
  for (int64_t j = 100000; 0 <= j; j--) {
    if (dp[n - 1][j] <= wlim) {
      ans = max(ans, j);
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define POP pop_back()
#define ll long long
#define db double
#define POP pop_back()
#define endl '\n'
const int maxn = 1e3 + 5;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const ll inF = 0x3f3f3f3f3f3f3f3f;
struct node {
  int w, v;
};
node a[maxn];
ll dp[maxn];
int main() {
  int n, c;
  cin >> n >> c;
  int w, v, tmp = 0;
  for (int i = 1; i <= n; i++)
    cin >> a[i].w >> a[i].v, tmp += a[i].v;
  for (int i = 1; i <= tmp; i++)
    dp[i] = inF;
  for (int i = 1; i <= n; i++) {
    for (int j = tmp; j >= a[i].v; j--) {
      dp[j] = min(dp[j], dp[j - a[i].v] + a[i].w);
    }
  }
  int ans = 0;
  for (int i = tmp; i >= 1; i--) {
    if (dp[i] <= c) {
      ans = i;
      break;
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define POP pop_back()
#define ll long long
#define db double
#define POP pop_back()
#define endl '\n'
const int maxn = 1e5 + 5;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const ll inF = 0x3f3f3f3f3f3f3f3f;
struct node {
  int w, v;
};
node a[maxn];
ll dp[maxn];
int main() {
  int n, c;
  cin >> n >> c;
  int w, v, tmp = 0;
  for (int i = 1; i <= n; i++)
    cin >> a[i].w >> a[i].v, tmp += a[i].v;
  for (int i = 1; i <= tmp; i++)
    dp[i] = inF;
  for (int i = 1; i <= n; i++) {
    for (int j = tmp; j >= a[i].v; j--) {
      dp[j] = min(dp[j], dp[j - a[i].v] + a[i].w);
    }
  }
  int ans = 0;
  for (int i = tmp; i >= 1; i--) {
    if (dp[i] <= c) {
      ans = i;
      break;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int N, W;
  cin >> N >> W;
  long long int v[N], w[N];
  for (long long int i = 0; i < N; i++)
    cin >> w[i] >> v[i];
  long long int dp[N + 1][100001];
  dp[0][0] = 0;
  for (long long int i = 1; i <= 100000; i++)
    dp[0][i] = W + 1;
  for (long long int i = 1; i <= N; i++) {
    for (long long int j = 0; j <= 100000; j++) {
      if (j < v[i - 1])
        dp[i][j] = dp[i - 1][j];
      else
        dp[i][j] = min(w[i - 1] + dp[i - 1][j - v[i - 1]], dp[i - 1][j]);
    }
  }
  long long int x = 1000000;
  while (W < dp[N][x])
    x--;
  cout << x << endl;
}

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

Buggy submission (status: Runtime Error):
// In the name of God

#include <bits/stdc++.h>

using namespace std;

#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define debug(x) cout << #x ": " << x << endl;

int dp[105][(int)1e3 + 5];
int w[105];
int v[105];

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


Fixed submission:
// In the name of God

#include <bits/stdc++.h>

using namespace std;

#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define debug(x) cout << #x ": " << x << endl;

int dp[105][(int)1e5 + 5];
int w[105];
int v[105];

int main() {
  IOS int n, W;
  cin >> n >> W;
  int mxv = 0;
  for (int i = 0; i < n; i++) {
    cin >> w[i] >> v[i];
    mxv += v[i];
  }
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= mxv; j++) {
      dp[i][j] = W + 1;
    }
  }
  for (int i = 0; i <= n; i++)
    dp[i][0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= mxv; j++) {
      if (j >= v[i - 1])
        dp[i][j] = min(dp[i][j],
                       min(dp[i - 1][j], dp[i - 1][j - v[i - 1]] + w[i - 1]));
      else
        dp[i][j] = min(dp[i][j], dp[i - 1][j]);
    }
  }
  int ans = 0;
  for (int i = 0; i <= mxv; i++) {
    if (dp[n][i] <= W)
      ans = max(ans, i);
  }
  cout << ans;
  return 0;
}
Problem p03164 (C++)

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

using namespace std;

#define eb emplace_back
#define ii pair<int, int>
#define OK (cerr << "OK" << endl)
#define debug(x) cerr << #x " = " << (x) << endl
#define ff first
#define ss second
#define int long long
#define tt tuple<int, int, int>
#define all(x) x.begin(), x.end()
#define Matrix vector<vector<int>>
#define Mat(n, m, v) vector<vector<int>>(n, vector<int>(m, v))
#define endl '\n'

constexpr int INF = 1e18;
constexpr int MOD = 1e9 + 7;
constexpr int MAXN = 1e5 + 3;

int dp[101][MAXN];
int W;
int w[101];
int v[101];

void solve() {
  int n;
  cin >> n;
  cin >> W;

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

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

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

  cout << ans << endl;
}

signed main() {

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

  int t = 1;

#ifdef MULTIPLE_TEST_CASES
  cin >> t;
#endif
  while (t--)
    solve();
}

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

using namespace std;

#define eb emplace_back
#define ii pair<int, int>
#define OK (cerr << "OK" << endl)
#define debug(x) cerr << #x " = " << (x) << endl
#define ff first
#define ss second
#define int long long
#define tt tuple<int, int, int>
#define all(x) x.begin(), x.end()
#define Matrix vector<vector<int>>
#define Mat(n, m, v) vector<vector<int>>(n, vector<int>(m, v))
#define endl '\n'

constexpr int INF = 1e18;
constexpr int MOD = 1e9 + 7;
constexpr int MAXN = 1e5 + 3;

int dp[101][MAXN];
int W;
int w[101];
int v[101];

void solve() {
  int n;
  cin >> n;
  cin >> W;

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

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

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

  cout << ans << endl;
}

signed main() {

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

  int t = 1;

#ifdef MULTIPLE_TEST_CASES
  cin >> t;
#endif
  while (t--)
    solve();
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 1e2 + 2, W = 1e9 + 5, V = 1e3 + 3;

long long dp[2][V];

const long long INF = 1e13 + 13;

int main() {
  int n, ww, vv = 0;
  cin >> n >> ww;
  vector<int> v(n), w(n);
  for (int i = 0; i < n; ++i) {
    cin >> w[i] >> v[i];
    vv += v[i];
  }
  for (int i = n - 1; i >= 0; --i) {
    for (int j = 0; j <= vv; ++j) {
      if (i == n - 1 && j) {
        dp[i & 1][j] = INF;
        if (j - v[i] == 0)
          dp[i & 1][j] = min(dp[i & 1][j], dp[(i + 1) & 1][j - v[i]] + w[i]);
      } else {
        dp[i & 1][j] = dp[(i + 1) & 1][j];
        if (j - v[i] >= 0)
          dp[i & 1][j] = min(dp[i & 1][j], dp[(i + 1) & 1][j - v[i]] + w[i]);
      }
    }
  }
  for (int i = vv; i >= 0; --i)
    if (dp[0][i] <= ww) {
      cout << i;
      return 0;
    }
  cout << 0;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e2 + 2, W = 1e9 + 5, V = 1e5 + 5;

long long dp[2][V];

const long long INF = 1e13 + 13;

int main() {
  int n, ww, vv = 0;
  cin >> n >> ww;
  vector<int> v(n), w(n);
  for (int i = 0; i < n; ++i) {
    cin >> w[i] >> v[i];
    vv += v[i];
  }
  for (int i = n - 1; i >= 0; --i) {
    for (int j = 0; j <= vv; ++j) {
      if (i == n - 1 && j) {
        dp[i & 1][j] = INF;
        if (j - v[i] == 0)
          dp[i & 1][j] = min(dp[i & 1][j], dp[(i + 1) & 1][j - v[i]] + w[i]);
      } else {
        dp[i & 1][j] = dp[(i + 1) & 1][j];
        if (j - v[i] >= 0)
          dp[i & 1][j] = min(dp[i & 1][j], dp[(i + 1) & 1][j - v[i]] + w[i]);
      }
    }
  }
  for (int i = vv; i >= 0; --i)
    if (dp[0][i] <= ww) {
      cout << i;
      return 0;
    }
  cout << 0;
  return 0;
}
Problem p03164 (C++)

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

#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"
#define int long long
#define all(v) v.begin(), v.end()
#define sz(v) (int)v.size()
#define pii pair<int, int>
#define fi first
#define se second
#define forn(i, a, b) for (int i = a; i < b; i++)
#define deb(x) cout << #x << ' ' << x << endl
#define clock                                                                  \
  cerr << 1000 * (double)clock() / (double)CLOCKS_PER_SEC << "ms" << endl;

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

int n, w;
pii p[105];
int cache[105][100005];

int dp(int idx, int val) {
  if (val == 0)
    return 0;
  if (val < 0 || idx == n)
    return 1e15;
  int &ans = cache[idx][val];
  if (ans != -1)
    return ans;
  ans = min(dp(idx + 1, val), p[idx].fi + dp(idx + 1, val - p[idx].se));
  return ans;
}

int32_t main() {
  IOS INPUT();
  cin >> n >> w;
  forn(i, 0, n) cin >> p[i].fi >> p[i].se;
  memset(cache, -1, sizeof(cache));
  int ans = 0;
  forn(i, 1, 1e5 + 1) {
    int mwt = dp(0, i);
    if (mwt <= w)
      ans = i;
  }
  cout << ans << endl;
  return 0;
}


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

#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"
#define int long long
#define all(v) v.begin(), v.end()
#define sz(v) (int)v.size()
#define pii pair<int, int>
#define fi first
#define se second
#define forn(i, a, b) for (int i = a; i < b; i++)
#define deb(x) cout << #x << ' ' << x << endl
#define clock                                                                  \
  cerr << 1000 * (double)clock() / (double)CLOCKS_PER_SEC << "ms" << endl;

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

int n, w;
pii p[105];
int cache[105][100005];

int dp(int idx, int val) {
  if (val == 0)
    return 0;
  if (val < 0 || idx == n)
    return 1e15;
  int &ans = cache[idx][val];
  if (ans != -1)
    return ans;
  ans = min(dp(idx + 1, val), p[idx].fi + dp(idx + 1, val - p[idx].se));
  return ans;
}

int32_t main() {
  IOS // INPUT();
          cin >>
      n >> w;
  forn(i, 0, n) cin >> p[i].fi >> p[i].se;
  memset(cache, -1, sizeof(cache));
  int ans = 0;
  forn(i, 1, 1e5 + 1) {
    int mwt = dp(0, i);
    if (mwt <= w)
      ans = i;
  }
  cout << ans << endl;
  return 0;
}
Problem p03164 (C++)

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

using namespace std;

#define lli long long int
#define REP(i, s, l) for (lli i = s; i < l; i++)
#define DEBUG 0
#define INF (1LL << 50)
#define MOD 1000000007

signed main() {

  lli n, w;
  vector<pair<lli, lli>> p;

  cin >> n >> w;
  REP(i, 0, n) {
    lli a, b;
    cin >> a >> b;
    p.push_back(make_pair(a, b));
  }

  /*価値を引数にwを返す*/
  lli dp[100100] = {INF};
  REP(i, 0, 100100) dp[i] = INF;
  dp[0] = 0;

  REP(i, 0, n) {
    for (lli j = 100000; j >= 0; j--) {
      if (dp[j] == INF)
        continue;
      if (DEBUG)
        cout << "j=" << j << " dp[j]=" << dp[j] << endl;
      lli nextV = j + p[i].second;
      lli nextW = dp[j] + p[i].first;
      if (nextW > w)
        continue;
      if (dp[nextV] > nextW) {
        dp[nextV] = nextW;
        if (DEBUG)
          cout << "nextV=" << nextV << " nextW=" << nextW << endl;
      }
    }
  }

  lli ans = 0;
  REP(i, 0, 1000010) {
    if (dp[i] && dp[i] != INF)
      ans = i;
  }
  cout << ans << endl;

  return 0;
}

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

using namespace std;

#define lli long long int
#define REP(i, s, l) for (lli i = s; i < l; i++)
#define DEBUG 0
#define INF (1LL << 50)
#define MOD 1000000007

signed main() {

  lli n, w;
  vector<pair<lli, lli>> p;

  cin >> n >> w;
  REP(i, 0, n) {
    lli a, b;
    cin >> a >> b;
    p.push_back(make_pair(a, b));
  }

  /*価値を引数にwを返す*/
  lli dp[100100] = {INF};
  REP(i, 0, 100100) dp[i] = INF;
  dp[0] = 0;

  REP(i, 0, n) {
    for (lli j = 100000; j >= 0; j--) {
      if (dp[j] == INF)
        continue;
      if (DEBUG)
        cout << "j=" << j << " dp[j]=" << dp[j] << endl;
      lli nextV = j + p[i].second;
      lli nextW = dp[j] + p[i].first;
      if (nextW > w)
        continue;
      if (dp[nextV] > nextW) {
        dp[nextV] = nextW;
        if (DEBUG)
          cout << "nextV=" << nextV << " nextW=" << nextW << endl;
      }
    }
  }

  lli ans = 0;
  REP(i, 0, 100010) {
    if (dp[i] && dp[i] != INF)
      ans = i;
  }
  cout << ans << endl;

  return 0;
}
Problem p03164 (C++)

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

const long long INF = 1e18 + 10;
const int MAX_V = 1e5 + 10;

int main() {

  int n, max_w;
  scanf("%d%d", &n, &max_w);

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

  vector<vector<long long>> dp(n + 1, vector<long long>(MAX_V, INF));

  dp[0][0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= MAX_V; j++) {
      if (dp[i][j] != INF) {
        dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
        dp[i + 1][j + v[i]] = min(dp[i + 1][j + v[i]], dp[i][j] + w[i]);
      }
    }
  }

  long long result = 0;
  for (int i = dp[n].size() - 1; i >= 0; i--) {
    if (dp[n][i] <= max_w) {
      result = i;
      break;
    }
  }

  cout << result;

  return 0;
}

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

const long long INF = 1e18 + 10;
const int MAX_V = 1e5 + 10;

int main() {

  int n, max_w;
  scanf("%d%d", &n, &max_w);

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

  vector<vector<long long>> dp(n + 1, vector<long long>(MAX_V, INF));

  dp[0][0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < MAX_V; j++) {
      if (dp[i][j] != INF) {
        dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
        dp[i + 1][j + v[i]] = min(dp[i + 1][j + v[i]], dp[i][j] + w[i]);
      }
    }
  }

  long long result = 0;
  for (int i = dp[n].size() - 1; i >= 0; i--) {
    if (dp[n][i] <= max_w) {
      result = i;
      break;
    }
  }

  cout << result;

  return 0;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define loop(i, x, n) for (int i = (x); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define int long long
using namespace std;
const int MOD = 1e9 + 7;
const int INF = 1e10;
template <typename T> void cmax(T &a, T b) { a = max(a, b); }
template <typename T> void cmin(T &a, T b) { a = min(a, b); }

signed main() {
  int N, W;
  cin >> N >> W;
  vector<int> w(N), v(N);
  rep(i, N) cin >> w[i] >> v[i];
  vector<vector<int>> dp(101, vector<int>(100005, INF));
  dp[0][0] = 0;
  rep(i, N) {
    rep(j, 100005) {
      cmin(dp[i + 1][j], dp[i][j]);
      cmin(dp[i + 1][j + v[i]], dp[i][j] + w[i]);
    }
  }
  int ans = 0;
  rep(i, 100005) if (dp[N][i] <= W) cmax(ans, i);
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define loop(i, x, n) for (int i = (x); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define int long long
using namespace std;
const int MOD = 1e9 + 7;
const int INF = 1e10;
template <typename T> void cmax(T &a, T b) { a = max(a, b); }
template <typename T> void cmin(T &a, T b) { a = min(a, b); }

signed main() {
  int N, W;
  cin >> N >> W;
  vector<int> w(N), v(N);
  rep(i, N) cin >> w[i] >> v[i];
  vector<vector<int>> dp(101, vector<int>(100005, INF));
  dp[0][0] = 0;
  rep(i, N) {
    rep(j, 100005) {
      cmin(dp[i + 1][j], dp[i][j]);
      if (100001 < j + v[i])
        continue;
      cmin(dp[i + 1][j + v[i]], dp[i][j] + w[i]);
    }
  }
  int ans = 0;
  rep(i, 100005) if (dp[N][i] <= W) cmax(ans, i);
  cout << ans << endl;

  return 0;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
const int INF = 1e9;
const int MOD = 1e9 + 7;
const long long LINF = 1e18;
#define dump(x) cout << 'x' << ' = ' << (x) << ` `;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOREACH(x, a) for (auto &(x) : (a))
typedef long long ll;
using namespace std;

int n, W;
ll dp[105][100020];

int main(int argc, char const *argv[]) {
  cin >> n >> W;
  vector<ll> w(n);
  vector<ll> v(n);
  for (int i = 0; i < n; ++i)
    cin >> w[i] >> v[i];
  REP(i, n + 10) REP(j, 100010) dp[i][j] = LINF;
  REP(i, n) dp[i][0] = 0;
  REP(i, n) REP(j, 100010) {
    dp[i + 1][j] = dp[i][j];
    if (j - v[i] < 0)
      continue;
    else
      dp[i + 1][j] = min(dp[i][j - v[i]] + w[i], dp[i][j]);
  }
  ll ans = 0;
  for (ll i = 0; i < 100010; ++i) {
    if (dp[n][i] > W)
      continue;
    else
      ans = max(ans, i);
  }
  cout << ans << std::endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
const int INF = 1e9;
const int MOD = 1e9 + 7;
const long long LINF = 1e18;
#define dump(x) cout << 'x' << ' = ' << (x) << ` `;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOREACH(x, a) for (auto &(x) : (a))
typedef long long ll;
using namespace std;

int n, W;
ll dp[105][100020];

int main(int argc, char const *argv[]) {
  cin >> n >> W;
  vector<ll> w(n);
  vector<ll> v(n);
  for (int i = 0; i < n; ++i)
    cin >> w[i] >> v[i];
  REP(i, n) REP(j, 100010) dp[i][j] = LINF;
  REP(i, n) dp[i][0] = 0;
  REP(i, n) REP(j, 100010) {
    dp[i + 1][j] = dp[i][j];
    if (j - v[i] < 0)
      continue;
    else
      dp[i + 1][j] = min(dp[i][j - v[i]] + w[i], dp[i][j]);
  }
  ll ans = 0;
  for (ll i = 0; i < 100010; ++i) {
    if (dp[n][i] > W)
      continue;
    else
      ans = max(ans, i);
  }
  cout << ans << std::endl;
  return 0;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int long long
typedef unsigned long long ull;
typedef long double ld;
#define endl "\n"
const int MAX = 300005;
const long long mod = 1e9 + 7;
int n, w;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  int i, j, x = 0, y;
  cin >> n >> w;
  int v[n + 1], wt[n + 1];
  for (i = 0; i < n; i++) {
    cin >> wt[i] >> v[i];
    x += v[i];
  }
  int dp[n + 1][x + 1];
  for (i = 1; i <= x; i++) {
    dp[0][i] = INT_MAX;
  }
  dp[0][0] = 0;
  for (i = 1; i <= n; i++) {
    for (j = 0; j <= x; j++) {
      dp[i][j] = INT_MAX;
    }
    for (j = 0; j <= x; j++) {
      dp[i][j] = min(dp[i][j], dp[i - 1][j]);
      if (j >= v[i - 1]) {
        dp[i][j] = min(dp[i][j], wt[i - 1] + dp[i - 1][j - v[i - 1]]);
      }
    }
  }
  int ans = 0;
  for (i = 0; i <= x; i++) {
    if (dp[n][i] <= w) {
      ans = max(ans, i);
    }
  }
  /*for(i=1;i<=n;i++)
  {
      for(j=1;j<=x;j++)
      {
          cout<<dp[i][j]<<" ";
      }
      cout<<endl;
  }*/
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int long long
typedef unsigned long long ull;
typedef long double ld;
#define endl "\n"
const int MAX = 300005;
const long long mod = 1e9 + 7;
int n, w;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int i, j, x = 0, y;
  cin >> n >> w;
  int v[n + 1], wt[n + 1];
  for (i = 0; i < n; i++) {
    cin >> wt[i] >> v[i];
    x += v[i];
  }
  int dp[n + 1][x + 1];
  for (i = 1; i <= x; i++) {
    dp[0][i] = INT_MAX;
  }
  dp[0][0] = 0;
  for (i = 1; i <= n; i++) {
    for (j = 0; j <= x; j++) {
      dp[i][j] = INT_MAX;
    }
    for (j = 0; j <= x; j++) {
      dp[i][j] = min(dp[i][j], dp[i - 1][j]);
      if (j >= v[i - 1]) {
        dp[i][j] = min(dp[i][j], wt[i - 1] + dp[i - 1][j - v[i - 1]]);
      }
    }
  }
  int ans = 0;
  for (i = 0; i <= x; i++) {
    if (dp[n][i] <= w) {
      ans = max(ans, i);
    }
  }
  /*for(i=1;i<=n;i++)
  {
      for(j=1;j<=x;j++)
      {
          cout<<dp[i][j]<<" ";
      }
      cout<<endl;
  }*/
  cout << ans << endl;
}