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

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

using namespace std;

#define mp make_pair
#define fi first
#define se second
#define all(v) v.begin(), v.end()
#define allarr(a) a, a + n
#define ll long long
#define pb push_back
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef pair<int, pi> trp;
typedef vector<pi> vpi;
typedef vector<pll> vpll;
ll _abs(ll x) { return (x > 0 ? x : -x); }

const int N = 105, MV = 1e3 + 2;
int n, W, v[N], w[N], V;
ll dp[N][MV]; // max value at index i with weight
int main() {
  fastio;
  cin >> n >> W;
  for (int i = 0; i < n; i++) {
    cin >> w[i] >> v[i];
    V += v[i];
  }
  ll ans = 0;
  for (int i = 0; i <= V; i++)
    dp[0][i] = 2e9;
  dp[0][0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= V; j++) {
      ll &r = dp[i][j];
      r = 2e9;
      r = min(r, dp[i - 1][j]);
      if (j - v[i - 1] >= 0)
        r = min(r, dp[i - 1][j - v[i - 1]] + w[i - 1]);
      if (r <= W)
        ans = max(ans, (ll)j);
      //      cout<<i<<" "<<j<<" "<<r<<endl;
    }
  }
  cout << ans << endl;
}

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

using namespace std;

#define mp make_pair
#define fi first
#define se second
#define all(v) v.begin(), v.end()
#define allarr(a) a, a + n
#define ll long long
#define pb push_back
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef pair<int, pi> trp;
typedef vector<pi> vpi;
typedef vector<pll> vpll;
ll _abs(ll x) { return (x > 0 ? x : -x); }

const int N = 105, MV = 1e5 + 5;
int n, W, v[N], w[N], V;
ll dp[N][MV]; // max value at index i with weight
int main() {
  fastio;
  cin >> n >> W;
  for (int i = 0; i < n; i++) {
    cin >> w[i] >> v[i];
    V += v[i];
  }
  ll ans = 0;
  for (int i = 0; i <= V; i++)
    dp[0][i] = 2e9;
  dp[0][0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= V; j++) {
      ll &r = dp[i][j];
      r = 2e9;
      r = min(r, dp[i - 1][j]);
      if (j - v[i - 1] >= 0)
        r = min(r, dp[i - 1][j - v[i - 1]] + w[i - 1]);
      if (r <= W)
        ans = max(ans, (ll)j);
      //      cout<<i<<" "<<j<<" "<<r<<endl;
    }
  }
  cout << ans << endl;
}
Problem p03164 (C++)

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

#define db(x) // cerr << #x << " == " << x << endl
#define all(container) container.begin(), container.end()
#define mp(i, j) make_pair(i, j)
#define pb push_back

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

template <typename T> void print_vector_debug(const T &t) {
  cout << "[DEBUG] VECTOR:";
  for (auto i = t.cbegin(); i != t.cend(); ++i) {
    if ((i + 1) != t.cend()) {
      cout << *i << " ";
    } else {
      cout << *i << endl;
    }
  }
}

#define MAXN 105
#define MAXV 100005
#define INF (ll)20000000000000000LL

ll N, W;
ll v[MAXN], w[MAXN];
int vis[MAXN][MAXV];
ll dp[MAXN][MAXV];

ll solve_dp(int idx, ll r) {
  if (idx == N) {
    return r <= 0 ? 0 : INF;
  }

  if (r <= 0) {
    return 0;
  }

  if (vis[idx][r]) {
    return dp[idx][r];
  }
  vis[idx][r] = 1;

  ll res = min(solve_dp(idx + 1, r), solve_dp(idx + 1, r - v[idx]) + w[idx]);
  dp[idx][r] = res;

  return res;
}

ll solve() {
  ll upper = 2000000;
  ll lower = 0;
  ll mid, best;

  while (lower <= upper) {
    mid = lower + (upper - lower) / 2;

    db(upper);
    db(lower);
    db(mid);

    if (solve_dp(0, mid) <= W) {
      db("possible");
      best = mid;
      lower = mid + 1;
    } else {
      db("impossible");
      upper = mid - 1;
    }
  }

  return best;
}

int main() {
  scanf("%lld%lld", &N, &W);

  for (int i = 0; i < N; i++) {
    scanf("%lld%lld", &w[i], &v[i]);
  }

  printf("%lld\n", solve());

  return 0;
}


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

#define db(x) // cerr << #x << " == " << x << endl
#define all(container) container.begin(), container.end()
#define mp(i, j) make_pair(i, j)
#define pb push_back

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

template <typename T> void print_vector_debug(const T &t) {
  cout << "[DEBUG] VECTOR:";
  for (auto i = t.cbegin(); i != t.cend(); ++i) {
    if ((i + 1) != t.cend()) {
      cout << *i << " ";
    } else {
      cout << *i << endl;
    }
  }
}

#define MAXN 105
#define MAXV 100005
#define INF (ll)20000000000000000LL

ll N, W;
ll v[MAXN], w[MAXN];
int vis[MAXN][MAXV];
ll dp[MAXN][MAXV];

ll solve_dp(int idx, ll r) {
  if (idx == N) {
    return r <= 0 ? 0 : INF;
  }

  if (r <= 0) {
    return 0;
  }

  if (vis[idx][r]) {
    return dp[idx][r];
  }
  vis[idx][r] = 1;

  ll res = min(solve_dp(idx + 1, r), solve_dp(idx + 1, r - v[idx]) + w[idx]);
  dp[idx][r] = res;

  return res;
}

ll solve() {
  ll upper = MAXV - 1;
  ll lower = 0;
  ll mid, best;

  while (lower <= upper) {
    mid = lower + (upper - lower) / 2;

    db(upper);
    db(lower);
    db(mid);

    if (solve_dp(0, mid) <= W) {
      db("possible");
      best = mid;
      lower = mid + 1;
    } else {
      db("impossible");
      upper = mid - 1;
    }
  }

  return best;
}

int main() {
  scanf("%lld%lld", &N, &W);

  for (int i = 0; i < N; i++) {
    scanf("%lld%lld", &w[i], &v[i]);
  }

  printf("%lld\n", solve());

  return 0;
}
Problem p03164 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i > 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;

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

  vector<ll> w(N), v(N);
  REP(i, N)
  cin >> w[i] >> v[i];

  int V = accumulate(ALL(v), 0);
  ll dp[N + 1][V + 1];
  REP(i, V + 1)
  dp[0][i] = INF;

  REP(i, N)
  REP(j, V + 1) {
    if (j <= v[i]) {
      dp[i + 1][j] = min(dp[i][j], w[i]);
    } else {
      FOR(k, 1, j - v[i] + 1) {
        dp[i + 1][k + v[i]] = min(dp[i][k] + w[i], dp[i][k + v[i]]);
      }
    }
  }

  REPR(i, V) {
    if (dp[N][i] <= W) {
      cout << i << endl;
      break;
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i > 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;

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

  vector<ll> w(N), v(N);
  REP(i, N)
  cin >> w[i] >> v[i];

  int V = accumulate(ALL(v), 0);
  ll dp[N + 1][V + 1];
  REP(i, V + 1)
  dp[0][i] = INF;

  REP(i, N)
  REP(j, V + 1)
  dp[i + 1][j] = min((j <= v[i] ? 0 : dp[i][j - v[i]]) + w[i], dp[i][j]);

  REPR(i, V) {
    if (dp[N][i] <= W) {
      cout << i << endl;
      break;
    }
  }
  return 0;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  freopen("caitui.inp", "r", stdin);
  long long int n, w;
  cin >> n >> w;
  long long int m[n + 10], v[n + 10];

  long long int tong = 0;
  for (int i = 1; i <= n; i++) {
    cin >> m[i] >> v[i];
    tong += v[i];
  }
  long long int f[tong + 10];
  for (int i = 1; i <= tong; i++) {
    f[i] = 1000000000;
  }
  f[0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int t = tong; t >= v[i]; t--) {
      f[t] = min(m[i] + f[t - v[i]], f[t]);
    }
  }
  for (int t = tong; t >= v[1]; t--) {
    if (f[t] <= w) {
      cout << t;
      return 0;
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // freopen("caitui.inp", "r", stdin);
  long long int n, w;
  cin >> n >> w;
  long long int m[n + 10], v[n + 10];

  long long int tong = 0;
  for (int i = 1; i <= n; i++) {
    cin >> m[i] >> v[i];
    tong += v[i];
  }
  long long int f[tong + 10];
  for (int i = 1; i <= tong; i++) {
    f[i] = 1000000000;
  }
  f[0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int t = tong; t >= v[i]; t--) {
      f[t] = min(m[i] + f[t - v[i]], f[t]);
    }
  }
  for (int t = tong; t >= v[1]; t--) {
    if (f[t] <= w) {
      cout << t;
      return 0;
    }
  }
}
Problem p03164 (C++)

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

int const inf = 1e9 + 7;
int n, W, v[101], w[101], dp[101][1100000];

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

int main() {
  cin >> n >> W;
  for (int i = 0; i < n; i++) {
    cin >> w[i] >> v[i];
  }
  for (int i = 0; i < 101; i++)
    for (int j = 0; j < 1100000; j++)
      dp[i][j] = inf;
  dp[0][0] = 0;
  for (int i = 1; i <= n; i++) {
    // for(int j = 0; j < 1100000; j++)dp[i][j] = inf;
    for (int j = 0; j < 1100000; j++) {
      dp[i][j] = dp[i - 1][j];
      if (j - v[i - 1] >= 0) {
        dp[i][j] = min(dp[i][j], dp[i - 1][j - v[i - 1]] + w[i - 1]);
      }
    }
  }
  int mmax = 0;
  for (int i = 0; i < 11000000; i++) {
    if (dp[n][i] <= W)
      mmax = i;
    // cout << dp[n][i]  << ' ';
  }
  cout << mmax << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;

int const inf = 1e9 + 7;
int n, W, v[101], w[101], dp[101][1100000];

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

int main() {
  cin >> n >> W;
  for (int i = 0; i < n; i++) {
    cin >> w[i] >> v[i];
  }
  for (int i = 0; i < 101; i++)
    for (int j = 0; j < 1100000; j++)
      dp[i][j] = inf;
  dp[0][0] = 0;
  for (int i = 1; i <= n; i++) {
    // for(int j = 0; j < 1100000; j++)dp[i][j] = inf;
    for (int j = 0; j < 1100000; j++) {
      dp[i][j] = dp[i - 1][j];
      if (j - v[i - 1] >= 0) {
        dp[i][j] = min(dp[i][j], dp[i - 1][j - v[i - 1]] + w[i - 1]);
      }
    }
  }
  int mmax = 0;
  for (int i = 0; i < 1100000; i++) {
    if (dp[n][i] <= W)
      mmax = i;
    // cout << dp[n][i]  << ' ';
  }
  cout << mmax << endl;
  return 0;
}
Problem p03164 (Python)

Buggy submission (status: Time Limit Exceeded):
N, W = [int(_) for _ in input().split()]
WV = [[int(_) for _ in input().split()] for _ in range(N)]
dp = {}
dp[0] = 0
for w, v in WV:
    dp_o = dp.copy()
    for k in dp_o:
        if k + w <= W:
            dp[k + w] = max(dp_o.get(k + w, 0), dp_o[k] + v)
print(max([v for k, v in dp.items()]))


Fixed submission:
N, W = [int(_) for _ in input().split()]
WV = [[int(_) for _ in input().split()] for _ in range(N)]
dp = {}
dp[0] = 0
for w, v in WV:
    dp_o = dp.copy()
    for k in dp_o:
        dp[k + v] = min(dp_o.get(k + v, float("inf")), dp_o[k] + w)
print(max([k for k, v in dp.items() if v <= W]))
Problem p03164 (C++)

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

#define f first
#define s second
#define pb push_back
#define mp make_pair
#define ll long long int
typedef pair<int, int> pii;
vector<int> v;
map<int, int> op;
#define MAX 200005
bool visited[MAX];
vector<int> adj[MAX];
const int M = 1e9 + 7;
int n, m;
const ll INF = 1LL << 60;

ll solve(int n, int w, int wt[], int val[]) {
  vector<ll> dp(100001, LLONG_MAX);
  dp[0] = 0;
  for (int i = 0; i <= n; i++) {
    int w = wt[i], v = val[i];
    for (int j = 100000; j >= v; j--) {
      if (dp[j - v] == LLONG_MAX)
        continue;
      dp[j] = min(dp[j], dp[j - v] + w);
    }
  }
  for (int i = 100000; i >= 0; i--)
    if (dp[i] <= w)
      return i;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int w;
  cin >> n >> w;
  int wt[n], val[n];
  for (int i = 0; i < n; i++)
    cin >> wt[i] >> val[i];
  cout << solve(n, w, wt, val) << endl;
  return 0;
}


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

#define f first
#define s second
#define pb push_back
#define mp make_pair
#define ll long long int
typedef pair<int, int> pii;
vector<int> v;
map<int, int> op;
#define MAX 200005
bool visited[MAX];
vector<int> adj[MAX];
const int M = 1e9 + 7;
int n, m;
const ll INF = 1LL << 60;

ll solve(int n, int w, int wt[], int val[]) {
  vector<ll> dp(100001, LLONG_MAX);
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    int w = wt[i], v = val[i];
    for (int j = 100000; j >= v; j--) {
      if (dp[j - v] == LLONG_MAX)
        continue;
      dp[j] = min(dp[j], dp[j - v] + w);
    }
  }
  for (int i = 100000; i >= 0; i--)
    if (dp[i] <= w)
      return i;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int w;
  cin >> n >> w;
  int wt[n], val[n];
  for (int i = 0; i < n; i++)
    cin >> wt[i] >> val[i];
  cout << solve(n, w, wt, val) << endl;
  return 0;
}
Problem p03164 (C++)

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

typedef long long ll;

#define INF 1000000000001
#define N 101
#define VALUE 10001
int n, weight;
ll w[N], v[N], dp[VALUE], mx;

signed main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

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

    mx += v[i];
  }

  for (int i = 1; i <= mx; i++)
    dp[i] = INF;

  for (int j = 1; j <= n; j++)
    for (int i = mx; i >= v[j]; i--)
      dp[i] = min(dp[i], dp[i - v[j]] + w[j]);

  for (int i = mx; i >= 0; i--)
    if (dp[i] <= weight) {
      cout << i << '\n';

      return 0;
    }
}


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

typedef long long ll;

#define INF 1000000000001
#define N 101
#define VALUE 100001
int n, weight;
ll w[N], v[N], dp[VALUE], mx;

signed main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

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

    mx += v[i];
  }

  for (int i = 1; i <= mx; i++)
    dp[i] = INF;

  for (int j = 1; j <= n; j++)
    for (int i = mx; i >= v[j]; i--)
      dp[i] = min(dp[i], dp[i - v[j]] + w[j]);

  for (int i = mx; i >= 0; i--)
    if (dp[i] <= weight) {
      cout << i << '\n';

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

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

using namespace std;

const int64_t INF = 1e18L + 5;
const int MAX_W_V_SIZE = 105, M = 2e3 + 5;
int64_t W[105], V[105], dp[M];

int main() {
  int n, w, max_val;
  cin >> n >> w;
  max_val = 0;

  for (int i = 0; i < n; i++)
    cin >> W[i] >> V[i], max_val += V[i];
  for (int i = 0; i <= M; i++)
    dp[i] = INF;

  dp[0] = 0;
  for (int k = 0; k < n; k++) {
    for (int x = max_val - V[k]; x >= 0; x--) {
      if (dp[x] || !dp[0]) {
        dp[x + V[k]] = min(dp[x + V[k]], W[k] + dp[x]);
      }
    }
  }

  int64_t m = 0;
  for (int i = 0; i <= max_val; i++) {
    // cout << dp[i] << " ";
    if (dp[i] <= w) {
      m = max(m, (int64_t)i);
    }
  }

  cout << m;

  return 0;
}


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

using namespace std;

const int64_t INF = 1e18L + 5;
const int MAX_W_V_SIZE = 105, M = 2e5 + 5;
int64_t W[105], V[105], dp[M];

int main() {
  int n, w, max_val;
  cin >> n >> w;
  max_val = 0;

  for (int i = 0; i < n; i++)
    cin >> W[i] >> V[i], max_val += V[i];
  for (int i = 0; i <= M; i++)
    dp[i] = INF;

  dp[0] = 0;
  for (int k = 0; k < n; k++) {
    for (int x = max_val - V[k]; x >= 0; x--) {
      if (dp[x] || !dp[0]) {
        dp[x + V[k]] = min(dp[x + V[k]], W[k] + dp[x]);
      }
    }
  }

  int64_t m = 0;
  for (int i = 0; i <= max_val; i++) {
    // cout << dp[i] << " ";
    if (dp[i] <= w) {
      m = max(m, (int64_t)i);
    }
  }

  cout << m;

  return 0;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long ll;
typedef pair<ll, ll> P;
const ll mod = 1000000007;
const ll LINF = 1LL << 60;
const int INF = 1 << 30;

int main() {
  ll n, W;
  cin >> n >> W;
  vector<ll> w(n);
  vector<ll> v(n);
  for (int i = 0; i < n; i++) {
    cin >> w[i] >> v[i];
  }
  vector<vector<ll>> dp(n + 1, vector<ll>(100001, LINF));
  dp[0][0] = 0LL;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= 100000; j++) {
      dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - v[i - 1]] + w[i - 1]);
    }
  }
  for (int i = 100000; i >= 0; i--) {
    if (dp[n][i] <= W) {
      cout << i << endl;
      break;
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long ll;
typedef pair<ll, ll> P;
const ll mod = 1000000007;
const ll LINF = 1LL << 60;
const int INF = 1 << 30;

int main() {
  ll n, W;
  cin >> n >> W;
  vector<ll> w(n);
  vector<ll> v(n);
  for (int i = 0; i < n; i++) {
    cin >> w[i] >> v[i];
  }
  vector<vector<ll>> dp(n + 1, vector<ll>(100001, LINF));
  dp[0][0] = 0LL;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= 100000; j++) {
      if (j - v[i - 1] >= 0) {
        dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - v[i - 1]] + w[i - 1]);
      } else {
        dp[i][j] = dp[i - 1][j];
      }
    }
  }
  for (int i = 100000; i >= 0; i--) {
    if (dp[n][i] <= W) {
      cout << i << endl;
      break;
    }
  }
  return 0;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long c[105], v[105];
long long dp[105][1005];
#define inf 1e9 + 5
int main() {
  long long n, w;
  cin >> n >> w;
  long long v_sum = 0;
  for (int i = 1; i <= n; i++) {
    cin >> c[i] >> v[i];
    v_sum += v[i];
  }
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= v_sum; j++) {
      dp[i][j] = inf;
    }
  }
  //	for(int i = 0; i<=1004s
  for (int i = 0; i <= n; i++) {
    dp[i][0] = 0;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= v_sum; j++) {
      if (i - 1 >= 0 && j - v[i] >= 0)
        dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - v[i]] + c[i]);
      else
        dp[i][j] = dp[i - 1][j];
    }
  }
  long long ans = 0;

  for (long long i = 1; i <= n; i++) {
    for (long long j = 0; j <= v_sum; j++) {
      //			cout << dp[i][j] << " ";
      if (dp[i][j] <= w) {
        ans = max(j, ans);
      }
    }
    //		cout << endl;
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long c[105], v[105];
long long dp[105][100005];
#define inf 1e9 + 5
int main() {
  long long n, w;
  cin >> n >> w;
  long long v_sum = 0;
  for (int i = 1; i <= n; i++) {
    cin >> c[i] >> v[i];
    v_sum += v[i];
  }
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= v_sum; j++) {
      dp[i][j] = inf;
    }
  }
  //	for(int i = 0; i<=1004s
  for (int i = 0; i <= n; i++) {
    dp[i][0] = 0;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= v_sum; j++) {
      if (i - 1 >= 0 && j - v[i] >= 0)
        dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - v[i]] + c[i]);
      else
        dp[i][j] = dp[i - 1][j];
    }
  }
  long long ans = 0;

  for (long long i = 1; i <= n; i++) {
    for (long long j = 0; j <= v_sum; j++) {
      //			cout << dp[i][j] << " ";
      if (dp[i][j] <= w) {
        ans = max(j, ans);
      }
    }
    //		cout << endl;
  }
  cout << ans << endl;
}
Problem p03164 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <unordered_map>

using namespace std;

const int w_max = 1e9 + 1;
const int v_max = 1e5 + 10;
const int size = 1e2 + 1;
int memo[v_max][size];
bool vis[v_max][size];
int v[size];
int w[size];

int n, w_tot;

int dp(int vtmp, int i) {
  if (vtmp <= 0)
    return 0;
  if (i == n)
    return w_max;
  if (vis[vtmp][i])
    return memo[vtmp][i];

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

int answer(int v_tot) {
  int ans = 0;
  for (int i = v_tot; i >= 0; i--) {
    if (dp(i, 0) <= w_tot)
      return i;
  }
  return 0;
}

int main() {

  scanf("%d %d", &n, &w_tot);
  int v_tot = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d %d", &w[i], &v[i]);
    v_tot += v[i];
  }
  printf("%d\n", answer(v_tot));

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <unordered_map>

using namespace std;

const int w_max = 1e9 + 1;
const int v_max = 1e5 + 10;
const int size = 1e2 + 1;
int memo[v_max][size];
bool vis[v_max][size];
int v[size];
int w[size];

int n, w_tot;

int dp(int vtmp, int i) {
  if (vtmp <= 0)
    return 0;
  if (i == n)
    return w_max;
  if (vis[vtmp][i])
    return memo[vtmp][i];

  vis[vtmp][i] = 1;

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

int answer(int v_tot) {
  int ans = 0;
  for (int i = v_tot; i >= 0; i--) {
    if (dp(i, 0) <= w_tot)
      return i;
  }
  return 0;
}

int main() {

  scanf("%d %d", &n, &w_tot);
  int v_tot = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d %d", &w[i], &v[i]);
    v_tot += v[i];
  }
  printf("%d\n", answer(v_tot));

  return 0;
}
Problem p03164 (C++)

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

typedef long long ll;
const ll INF = 1e+12;

int main() {
  int N, W, sval = 0, ans = 0;
  static ll dp[110][1010];
  cin >> N >> W;
  vector<int> w(N), v(N);
  for (int i = 0; i < N; ++i) {
    cin >> w[i] >> v[i];
    sval += v[i];
  }
  for (int i = 0; i <= N; ++i)
    for (int j = 1; j <= sval; ++j)
      dp[i][j] = INF;
  for (int i = 1; i <= N; ++i) {
    for (int j = 1; j <= sval; ++j) {
      if (j >= v[i - 1]) {
        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 - 1][j], dp[i][j]);
    }
  }
  for (int i = 1; i <= sval; ++i)
    if (dp[N][i] <= W)
      ans = i;
  cout << ans << endl;
  return 0;
}

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

typedef long long ll;
const ll INF = 1e+12;

int main() {
  int N, W, sval = 0, ans = 0;
  static ll dp[110][100010];
  cin >> N >> W;
  vector<int> w(N), v(N);
  for (int i = 0; i < N; ++i) {
    cin >> w[i] >> v[i];
    sval += v[i];
  }
  for (int i = 0; i <= N; ++i)
    for (int j = 1; j <= sval; ++j)
      dp[i][j] = INF;
  for (int i = 1; i <= N; ++i) {
    for (int j = 1; j <= sval; ++j) {
      if (j >= v[i - 1]) {
        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 - 1][j], dp[i][j]);
    }
  }
  for (int i = 1; i <= sval; ++i)
    if (dp[N][i] <= W)
      ans = i;
  cout << ans << endl;
  return 0;
}
Problem p03164 (C++)

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

ll N, K;
ll DP[100005][102];
ll W[102], V[102];
ll INF = 1e18;

int main() {

  cin >> N >> K;
  for (int i = 0; i < N; i++)
    cin >> W[i + 1] >> V[i + 1];

  for (int i = 1; i < 1003 * N; i++)
    DP[i][0] = INF;

  for (int i = 0; i < N * 1003; i++)
    for (int j = 1; j < N + 1; j++)
      DP[i][j] = min(DP[i][j - 1],
                     ((i - V[j] >= 0) ? (DP[i - V[j]][j - 1] + W[j]) : INF));

  for (int i = N * 1003 - 1; i >= 0; i--)
    if (DP[i][N] <= K) {
      cout << i;
      return 0;
    }
}

Fixed submission:
#include <iostream>
using namespace std;
typedef long long int ll;

ll N, K;
ll DP[110005][102];
ll W[102], V[102];
ll INF = 1e18;

int main() {

  cin >> N >> K;
  for (int i = 0; i < N; i++)
    cin >> W[i + 1] >> V[i + 1];

  for (int i = 1; i < 1003 * N; i++)
    DP[i][0] = INF;

  for (int i = 0; i < N * 1003; i++)
    for (int j = 1; j < N + 1; j++)
      DP[i][j] = min(DP[i][j - 1],
                     ((i - V[j] >= 0) ? (DP[i - V[j]][j - 1] + W[j]) : INF));

  for (int i = N * 1003 - 1; i >= 0; i--)
    if (DP[i][N] <= K) {
      cout << i;
      return 0;
    }
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define MX 100000
using namespace std;
typedef long long lnt;
template <class T> inline void read(T &x) {
  x = 0;
  int c = getchar(), f = 1;
  for (; !isdigit(c); c = getchar())
    if (c == 45)
      f = -1;
  for (; isdigit(c); c = getchar())
    (x *= 10) += f * (c - '0');
}
int n, w[105], v[105];
lnt m, f[MX + 5];
int main() {
  read(n), read(m);
  for (int i = 1; i <= n; i++)
    read(w[i]), read(v[i]);
  memset(f, 0x3f, sizeof f), f[0] = 0;
  for (int i = 1; i <= n; i++)
    for (int j = MX; ~j; j--)
      f[j] = min(f[j], f[j - v[i]] + w[i]);
  for (int i = MX; ~i; i--)
    if (f[i] <= m)
      return printf("%d\n", i), 0;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define MX 100000
using namespace std;
typedef long long lnt;
template <class T> inline void read(T &x) {
  x = 0;
  int c = getchar(), f = 1;
  for (; !isdigit(c); c = getchar())
    if (c == 45)
      f = -1;
  for (; isdigit(c); c = getchar())
    (x *= 10) += f * (c - '0');
}
int n, w[105], v[105];
lnt m, f[MX + 5];
int main() {
  read(n), read(m);
  for (int i = 1; i <= n; i++)
    read(w[i]), read(v[i]);
  memset(f, 0x3f, sizeof f), f[0] = 0;
  for (int i = 1; i <= n; i++)
    for (int j = MX; j >= v[i]; j--)
      f[j] = min(f[j], f[j - v[i]] + w[i]);
  for (int i = MX; ~i; i--)
    if (f[i] <= m)
      return printf("%d\n", i), 0;
  return 0;
}
Problem p03164 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define MOD 1000000007
#define ll long long
#define mp make_pair
#define pb push_back
#define N 110
#define W 100010
using namespace std;
ll dp[N][W], A[N], B[N], n;
ll fun(int a, int b) {
  if (b < 0)
    return 2 * 1e9;
  if (a == n) {
    if (b == 0)
      return 0;
    return 2 * 1e9;
  }
  return dp[a][b] = min(fun(a + 1, b), B[a] + fun(a + 1, b - A[a]));
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int w;
  cin >> n >> w;
  for (int i = 0; i < n; i++)
    cin >> B[i] >> A[i];
  memset(dp, -1, sizeof(dp));
  for (int i = 100000; i >= 0; i--) {
    if (fun(0, i) <= w) {
      cout << i << '\n';
      return 0;
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define MOD 1000000007
#define ll long long
#define mp make_pair
#define pb push_back
#define N 110
#define W 100010
using namespace std;
ll dp[N][W], A[N], B[N], n;
ll fun(int a, int b) {
  if (b < 0)
    return 2 * 1e9;
  if (a == n) {
    if (b == 0)
      return 0;
    return 2 * 1e9;
  }
  if (dp[a][b] != -1)
    return dp[a][b];
  return dp[a][b] = min(fun(a + 1, b), B[a] + fun(a + 1, b - A[a]));
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int w;
  cin >> n >> w;
  for (int i = 0; i < n; i++)
    cin >> B[i] >> A[i];
  memset(dp, -1, sizeof(dp));
  for (int i = 100000; i >= 0; i--) {
    if (fun(0, i) <= w) {
      cout << i << '\n';
      return 0;
    }
  }
}
Problem p03164 (C++)

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

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

  long max_value = 0;

  vector<vector<long>> nap(N, vector<long>(total_val + 1, 1e18));
  for (int i = 0; i < N; i++)
    nap[i][0] = 0;
  for (long j = 1; j <= total_val; j++) {
    if (j <= v[0])
      nap[0][j] = w[0];
    if (nap[0][j] <= W)
      max_value = max(max_value, j);
  }
  for (int i = 1; i < N; i++) {
    for (int j = 1; j <= total_val; j++) {
      if (j <= v[0]) {
        nap[i][j] = min((long)w[i], nap[i - 1][j]);
      } else {
        nap[i][j] = min(nap[i - 1][j - v[i]] + w[i], nap[i - 1][j]);
      }
      if (nap[i][j] <= W)
        max_value = max(max_value, (long)j);
    }
  }

  cout << max_value << endl;
}

int main(int argc, char **argv) {
  Main();
  return 0;
}


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

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

  long max_value = 0;

  vector<vector<long>> nap(N, vector<long>(total_val + 1, 1e18));
  for (int i = 0; i < N; i++)
    nap[i][0] = 0;
  for (long j = 1; j <= total_val; j++) {
    if (j <= v[0])
      nap[0][j] = w[0];
    if (nap[0][j] <= W)
      max_value = max(max_value, j);
  }
  for (int i = 1; i < N; i++) {
    for (int j = 1; j <= total_val; j++) {
      if (j <= v[i]) {
        nap[i][j] = min((long)w[i], nap[i - 1][j]);
      } else {
        nap[i][j] = min(nap[i - 1][j - v[i]] + w[i], nap[i - 1][j]);
      }
      if (nap[i][j] <= W)
        max_value = max(max_value, (long)j);
    }
  }

  cout << max_value << endl;
}

int main(int argc, char **argv) {
  Main();
  return 0;
}
Problem p03164 (C++)

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

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(100, vector<int>(100000 + 100, 1e16));
  int V = *max_element(v.begin(), v.end());
  int ans = 0;
  dp[0][0] = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= V * N; j++) {
      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]);
    }
  }
  for (int j = 0; j <= V * N; j++) {
    if (dp[N][j] <= W)
      ans = j;
  }
  cout << ans << endl;
}

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

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(110, vector<int>(1000000 + 100, 1e16));
  int V = *max_element(v.begin(), v.end());
  int ans = 0;
  dp[0][0] = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= V * N; j++) {
      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]);
    }
  }
  for (int j = 0; j <= V * N; j++) {
    if (dp[N][j] <= W)
      ans = j;
  }
  cout << ans << endl;
}
Problem p03164 (C++)

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

#define pb push_back
#define mp make_pair
#define prior priority_queue
#define MOD 1000000007
#define INF64 (long long int)1e18
#define INF (int)1e9
#define PI 3.1415926535897932384626433832795
#define ll long long int
#define ld long double
#define ret return
#define NUM 1000001
const int SIZE = 2e5 + 4;
void fastio() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}

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

int p[20000];
ll dp[200005];

int main() {
  fastio();
  input();

  ll N;
  ll W;
  cin >> N >> W;
  ll w[N + 1];
  ll v[N + 1];
  ll total_val = 0;
  for (ll i = 1; i <= N; i++) {
    cin >> w[i] >> v[i];
    total_val += v[i];
  }
  ll dp[N + 1][total_val + 1];
  for (ll i = 0; i <= N; i++) {
    for (ll j = 0; j <= total_val; j++) {
      if (j == 0) {
        dp[i][j] = (ll)0;
      } else {
        dp[i][j] = INF64;
      }
    }
  }
  /*	for(int i=0;i<=N;i++){
                  for(int j=0;j<=total_val;j++){
                          cout<<dp[i][j]<<" ";
                  }
                  cout<<endl;
          }*/
  for (ll i = 1; i <= N; i++) {
    for (ll j = 1; j <= total_val; j++) {
      if (v[i] <= j)
        dp[i][j] = min(w[i] + dp[i - 1][j - v[i]], dp[i - 1][j]);
      else
        dp[i][j] = dp[i - 1][j];
    }
  }
  ll ans = 0;
  for (ll i = 1; i <= total_val; i++) {
    if (dp[N][i] <= W) {
      ans = i;
    }
  }
  cout << ans << endl;
  return 0;
}


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

#define pb push_back
#define mp make_pair
#define prior priority_queue
#define MOD 1000000007
#define INF64 (long long int)1e18
#define INF (int)1e9
#define PI 3.1415926535897932384626433832795
#define ll long long int
#define ld long double
#define ret return
#define NUM 1000001
const int SIZE = 2e5 + 4;
void fastio() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}

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

int p[20000];
ll dp[200005];

int main() {
  fastio();
  // input();

  ll N;
  ll W;
  cin >> N >> W;
  ll w[N + 1];
  ll v[N + 1];
  ll total_val = 0;
  for (ll i = 1; i <= N; i++) {
    cin >> w[i] >> v[i];
    total_val += v[i];
  }
  ll dp[N + 1][total_val + 1];
  for (ll i = 0; i <= N; i++) {
    for (ll j = 0; j <= total_val; j++) {
      if (j == 0) {
        dp[i][j] = (ll)0;
      } else {
        dp[i][j] = INF64;
      }
    }
  }
  /*	for(int i=0;i<=N;i++){
                  for(int j=0;j<=total_val;j++){
                          cout<<dp[i][j]<<" ";
                  }
                  cout<<endl;
          }*/
  for (ll i = 1; i <= N; i++) {
    for (ll j = 1; j <= total_val; j++) {
      if (v[i] <= j)
        dp[i][j] = min(w[i] + dp[i - 1][j - v[i]], dp[i - 1][j]);
      else
        dp[i][j] = dp[i - 1][j];
    }
  }
  ll ans = 0;
  for (ll i = 1; i <= total_val; i++) {
    if (dp[N][i] <= W) {
      ans = i;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repl(i, l, r) for (ll i = (l); i < (r); i++)
#define per(i, n) for (ll i = n - 1; i >= 0; i--)
#define perl(i, r, l) for (ll i = r - 1; i >= l; i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x, vector<x>, greater<x>>
#define all(x) (x).begin(), (x).end()
#define CST(x) cout << fixed << setprecision(x)
#define rev(x) reverse(x);
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pl = pair<ll, ll>;
using vpl = vector<pl>;
using vvpl = vector<vpl>;
const ll MOD = 1000000007;
const ll MOD9 = 998244353;
const int inf = 1e9 + 10;
const ll INF = 4e18;
const ll dy[9] = {1, 0, -1, 0, 1, 1, -1, -1, 0};
const ll dx[9] = {0, -1, 0, 1, 1, -1, 1, -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;
}
vvl dp(101, vl(100010, INF));
int main() {
  ll n, w;
  cin >> n >> w;
  vpl it(n);
  rep(i, n) cin >> it[i].fi >> it[i].se;
  dp[0][0] = 0;
  rep(i, n) {
    rep(j, 100005) {
      chmin(dp[i + 1][j + it[i].se], dp[i][j] + it[i].fi);
      chmin(dp[i + 1][j], dp[i][j]);
    }
  }
  per(i, 100005) if (dp[n][i] <= w) cout << i << endl, exit(0);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repl(i, l, r) for (ll i = (l); i < (r); i++)
#define per(i, n) for (ll i = n - 1; i >= 0; i--)
#define perl(i, r, l) for (ll i = r - 1; i >= l; i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x, vector<x>, greater<x>>
#define all(x) (x).begin(), (x).end()
#define CST(x) cout << fixed << setprecision(x)
#define rev(x) reverse(x);
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pl = pair<ll, ll>;
using vpl = vector<pl>;
using vvpl = vector<vpl>;
const ll MOD = 1000000007;
const ll MOD9 = 998244353;
const int inf = 1e9 + 10;
const ll INF = 4e18;
const ll dy[9] = {1, 0, -1, 0, 1, 1, -1, -1, 0};
const ll dx[9] = {0, -1, 0, 1, 1, -1, 1, -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;
}
vvl dp(101, vl(100010, INF));
int main() {
  ll n, w;
  cin >> n >> w;
  vpl it(n);
  rep(i, n) cin >> it[i].fi >> it[i].se;
  dp[0][0] = 0;
  rep(i, n) {
    rep(j, 100005) {
      if (j + it[i].se <= 100001)
        chmin(dp[i + 1][j + it[i].se], dp[i][j] + it[i].fi);
      chmin(dp[i + 1][j], dp[i][j]);
    }
  }
  per(i, 100005) if (dp[n][i] <= w) cout << i << endl, exit(0);
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define repp(i, l, r) for (long long i = (l); i < (r); i++)
#define rep(i, n) for (long long i = 0; i < (n); ++i)
#define per(i, n) for (long long i = (n); i >= 0; --i)
const int INF = 1 << 30; // int max
const long long int MOD = 1000000007;
using namespace std;
using ll = long long;
using P = pair<int, int>;
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;
}

long long dp[105][1005];

// ミョ(-ω- ?)
int main() {
  ll n, w;
  cin >> n >> w;
  vector<ll> weight(n, 0);
  vector<ll> value(n, 0);
  ll max_value = 0;
  rep(i, n) {
    cin >> weight[i] >> value[i];
    max_value = max(max_value, value[i]);
  }
  rep(i, n * max_value + 1) dp[0][i] = (1LL << 60);
  // dp[i][j] = i番目の荷物　価値jまで詰めたときの重さの最小値
  dp[0][0] = 0;
  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j <= n * max_value; j++) {
      if (j < value[i])
        dp[i + 1][j] = dp[i][j];
      else
        dp[i + 1][j] = min(dp[i][j], dp[i][j - value[i]] + weight[i]);
    }
  }
  ll res = 0;
  rep(i, n * max_value + 1) {
    if (dp[n][i] <= w)
      res = i;
  }
  cout << res << "\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define repp(i, l, r) for (long long i = (l); i < (r); i++)
#define rep(i, n) for (long long i = 0; i < (n); ++i)
#define per(i, n) for (long long i = (n); i >= 0; --i)
const int INF = 1 << 30; // int max
const long long int MOD = 1000000007;
using namespace std;
using ll = long long;
using P = pair<int, int>;
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;
}

long long dp[105][1000005];

// ミョ(-ω- ?)
int main() {
  ll n, w;
  cin >> n >> w;
  vector<ll> weight(n, 0);
  vector<ll> value(n, 0);
  ll max_value = 0;
  rep(i, n) {
    cin >> weight[i] >> value[i];
    max_value = max(max_value, value[i]);
  }
  rep(i, n * max_value + 1) dp[0][i] = (1LL << 60);
  // dp[i][j] = i番目の荷物　価値jまで詰めたときの重さの最小値
  dp[0][0] = 0;
  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j <= n * max_value; j++) {
      if (j < value[i])
        dp[i + 1][j] = dp[i][j];
      else
        dp[i + 1][j] = min(dp[i][j], dp[i][j - value[i]] + weight[i]);
    }
  }
  ll res = 0;
  rep(i, n * max_value + 1) {
    if (dp[n][i] <= w)
      res = i;
  }
  cout << res << "\n";
  return 0;
}
Problem p03164 (C++)

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

int solve(int r, int i, int wt[], int val[], int n) {
  if (r <= 0)
    return 0;
  if (i == n)
    return INT_MAX;
  if (dp[r][i] != -1)
    return dp[r][i];

  return dp[r][i] = min(solve(r, i + 1, wt, val, n),
                        wt[i] + solve(r - val[i], i + 1, wt, val, n));
}
int32_t main() {

  int n, w;
  cin >> n >> w;
  int wt[n + 1];
  int val[n + 1];
  int V = 0;
  for (int i = 0; i < n; i++) {
    cin >> wt[i];
    cin >> val[i];
    V += val[i];
  }
  memset(dp, -1, sizeof(dp));
  for (int i = V; i >= 0; i--) {
    if (solve(i, 0, wt, val, n) <= w) {
      cout << i << endl;
      break;
    }
  }
}


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

int solve(int r, int i, int wt[], int val[], int n) {
  if (r <= 0)
    return 0;
  if (i == n)
    return INT_MAX;
  if (dp[r][i] != -1)
    return dp[r][i];

  return dp[r][i] = min(solve(r, i + 1, wt, val, n),
                        wt[i] + solve(r - val[i], i + 1, wt, val, n));
}
int32_t main() {

  int n, w;
  cin >> n >> w;
  int wt[n + 1];
  int val[n + 1];
  int V = 0;
  for (int i = 0; i < n; i++) {
    cin >> wt[i];
    cin >> val[i];
    V += val[i];
  }
  memset(dp, -1, sizeof(dp));
  for (int i = V; i >= 0; i--) {
    if (solve(i, 0, wt, val, n) <= w) {
      cout << i << endl;
      break;
    }
  }
}
Problem p03164 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <iostream>
#define mp make_pair

using namespace std;

typedef long long LL;
typedef pair<int, int> pii;
typedef pair<int, LL> pil;

const int MAXN = 100;
const int MAXV = 1e5;
const int INF = 0x3f3f3f3f;

template <typename T> inline T read() {
  T res = 0, flag = 1;
  char in = getchar();
  while (!isdigit(in)) {
    if (in == '-')
      flag = -1;
    in = getchar();
  }
  while (isdigit(in)) {
    res = (res << 1) + (res << 3) + in - '0';
    in = getchar();
  }
  return res * flag;
}

template <typename T> inline void chkmax(T &a, T b) {
  if (a < b)
    a = b;
}
template <typename T> inline void chkmin(T &a, T b) {
  if (a > b)
    a = b;
}

int dp[MAXV + 10];
int w[MAXN], val[MAXN], n, W;

inline void solve() {
  n = read<int>();
  W = read<int>();
  for (int i = 1; i <= n; ++i)
    w[i] = read<int>(), val[i] = read<int>();

  memset(dp, 0x3f, sizeof dp);
  dp[0] = 0;
  for (int i = 1; i <= n; ++i)
    for (int j = MAXV; j >= val[i]; --j)
      chkmin(dp[j], dp[j - val[i]] + w[i]);

  for (int i = MAXV;; --i)
    if (dp[i] <= W) {
      printf("%d\n", i);
      return;
    }
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("E.in", "r", stdin);
  freopen("E.out", "w", stdout);
#endif

  solve();

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <iostream>
#define mp make_pair

using namespace std;

typedef long long LL;
typedef pair<int, int> pii;
typedef pair<int, LL> pil;

const int MAXN = 100;
const int MAXV = 1e5;
const int INF = 0x3f3f3f3f;

template <typename T> inline T read() {
  T res = 0, flag = 1;
  char in = getchar();
  while (!isdigit(in)) {
    if (in == '-')
      flag = -1;
    in = getchar();
  }
  while (isdigit(in)) {
    res = (res << 1) + (res << 3) + in - '0';
    in = getchar();
  }
  return res * flag;
}

template <typename T> inline void chkmax(T &a, T b) {
  if (a < b)
    a = b;
}
template <typename T> inline void chkmin(T &a, T b) {
  if (a > b)
    a = b;
}

int dp[MAXV + 10];
int w[MAXN], val[MAXN], n, W;

inline void solve() {
  n = read<int>();
  W = read<int>();
  for (int i = 1; i <= n; ++i)
    w[i] = read<int>(), val[i] = read<int>();

  memset(dp, 0x3f, sizeof dp);
  dp[0] = 0;
  for (int i = 1; i <= n; ++i)
    for (int j = MAXV; j >= val[i]; --j)
      chkmin(dp[j], dp[j - val[i]] + w[i]);

  for (int i = MAXV;; --i)
    if (dp[i] <= W) {
      printf("%d\n", i);
      return;
    }
}

int main() {

  solve();

  return 0;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
using ll = long long;
using namespace std;
template <typename T> using vec = std::vector<T>;

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

  vec<ll> w(N), v(N);
  rep(i, N) cin >> w[i] >> v[i];

  ll MAX_VAL = 1000 * 100;
  vec<vec<ll>> dp(N + 1, vec<ll>(MAX_VAL + 100, 1e14 + 5));

  for (int i = 1; i <= N; ++i) {
    dp[i][v[i - 1]] = min(dp[i][v[i - 1]], w[i - 1]);

    for (int j = 0; j <= MAX_VAL; ++j) {
      dp[i][j] = min({dp[i][j], dp[i - 1][j]});

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

  ll ans = 0;

  rep(i, MAX_VAL + 50) if (dp[N][i] <= W) ans = max(ans, i);
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
using ll = long long;
using namespace std;
template <typename T> using vec = std::vector<T>;

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

  vec<ll> w(N), v(N);
  rep(i, N) cin >> w[i] >> v[i];

  ll MAX_VAL = 10000 * N;
  vec<vec<ll>> dp(N + 1, vec<ll>(MAX_VAL + 100, 1e14 + 5));

  for (int i = 1; i <= N; ++i) {
    dp[i][v[i - 1]] = min(dp[i][v[i - 1]], w[i - 1]);

    for (int j = 0; j <= MAX_VAL; ++j) {
      dp[i][j] = min({dp[i][j], dp[i - 1][j]});

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

  ll ans = 0;

  rep(i, MAX_VAL + 50) if (dp[N][i] <= W) ans = max(ans, i);
  cout << ans << endl;
}
Problem p03164 (C++)

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

#define ll long long int
#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define nl '\n'
#define repd(i, a, b) for (int i = a; i >= b; --i)
#define pb push_back
#define all(a) a.begin(), a.end()
#define F first
#define S second
const ll N = 1e6 + 9;
const ll mod = 1e9 + 7;

ll a[100][N];
vector<pair<ll, ll>> v;

void solve() {
  ll n, w, w1, v1;
  cin >> n >> w;
  v.pb({0, 0});

  rep(i, 1, n) {
    cin >> w1 >> v1;
    v.pb({v1, w1});
  }
  // sort(all(v));

  rep(i, 0, n) { rep(j, 1, 100000) a[i][j] = 1e9 + 7; }
  rep(i, 1, n) a[i][0] = 0;

  rep(i, 1, n) {
    rep(j, 1, 100000) {
      a[i][j] = a[i - 1][j];
      if (j - v[i].F >= 0)
        a[i][j] = min(a[i - 1][j], a[i - 1][j - v[i].F] + v[i].S);
      // cout<<j<<"<<"<<a[i][j]<<" ";}
    }
    // cout<<nl;
  }
  repd(i, 100000, 1) {
    if (a[n][i] <= w) {
      cout << i;
      break;
    }
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll t = 1;
  // pre();
  // cin>>t;
  // if(t^1)exit(0);
  while (t--)
    solve();
}

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

#define ll long long int
#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define nl '\n'
#define repd(i, a, b) for (int i = a; i >= b; --i)
#define pb push_back
#define all(a) a.begin(), a.end()
#define F first
#define S second
const ll N = 1e6 + 9;
const ll mod = 1e9 + 7;

ll a[105][N];
vector<pair<ll, ll>> v;

void solve() {
  ll n, w, w1, v1;
  cin >> n >> w;
  v.pb({0, 0});

  rep(i, 1, n) {
    cin >> w1 >> v1;
    v.pb({v1, w1});
  }
  // sort(all(v));

  rep(i, 0, n) { rep(j, 1, 100000) a[i][j] = 1e9 + 7; }
  rep(i, 1, n) a[i][0] = 0;

  rep(i, 1, n) {
    rep(j, 1, 100000) {
      a[i][j] = a[i - 1][j];
      if (j - v[i].F >= 0)
        a[i][j] = min(a[i - 1][j], a[i - 1][j - v[i].F] + v[i].S);
      // cout<<j<<"<<"<<a[i][j]<<" ";}
    }
    // cout<<nl;
  }
  repd(i, 100000, 1) {
    if (a[n][i] <= w) {
      cout << i;
      break;
    }
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll t = 1;
  // pre();
  // cin>>t;
  // if(t^1)exit(0);
  while (t--)
    solve();
}