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

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

void chmin(ll &a, ll b) {
  if (a > b) {
    a = b;
  }
  return;
}
const ll inf = 1LL << 60;
int main() {
  ll max_v = 0;
  for (ll i = 0; i < 110; i++) {
    for (ll j = 0; j < 100100; j++) {
      dp[i][j] = inf;
    }
  }

  ll N, W;
  cin >> N >> W;

  vector<ll> w(N);
  vector<ll> v(N);

  for (ll i = 0; i < N; i++) {
    cin >> w[i] >> v[i];
    max_v += v[i];
  }

  dp[0][v[0]] = w[0];
  dp[0][0] = 0;

  for (ll i = 0; i < N - 1; i++) {
    for (ll j = 0; j < 100100; j++) {

      chmin(dp[i + 1][j], dp[i][j]);
      if (j - v[j + 1] >= 0) {
        chmin(dp[i + 1][j], dp[i][j - v[i + 1]] + w[i + 1]);
      }
    }
  }
  ll res = 0;
  for (ll i = 0; i <= max_v; i++) {
    if (dp[N - 1][i] <= W) {
      res = i;
    }
  }

  cout << res << endl;
  return 0;
}


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

void chmin(ll &a, ll b) {
  if (a > b) {
    a = b;
  }
  return;
}
const ll inf = 1LL << 60;
int main() {
  ll max_v = 0;
  for (ll i = 0; i < 110; i++) {
    for (ll j = 0; j < 100100; j++) {
      dp[i][j] = inf;
    }
  }

  ll N, W;
  cin >> N >> W;

  vector<ll> w(N);
  vector<ll> v(N);

  for (ll i = 0; i < N; i++) {
    cin >> w[i] >> v[i];
    max_v += v[i];
  }

  dp[0][v[0]] = w[0];
  dp[0][0] = 0;

  for (ll i = 0; i < N - 1; i++) {
    for (ll j = 0; j < 100100; j++) {

      chmin(dp[i + 1][j], dp[i][j]);
      if (j - v[i + 1] >= 0) {
        chmin(dp[i + 1][j], dp[i][j - v[i + 1]] + w[i + 1]);
      }
    }
  }
  ll res = 0;
  for (ll i = 0; i <= max_v; i++) {
    if (dp[N - 1][i] <= W) {
      res = i;
    }
  }

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

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


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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long int
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n; i >= 0; i--)
#define REP(i, s, t) for (int i = s; i <= t; i++)
#define RREP(i, s, t) for (int i = s; i >= t; i--)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000
#define int long long
int dp[110][100020];
signed main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, W;
  cin >> N >> W;
  int w[110], v[110];
  rep(i, N) cin >> w[i] >> v[i];
  rep(i, 110) rep(j, 100010) dp[i][j] = INF;
  dp[0][0] = 0;
  rep(i, N) {
    RREP(j, 100000, 0) {
      dp[i + 1][j] = dp[i][j];
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j - v[i]] + w[i]);
    }
  }
  int ans = 0;
  rep(i, N + 1) {
    rep(j, 100010) {
      if (dp[i][j] <= W)
        ans = max(ans, j);
    }
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long int
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n; i >= 0; i--)
#define REP(i, s, t) for (int i = s; i <= t; i++)
#define RREP(i, s, t) for (int i = s; i >= t; i--)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000
#define int long long
int dp[110][100020];
signed main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, W;
  cin >> N >> W;
  int w[110], v[110];
  rep(i, N) cin >> w[i] >> v[i];
  rep(i, 110) rep(j, 100010) dp[i][j] = INF;
  dp[0][0] = 0;
  rep(i, N) {
    RREP(j, 100000, 0) {
      dp[i + 1][j] = dp[i][j];
      if (j - v[i] >= 0)
        dp[i + 1][j] = min(dp[i + 1][j], dp[i][j - v[i]] + w[i]);
    }
  }
  int ans = 0;
  rep(i, N + 1) {
    rep(j, 100010) {
      if (dp[i][j] <= W)
        ans = max(ans, j);
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, a, n) for (ll i = ((ll)a); i < ((ll)n); i++)
using namespace std;
typedef long long ll;

int main(void) {
  ll N, W;
  cin >> N >> W;
  vector<ll> w(N), v(N);
  REP(i, 0, N) cin >> w[i] >> v[i];

  const ll INF = 1LL << 60;
  vector<vector<ll>> dp(N + 1, vector<ll>(100001, INF));
  dp[0][0] = 0;

  REP(i, 0, N) {
    REP(j, 0, 100001) {
      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]);
    }
  }

  ll ans = 0;
  REP(i, 0, 100001) {
    if (dp[N][i] <= W) {
      ans = i;
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, a, n) for (ll i = ((ll)a); i < ((ll)n); i++)
using namespace std;
typedef long long ll;

int main(void) {
  ll N, W;
  cin >> N >> W;
  vector<ll> w(N), v(N);
  REP(i, 0, N) cin >> w[i] >> v[i];

  const ll INF = 1LL << 60;
  vector<vector<ll>> dp(N + 1, vector<ll>(100001, INF));
  dp[0][0] = 0;

  REP(i, 0, N) {
    REP(j, 0, 100001) {
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
      if (j + v[i] <= 100000) {
        dp[i + 1][j + v[i]] = min(dp[i + 1][j + v[i]], dp[i][j] + w[i]);
      }
    }
  }

  ll ans = 0;
  REP(i, 0, 100001) {
    if (dp[N][i] <= W) {
      ans = i;
    }
  }
  cout << ans << endl;
}
Problem p03164 (C++)

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

using ll = long long;
using namespace std;
const int INFint = 2e9 + 1;
const ll INFll = 2e18 + 1;
ll MOD = 1e9 + 7;

int main() {
  int N, W;
  cin >> N >> W;
  vector<int> wi(N + 1), vi(N + 1);
  for (int i(1); i <= N; i++) {
    cin >> wi[i] >> vi[i];
  }
  ll dp[101][10001] = {0};
  for (int i(0); i <= N; i++) {
    for (int v(1); v <= N * 1000; v++) {
      dp[i][v] = INFll;
    }
  }
  for (int i(1); i <= N; i++) {
    for (int v(0); v <= N * 1000; v++) {
      if (v >= vi[i]) {
        dp[i][v] = min(dp[i - 1][v], dp[i - 1][v - vi[i]] + wi[i]);
      } else {
        dp[i][v] = dp[i - 1][v];
      }
    }
  }
  for (int i(0); i < N; i++) {
    for (int v(0); v <= N * 10; v++) {
      // cout << dp[i][v] << " ";
    }
    // cout << endl;
  }
  ll ans(0);
  for (ll i(0); i <= N * 1000; i++) {
    if (dp[N][i] <= W)
      ans = i;
  }
  cout << ans << endl;
}

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

using ll = long long;
using namespace std;
const int INFint = 2e9 + 1;
const ll INFll = 2e18 + 1;
ll MOD = 1e9 + 7;

int main() {
  int N, W;
  cin >> N >> W;
  vector<int> wi(N + 1), vi(N + 1);
  for (int i(1); i <= N; i++) {
    cin >> wi[i] >> vi[i];
  }
  ll dp[101][100001] = {0};
  for (int i(0); i <= N; i++) {
    for (int v(1); v <= N * 1000; v++) {
      dp[i][v] = INFll;
    }
  }
  for (int i(1); i <= N; i++) {
    for (int v(0); v <= N * 1000; v++) {
      if (v >= vi[i]) {
        dp[i][v] = min(dp[i - 1][v], dp[i - 1][v - vi[i]] + wi[i]);
      } else {
        dp[i][v] = dp[i - 1][v];
      }
    }
  }
  for (int i(0); i < N; i++) {
    for (int v(0); v <= N * 10; v++) {
      // cout << dp[i][v] << " ";
    }
    // cout << endl;
  }
  ll ans(0);
  for (ll i(0); i <= N * 1000; i++) {
    if (dp[N][i] <= W)
      ans = i;
  }
  cout << ans << endl;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define var auto
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
const int MAX_ITEM = 100;
const int MAX_VALUE = 100000;
ll dp[MAX_ITEM][MAX_VALUE]; // 重さ

int main() {
  int n;
  ll w;
  cin >> n >> w;
  ll weight[n];
  ll value[n];
  rep(i, n) { cin >> weight[i] >> value[i]; }

  // 最小化問題なのでINF埋め
  rep(i, MAX_ITEM) { rep(j, MAX_VALUE) dp[i][j] = INF; }

  // 初期値00だけ0にする
  dp[0][0] = 0;

  // N個の品物の数ループ
  rep(i, n) {
    // 価値が 0->10^3*100 になるまで 1+v 回ループ
    rep(limit, 1 + MAX_VALUE) {
      // 選ばない場合
      chmin(dp[i + 1][limit], dp[i][limit]);

      // 選びたい
      if (limit - value[i] >= 0) {
        // 選んだ
        chmin(dp[i + 1][limit], weight[i] + dp[i][limit - value[i]]);
      }
    }
  }

  // 10^3*100 の1次配列中から dp[i][sum_v]<w を満たす sum_v を見つける
  ll res;
  rep(i, MAX_VALUE) {
    if (dp[n][i] <= w)
      res = i;
  }

  cout << res << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define var auto
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
const int MAX_ITEM = 110;
const int MAX_VALUE = 100100;
ll dp[MAX_ITEM][MAX_VALUE]; // 重さ

int main() {
  int n;
  ll w;
  cin >> n >> w;
  ll weight[n];
  ll value[n];
  rep(i, n) { cin >> weight[i] >> value[i]; }

  // 最小化問題なのでINF埋め
  rep(i, MAX_ITEM) { rep(j, MAX_VALUE) dp[i][j] = INF; }

  // 初期値00だけ0にする
  dp[0][0] = 0;

  // N個の品物の数ループ
  rep(i, n) {
    // 価値が 0->10^3*100 になるまで 1+v 回ループ
    rep(limit, 1 + MAX_VALUE) {
      // 選ばない場合
      chmin(dp[i + 1][limit], dp[i][limit]);

      // 選びたい
      if (limit - value[i] >= 0) {
        // 選んだ
        chmin(dp[i + 1][limit], weight[i] + dp[i][limit - value[i]]);
      }
    }
  }

  // 10^3*100 の1次配列中から dp[i][sum_v]<w を満たす sum_v を見つける
  ll res;
  rep(i, MAX_VALUE) {
    if (dp[n][i] <= w)
      res = i;
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>
#define ll long long int
#define lld long double
#define F first
#define S second
#define f(i, a, b) for (int i = a; i <= b; i++)
#define g(i, a, b) for (int i = a; i >= b; i--)
#define mp make_pair
#define pb push_back
#define mh make_heap
#define ph push_heap
#define pq priority_queue
#define bits(x) __builtin_popcountll(x)
#define op(x) cout << "Case #" << x << ": "
using namespace std;
ll mod = 1000000007;
ll INF = 1000000000000000000;
ll n, k;
ll a[101];
ll v[101];
ll w[101];
ll dp[101][100001];
ll lim;

ll foo(ll idx, ll val) {

  if (idx == n) {
    if (val >= lim)
      return 0;
    return INF;
  }

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

  ll res = 0;
  ll v1 = w[idx] + foo(idx + 1, val + v[idx]);
  ll v2 = foo(idx + 1, val);
  res = min(v1, v2);
  return dp[idx][val] = res;
}

void solve(int t) {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> w[i] >> v[i];
  }
  ll s = 0;
  for (int i = 0; i < n; i++)
    s += v[i];

  ll lo = 0;
  ll hi = s;
  ll ans;
  ll mid;

  // lim = 140;
  // cout << foo(0, 0) << endl;
  while (lo <= hi) {
    mid = (lo + hi) / 2;
    lim = mid;

    memset(dp, -1, sizeof(dp));
    if (foo(0, 0) <= k) {
      ans = mid;
      lo = mid + 1;
    } else {
      hi = mid - 1;
    }
  }

  cout << ans << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

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

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


Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
#define ll long long int
#define lld long double
#define F first
#define S second
#define f(i, a, b) for (int i = a; i <= b; i++)
#define g(i, a, b) for (int i = a; i >= b; i--)
#define mp make_pair
#define pb push_back
#define mh make_heap
#define ph push_heap
#define pq priority_queue
#define bits(x) __builtin_popcountll(x)
#define op(x) cout << "Case #" << x << ": "
using namespace std;
ll mod = 1000000007;
ll INF = 1000000000000000000;
ll n, k;
ll a[101];
ll v[101];
ll w[101];
ll dp[101][100001];
ll lim;

ll foo(ll idx, ll val) {

  if (idx == n) {
    if (val >= lim)
      return 0;
    return INF;
  }

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

  ll res = 0;
  ll v1 = w[idx] + foo(idx + 1, val + v[idx]);
  ll v2 = foo(idx + 1, val);
  res = min(v1, v2);
  return dp[idx][val] = res;
}

void solve(int t) {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> w[i] >> v[i];
  }
  ll s = 0;
  for (int i = 0; i < n; i++)
    s += v[i];

  ll lo = 0;
  ll hi = s;
  ll ans;
  ll mid;

  // lim = 140;
  // cout << foo(0, 0) << endl;
  while (lo <= hi) {
    mid = (lo + hi) / 2;
    lim = mid;

    memset(dp, -1, sizeof(dp));
    if (foo(0, 0) <= k) {
      ans = mid;
      lo = mid + 1;
    } else {
      hi = mid - 1;
    }
  }

  cout << ans << endl;
}

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

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

  for (int i = 1; i <= t; i++) {
    solve(i);
  }
}
Problem p03164 (Python)

Buggy submission (status: Runtime Error):
n, w = map(int, input().split())
item = [list(map(int, input().split())) for i in range(n)]

max_v = 100005

dp = [[float("inf") * max_v] for j in range(n + 1)]
dp[0][0] = 0

for i in range(n):
    for sum_v in range(max_v):
        if sum_v - item[i][1] >= 0:
            dp[i + 1][sum_v] = min(
                dp[i + 1][sum_v], dp[i][sum_v - item[i][1]] + item[i][0]
            )
        dp[i + 1][sum_v] = min((dp[i + 1][sum_v], dp[i][sum_v]))

for sum_v in reversed(range(max_v)):
    if dp[-1][sum_v] <= w:
        print(sum_v)
        break


Fixed submission:
n, w = map(int, input().split())
item = [list(map(int, input().split())) for i in range(n)]

max_v = 100005

dp = [[float("inf")] * max_v for j in range(n + 1)]
dp[0][0] = 0

for i in range(n):
    for sum_v in range(max_v):
        if sum_v - item[i][1] >= 0:
            dp[i + 1][sum_v] = min(
                dp[i + 1][sum_v], dp[i][sum_v - item[i][1]] + item[i][0]
            )
        dp[i + 1][sum_v] = min((dp[i + 1][sum_v], dp[i][sum_v]))

for sum_v in reversed(range(max_v)):
    if dp[-1][sum_v] <= w:
        print(sum_v)
        break
Problem p03164 (Python)

Buggy submission (status: Runtime Error):
N, W = map(int, input().split())

items = [list(map(int, input().split())) for _ in range(N)]
v_sum = sum([item[1] for item in items])

inf = float("inf")
dp = [inf for _ in range(v_sum + 1)]

dp[0] = 0
for i in range(N):
    for j in range(v_sum, -1, -1):
        if dp[j] >= dp[j - items[i][1] + items[i][0]]:
            dp[j] = dp[j - items[i][1]] + items[i][0]

for i in range(v_sum, -1, -1):
    if dp[i] <= W:
        print(i)
        exit()


Fixed submission:
N, W = map(int, input().split())

items = [list(map(int, input().split())) for _ in range(N)]
v_sum = sum([item[1] for item in items])

inf = float("inf")
dp = [inf for _ in range(v_sum + 1)]

dp[0] = 0
for i in range(N):
    for j in range(v_sum, -1, -1):
        if dp[j] > dp[j - items[i][1]] + items[i][0]:
            dp[j] = dp[j - items[i][1]] + items[i][0]

for i in range(v_sum, -1, -1):
    if dp[i] <= W:
        print(i)
        exit()
Problem p03164 (Python)

Buggy submission (status: Time Limit Exceeded):
N, W = map(int, input().split())
items = [None] * N
for n in range(N):
    w, v = map(int, input().split())
    items[n] = (w, v)
V = 10**6 + 1

dp = [[10**9 + 1] * V for _ in range(N + 1)]
dp[0][0] = 0

for i in range(N):
    for j in range(V):
        w, v = items[i]
        if j >= v:
            dp[i + 1][j] = min(dp[i][j - v] + w, dp[i][j])
        else:
            dp[i + 1][j] = dp[i][j]

ans = 0
for v in range(V):
    if dp[N][v] <= W:
        ans = v
print(ans)


Fixed submission:
N, W = map(int, input().split())
items = [None] * N
for n in range(N):
    w, v = map(int, input().split())
    items[n] = (w, v)
V = 10**5 + 1

dp = [[10**9 + 1] * V for _ in range(N + 1)]
dp[0][0] = 0

for i in range(N):
    for j in range(V):
        w, v = items[i]
        if j >= v:
            dp[i + 1][j] = min(dp[i][j - v] + w, dp[i][j])
        else:
            dp[i + 1][j] = dp[i][j]

ans = 0
for v in range(V):
    if dp[N][v] <= W:
        ans = v
print(ans)
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(v) v.begin(), v.end()
using namespace std;
using ll = long long;
typedef pair<int, int> P;

const int INF = 1001001001;
const long double PI = (acos(-1));
const int mod = 1e9 + 7;
const int vx[4] = {0, 1, 0, -1};
const int vy[4] = {1, 0, -1, 0};

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

  ll N, W;
  cin >> N >> W;
  vector<ll> v(N);
  vector<ll> w(N);
  ll sum = 0;

  rep(i, N) {
    cin >> w[i] >> v[i];
    sum += v[i];
  }
  vector<vector<ll>> dp(N + 1, vector<ll>(sum + 1, INF));
  dp[0][0] = 0;
  rep(i, N) {
    rep(j, sum + 1) {
      if (dp[i][j - v[i]] >= 0)
        dp[i + 1][j] = min(dp[i][j - v[i]] + w[i], dp[i][j]);
      else
        dp[i + 1][j] = dp[i][j];
    }
  }
  ll ans = 0;
  rep(j, sum + 1) {
    if (dp[N][j] <= W)
      ans = max(ans, j);
  }

  cout << ans << endl;

  cout << endl;
  return 0;
}


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

#define rep(i, n) for (ll i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
using namespace std;
using ll = long long;
typedef pair<int, int> P;

const int INF = 1001001001;
const long double PI = (acos(-1));
const int mod = 1e9 + 7;
const int vx[4] = {0, 1, 0, -1};
const int vy[4] = {1, 0, -1, 0};

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

  ll N, W;
  cin >> N >> W;
  vector<ll> v(N);
  vector<ll> w(N);
  ll sum = 0;

  rep(i, N) {
    cin >> w[i] >> v[i];
    sum += v[i];
  }
  vector<vector<ll>> dp(N + 1, vector<ll>(sum + 1, INF));
  dp[0][0] = 0;
  rep(i, N) {
    rep(j, sum + 1) {
      if (j - v[i] >= 0)
        dp[i + 1][j] = min(dp[i][j - v[i]] + w[i], dp[i][j]);
      else
        dp[i + 1][j] = dp[i][j];
    }
  }
  ll ans = 0;
  rep(j, sum + 1) {
    if (dp[N][j] <= W)
      ans = max(ans, j);
  }

  cout << ans << endl;

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

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

#define int long long int
#define endl "\n"

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

  vector<int> wt(n), val(n);

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

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

  for (int i = 0; i < n; i++) {
    for (int j = sum_val; j >= 0; j--) {
      if (wt[i] + dp[j - val[i]] <= w)
        dp[j] = min(dp[j], wt[i] + dp[j - val[i]]);
    }
  }

  int ans = -1;

  for (int i = 1; i <= sum_val; i++) {
    if (dp[i] > 0 && dp[i] <= w)
      ans = max(ans, i);
  }
  cout << ans << endl;
}

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

#define int long long int
#define endl "\n"

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

  vector<int> wt(n), val(n);

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

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

  for (int i = 0; i < n; i++) {
    for (int j = sum_val; j >= val[i]; j--) {
      if (wt[i] + dp[j - val[i]] <= w)
        dp[j] = min(dp[j], wt[i] + dp[j - val[i]]);
    }
  }

  int ans = -1;

  for (int i = 1; i <= sum_val; i++) {
    if (dp[i] > 0 && dp[i] <= w)
      ans = max(ans, i);
  }
  cout << ans << endl;
}
Problem p03164 (C++)

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

using namespace std;

typedef long long ll;
const int INF = 1e9 + 1;

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

  vector<ll> value(N), weight(N);
  for (int i = 0; i < N; i++) {
    cin >> weight[i] >> value[i];
  }

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

  for (int n = 0; n < N; n++) {
    for (ll v = 0; v <= N * 1010; v++) {
      if (v >= value[n]) {
        dp[n + 1][v] = min(dp[n][v - value[n]] + weight[n], dp[n][v]);
      } else {
        dp[n + 1][v] = dp[n][v];
      }
    }
  }

  ll best_score = 0;

  for (ll j = 0; j <= N * 1010; j++) {
    if (dp[N][j] <= W) {
      // cout<<dp[N][j]<<" ";
      best_score = max(best_score, j);
    }
  }

  cout << best_score << endl;

  return 0;
}

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

using namespace std;

typedef long long ll;
const int INF = 1e9 + 1;

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

  vector<ll> value(N), weight(N);
  for (int i = 0; i < N; i++) {
    cin >> weight[i] >> value[i];
  }

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

  for (int n = 0; n < N; n++) {
    for (ll v = 0; v <= N * 1010; v++) {
      if (v >= value[n]) {
        dp[n + 1][v] = min(dp[n][v - value[n]] + weight[n], dp[n][v]);
      } else {
        dp[n + 1][v] = dp[n][v];
      }
    }
  }

  ll best_score = 0;

  for (ll j = 0; j <= N * 1010; j++) {
    if (dp[N][j] <= W) {
      // cout<<dp[N][j]<<" ";
      best_score = max(best_score, j);
    }
  }

  cout << best_score << endl;

  return 0;
}
Problem p03164 (C++)

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

int main() {
  ll n, limit;
  cin >> n >> limit;
  vector<pair<ll, ll>> luggage(n);
  ll quota_max = 0;
  for (int i = 0; i < n; ++i) {
    cin >> luggage[i].first >> luggage[i].second;
    quota_max += luggage[i].second;
  }
  vector<vector<ll>> dp(110, vector<ll>(100100, 1001001001001001));
  dp[0][0] = 0;
  for (int i = 0; i <= n; ++i) {
    ll new_w = luggage[i].first;
    ll new_v = luggage[i].second;
    for (int j = 0; j <= quota_max; ++j) {
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
      dp[i + 1][j + new_v] = min(dp[i + 1][j + new_v], dp[i][j] + new_w);
    }
  }
  ll ans = 0;
  for (ll k = 0; k <= quota_max; ++k)
    if (dp[n][k] <= limit)
      ans = max(ans, k);
  cout << ans << endl;
}


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

int main() {
  ll n, limit;
  cin >> n >> limit;
  vector<pair<ll, ll>> luggage(n);
  ll quota_max = 0;
  for (int i = 0; i < n; ++i) {
    cin >> luggage[i].first >> luggage[i].second;
    quota_max += luggage[i].second;
  }
  vector<vector<ll>> dp(110, vector<ll>(300300, 1001001001001001));
  dp[0][0] = 0;
  for (int i = 0; i <= n; ++i) {
    ll new_w = luggage[i].first;
    ll new_v = luggage[i].second;
    for (int j = 0; j <= quota_max; ++j) {
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
      dp[i + 1][j + new_v] = min(dp[i + 1][j + new_v], dp[i][j] + new_w);
    }
  }
  ll ans = 0;
  for (ll k = 0; k <= quota_max; ++k)
    if (dp[n][k] <= limit)
      ans = max(ans, k);
  cout << ans << endl;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
// https://atcoder.jp/contests/dp/tasks/dp_e
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <limits.h>

using namespace std;

struct object {
  int weight, value;
};

object arr[107];
long long dp[1007][107];

long long res(int value, int n) {
  if (value <= 0)
    return 0;
  if (n == 0)
    return INT_MAX;
  if (dp[value][n])
    return dp[value][n];
  dp[value][n] = min(res(value - arr[n - 1].value, n - 1) + arr[n - 1].weight,
                     res(value, n - 1));
  return dp[value][n];
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, w, sum = 0;
  cin >> n >> w;
  for (int i = 0; i < n; i++) {
    cin >> arr[i].weight >> arr[i].value;
    sum += arr[i].value;
  }
  for (int i = sum; i >= 0; i--) {
    if (res(i, n) <= w) {
      cout << i << endl;
      break;
    }
  }
  return 0;
}

Fixed submission:
// https://atcoder.jp/contests/dp/tasks/dp_e
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <limits.h>

using namespace std;

struct object {
  int weight, value;
};

object arr[107];
long long dp[100007][107];

long long res(int value, int n) {
  if (value <= 0)
    return 0;
  if (n == 0)
    return INT_MAX;
  if (dp[value][n])
    return dp[value][n];
  dp[value][n] = min(res(value - arr[n - 1].value, n - 1) + arr[n - 1].weight,
                     res(value, n - 1));
  return dp[value][n];
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, w, sum = 0;
  cin >> n >> w;
  for (int i = 0; i < n; i++) {
    cin >> arr[i].weight >> arr[i].value;
    sum += arr[i].value;
  }
  for (int i = sum; i >= 0; i--) {
    if (res(i, n) <= w) {
      cout << i << endl;
      break;
    }
  }
  return 0;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
long long inf = 1e18L + 5;
long long dp[100001];
int main() {
  int n, w;
  cin >> n >> w;
  pair<long long, long long> arr[n + 1];
  arr[0] = make_pair(0, 0);
  int sum = 0;
  for (int i = 1; i <= n; ++i) {
    long long a, b;
    cin >> a >> b;
    sum += b;
    arr[i] = make_pair(a, b);
  }
  for (int i = 0; i <= sum; ++i) {
    dp[i] = inf;
  }
  dp[0] = 0;

  for (int i = 1; i <= n; ++i) {
    for (int j = sum; j >= 0; --j) {
      dp[j] = min(dp[j], dp[j - arr[i].second] + arr[i].first);
    }
  }
  for (int i = sum; i >= 0; --i) {
    if (dp[i] <= w) {
      cout << i << endl;
      break;
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
long long inf = 1e18L + 5;
long long dp[100001];
int main() {
  int n, w;
  cin >> n >> w;
  pair<long long, long long> arr[n + 1];
  arr[0] = make_pair(0, 0);
  int sum = 0;
  for (int i = 1; i <= n; ++i) {
    long long a, b;
    cin >> a >> b;
    sum += b;
    arr[i] = make_pair(a, b);
  }
  for (int i = 0; i <= sum; ++i) {
    dp[i] = inf;
  }
  dp[0] = 0;

  for (int i = 1; i <= n; ++i) {
    for (int j = sum; j >= 0; --j) {
      if (j - arr[i].second >= 0) {
        dp[j] = min(dp[j], dp[j - arr[i].second] + arr[i].first);
      } else {
        break;
      }
    }
  }
  for (int i = sum; i >= 0; --i) {
    if (dp[i] <= w) {
      cout << i << endl;
      break;
    }
  }
  return 0;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF = 1e9 + 5;
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n, W;
  cin >> n >> W;
  vector<int> value(n);
  vector<int> weight(n);
  for (int i = 0; i < n; i++)
    cin >> weight[i] >> value[i];
  int sum_value = 0;
  for (int i = 0; i < n; i++)
    sum_value += value[i];
  vector<int> dp(sum_value + 1, INF);
  dp[0] = 0;
  // dp[i] = the min total weight of items with total value exactly equal to i
  for (int item = 0; item < n; item++) {
    for (int value_already = sum_value - value[item]; value_already >= 0;
         value_already--) {
      dp[value_already + value[item]] = min(dp[value_already + value[item]],
                                            dp[value_already] + weight[item]);
    }
  }
  int ans = 0;
  for (int i = 0; i <= sum_value; i++) {
    if (dp[i] <= W) {
      ans = max(ans, i);
    }
  }
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF = 1e9 + 5;
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  /*#ifndef ONLINE_JUDGE
  freopen("input.txt","r",stdin);
  freopen("output.txt","w",stdout);
  #endif*/
  int n, W;
  cin >> n >> W;
  vector<int> value(n);
  vector<int> weight(n);
  for (int i = 0; i < n; i++)
    cin >> weight[i] >> value[i];
  int sum_value = 0;
  for (int i = 0; i < n; i++)
    sum_value += value[i];
  vector<int> dp(sum_value + 1, INF);
  dp[0] = 0;
  // dp[i] = the min total weight of items with total value exactly equal to i
  for (int item = 0; item < n; item++) {
    for (int value_already = sum_value - value[item]; value_already >= 0;
         value_already--) {
      dp[value_already + value[item]] = min(dp[value_already + value[item]],
                                            dp[value_already] + weight[item]);
    }
  }
  int ans = 0;
  for (int i = 0; i <= sum_value; i++) {
    if (dp[i] <= W) {
      ans = max(ans, i);
    }
  }
  cout << ans;
}
Problem p03164 (C++)

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

using namespace std;

using ll = long long;

const int N = 105, W = 1e9 + 5, V = 1e3 + 5, INF = 1e9 + 5;

ll dp[N][N * V];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie();
  int n, w;
  cin >> n >> w;
  fill(dp[0], dp[0] + N * V, INF);
  dp[0][0] = 0;
  for (int i = 1; i <= n; i++) {
    int wi, vi;
    cin >> wi >> vi;
    for (int j = 0; j < wi; j++)
      dp[i][j] = dp[i - 1][j];
    for (int j = vi; j < N * V; j++)
      dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - vi] + wi);
  }
  for (int i = N * V - 1; i >= 0; i--)
    if (dp[n][i] <= w) {
      cout << i << "\n";
      return 0;
    }
}


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

using namespace std;

using ll = long long;

const int N = 105, W = 1e9 + 5, V = 1e3 + 5, INF = 1e9 + 5;

ll dp[N][N * V];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie();
  int n, w;
  cin >> n >> w;
  fill(dp[0], dp[0] + N * V, INF);
  dp[0][0] = 0;
  for (int i = 1; i <= n; i++) {
    int wi, vi;
    cin >> wi >> vi;
    for (int j = 0; j < vi; j++)
      dp[i][j] = dp[i - 1][j];
    for (int j = vi; j < N * V; j++)
      dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - vi] + wi);
  }
  for (int i = N * V - 1; i >= 0; i--)
    if (dp[n][i] <= w) {
      cout << i << "\n";
      return 0;
    }
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):

#include <bits/stdc++.h>
using namespace std;
#define int long long int
int v[100], w[100];
int dp[100][100005];
int f(int i, int value) {
  if (value == 0)
    return 0;
  if (i < 0)
    return 1000000000;
  if (dp[i][value] != -1)
    return dp[i][value];
  return dp[i][value] = min(f(i - 1, value), f(i - 1, value - v[i]) + w[i]);
}

signed main() {
  int N, W;
  cin >> N >> W;
  int sum = 0;
  for (int i = 0; i < N; i++) {
    cin >> w[i] >> v[i];
    sum += v[i];
  }
  for (int i = 0; i < N; i++)
    for (int j = 0; j <= sum; j++)
      dp[i][j] = -1;
  int ans = 0;
  for (int val = 1; val <= sum; val++) {
    if (f(N - 1, val) <= W) {
      ans = val;
    }
  }
  cout << ans;

  return 0;
}


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long int
int v[100], w[100];
int dp[100][100005];
int f(int i, int value) {
  if (value < 0)
    return 1000000000;
  if (value == 0)
    return 0;
  if (i < 0)
    return 1000000000;
  if (dp[i][value] != -1)
    return dp[i][value];
  return dp[i][value] = min(f(i - 1, value), f(i - 1, value - v[i]) + w[i]);
}

signed main() {
  int N, W;
  cin >> N >> W;
  int sum = 0;
  for (int i = 0; i < N; i++) {
    cin >> w[i] >> v[i];
    sum += v[i];
  }
  for (int i = 0; i < N; i++)
    for (int j = 0; j <= sum; j++)
      dp[i][j] = -1;
  int ans = 0;
  for (int val = 1; val <= sum; val++) {
    if (f(N - 1, val) <= W) {
      ans = val;
    }
  }
  cout << ans;

  return 0;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <climits>
#include <iostream>
#include <vector>
using namespace std;
#define ll long long
int main() {
  ll N = 0;
  cin >> N;
  ll VAL = 0;
  cin >> VAL;
  ll V = 0;
  vector<vector<ll>> inp(N, vector<ll>(2));
  for (ll i = 0; i < N; i++) {
    cin >> inp[i][0] >> inp[i][1];
    V += inp[i][1];
  }
  vector<ll> dp(V + 1, INT_MAX);
  dp[0] = 0;
  for (ll i = 0; i < N; i++) {
    for (ll j = V - inp[i][1]; j >= 0; j--) {
      dp[j + inp[i][1]] = min(dp[j + inp[i][1]], dp[j] + inp[i][0]);
    }
  }
  // for(ll i=0;i<N;i++){for(ll j=0;j<=VAL;j++)cout<<dp[i][j]<<" ";cout<<endl;}
  for (ll i = VAL; i >= 0; i--) {
    if (dp[i] != INT_MAX) {
      cout << dp[i] << endl;
      break;
    }
  }
  return 0;
}

Fixed submission:
#include <climits>
#include <iostream>
#include <vector>
using namespace std;
#define ll long long
int main() {
  ll N = 0;
  cin >> N;
  ll VAL = 0;
  cin >> VAL;
  ll V = 0;
  vector<vector<ll>> inp(N, vector<ll>(2));
  for (ll i = 0; i < N; i++) {
    cin >> inp[i][0] >> inp[i][1];
    V += inp[i][1];
  }
  vector<ll> dp(V + 1, INT_MAX);
  dp[0] = 0;
  for (ll i = 0; i < N; i++) {
    for (ll j = V - inp[i][1]; j >= 0; j--) {
      dp[j + inp[i][1]] = min(dp[j + inp[i][1]], dp[j] + inp[i][0]);
    }
  }
  // for(ll i=0;i<N;i++){for(ll j=0;j<=VAL;j++)cout<<dp[i][j]<<" ";cout<<endl;}
  // for(int i=0;i<V;i++) cout<<dp[i]<<" ";
  // cout<<endl;
  for (ll i = V; i >= 0; i--) {
    if (dp[i] <= VAL) {
      cout << i << endl;
      break;
    }
  }
  return 0;
}
Problem p03164 (C++)

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

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

  vector<vector<ll>> dp(N + 1, vector<ll>(N * 1000 + 10, 1e18));
  dp[0][0] = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= N * 1000; j++) {
      if (dp[i][j - v[i]] != 1e18 && j >= v[i]) {
        dp[i + 1][j] = min(dp[i][j], dp[i][j - v[i]] + w[i]);
      } else {
        dp[i + 1][j] = dp[i][j];
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i <= N * 1000; i++) {
    if (dp[N][i] <= W) {
      ans = i;
    }
  }
  cout << ans << endl;

  return 0;
}


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

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

  vector<vector<ll>> dp(N + 1, vector<ll>(N * 1000 + 10, 1e18));
  dp[0][0] = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= N * 1000; j++) {
      if (j >= v[i] && dp[i][j - v[i]] != 1e18) {
        dp[i + 1][j] = min(dp[i][j], dp[i][j - v[i]] + w[i]);
      } else {
        dp[i + 1][j] = dp[i][j];
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i <= N * 1000; i++) {
    if (dp[N][i] <= W) {
      ans = i;
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p03164 (C++)

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

int n, w;
int vi[105];
long long wi[105];
long long dp[100][100005];

void solve() {
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= 100000; j++)
      dp[i][j] = 1e15;
  dp[0][0] = 0;
  for (int i = 1; i <= n; i++) {
    dp[i][0] = 0;
    for (int j = 1; j <= 100000; j++) {
      dp[i][j] = min(dp[i][j], dp[i - 1][j]);
      if (j >= vi[i])
        dp[i][j] = min(dp[i][j], dp[i - 1][j - vi[i]] + wi[i]);
    }
  }
  for (int i = 0; i <= 100000; i++) {
    if (dp[n][100000 - i] <= w) {
      cout << 100000 - i << '\n';
      return;
    }
  }
}
int main() {
  cin >> n >> w;
  for (int i = 1; i <= n; i++)
    cin >> wi[i] >> vi[i];
  solve();
  return 0;
}


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

int n, w;
int vi[105];
long long wi[105];
long long dp[105][100005];

void solve() {
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= 100000; j++)
      dp[i][j] = 1e15;
  dp[0][0] = 0;
  for (int i = 1; i <= n; i++) {
    dp[i][0] = 0;
    for (int j = 1; j <= 100000; j++) {
      dp[i][j] = min(dp[i][j], dp[i - 1][j]);
      if (j >= vi[i])
        dp[i][j] = min(dp[i][j], dp[i - 1][j - vi[i]] + wi[i]);
    }
  }
  for (int i = 0; i <= 100000; i++) {
    if (dp[n][100000 - i] <= w) {
      cout << 100000 - i << '\n';
      return;
    }
  }
}
int main() {
  cin >> n >> w;
  for (int i = 1; i <= n; i++)
    cin >> wi[i] >> vi[i];
  solve();
  return 0;
}
Problem p03164 (C++)

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

const long long int INF = 1e18L + 5;
int main() {
  int n, W;
  cin >> n >> W;
  vector<vector<int>> v(n + 1, vector<int>(2));
  int sum = 0;
  for (int i = 1; i <= n; i++) {
    cin >> v[i][0] >> v[i][1];
    sum += v[i][1];
  }

  vector<long long int> dp(sum + 1, INF);
  for (int item = 0; item <= n; item++) {
    dp[0] = 0;
    for (int val = sum; val >= 0; --val) {
      dp[val] = min(dp[val], dp[val - v[item][1]] + v[item][0]);
    }
  }
  long long int ans = 0;
  for (int i = 0; i < sum + 1; i++) {
    if (dp[i] <= W)
      ans = max(ans, (long long int)i);
  }
  cout << ans;
  return 0;
}

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

const long long int INF = 1e18L + 5;
int main() {
  int n, W;
  cin >> n >> W;
  vector<vector<int>> v(n + 1, vector<int>(2));
  int sum = 0;
  for (int i = 1; i <= n; i++) {
    cin >> v[i][0] >> v[i][1];
    sum += v[i][1];
  }

  vector<long long int> dp(sum + 1, INF);
  for (int item = 0; item <= n; item++) {
    dp[0] = 0;
    for (int val = sum; val >= 0; --val) {
      if (val >= v[item][1]) {
        dp[val] = min(dp[val], dp[val - v[item][1]] + v[item][0]);
      }
    }
  }
  long long int ans = 0;
  for (int i = 0; i < sum + 1; i++) {
    if (dp[i] <= W)
      ans = max(ans, (long long int)i);
  }
  cout << ans;
  return 0;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define int long long
#define All(v) (v).begin(), (v).end()
int dy[4] = {-1, 0, 1, 0};
int dx[4] = {0, 1, 0, -1};
int Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int mod = 1000000007;
const int inf = mod * mod;
const int d5 = 100100;
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, W;
  cin >> N >> W;
  int dp[d5];
  // dp[i]:価値iを達成するのに必要な最小の重さ
  for (int i = 0; i < d5; i++)
    dp[i] = inf;
  dp[0] = 0;
  for (int i = 0; i < N; i++) {
    int w, v;
    cin >> w >> v;
    for (int j = d5; j >= 0; j--) {
      if (dp[j] == inf)
        continue;
      dp[j + v] = min(dp[j + v], dp[j] + w);
    }
  }
  int res = 0;
  for (int i = 0; i < d5; i++) {
    if (dp[i] <= W)
      res = max(res, i);
  }
  cout << res << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define int long long
#define All(v) (v).begin(), (v).end()
int dy[4] = {-1, 0, 1, 0};
int dx[4] = {0, 1, 0, -1};
int Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int mod = 1000000007;
const int inf = mod * mod;
const int d5 = 100100;
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, W;
  cin >> N >> W;
  int dp[d5];
  // dp[i]:価値iを達成するのに必要な最小の重さ
  for (int i = 0; i < d5; i++)
    dp[i] = inf;
  dp[0] = 0;
  for (int i = 0; i < N; i++) {
    int w, v;
    cin >> w >> v;
    for (int j = d5; j >= 0; j--) {
      if (dp[j] == inf)
        continue;
      if (j + v >= d5)
        continue;
      dp[j + v] = min(dp[j + v], dp[j] + w);
    }
  }
  int res = 0;
  for (int i = 0; i < d5; i++) {
    if (dp[i] <= W)
      res = max(res, i);
  }
  cout << res << endl;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rep2(i, a, b) for (ll i = a; i < b; ++i)
const ll MOD = 1000003;
const ll INF = 1e9;
const ll IINF = 1e18;
const double EPS = 1e-8;
const double pi = acos(-1);

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

const ll MAX = 100010;

int main() {
  ll N, W;
  cin >> N >> W;
  vector<ll> w(N), v(N);
  rep(i, N) cin >> w[i] >> v[i];
  vector<vector<ll>> dp(N + 1, vector<ll>(MAX, IINF));
  dp[0][0] = 0;
  rep(i, N) {
    rep(j, MAX) {
      chmin(dp[i + 1][j], dp[i][j]);
      chmin(dp[i + 1][j], dp[i][j - v[i]] + w[i]);
    }
  }
  ll ans = 0;
  rep(i, MAX) {
    if (dp[N][i] <= W)
      ans = i;
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rep2(i, a, b) for (ll i = a; i < b; ++i)
const ll MOD = 1000003;
const ll INF = 1e9;
const ll IINF = 1e18;
const double EPS = 1e-8;
const double pi = acos(-1);

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

const ll MAX = 100010;

int main() {
  ll N, W;
  cin >> N >> W;
  vector<ll> w(N), v(N);
  rep(i, N) cin >> w[i] >> v[i];
  vector<vector<ll>> dp(N + 1, vector<ll>(MAX, IINF));
  dp[0][0] = 0;
  rep(i, N) {
    rep(j, MAX) {
      chmin(dp[i + 1][j], dp[i][j]);
      if (j - v[i] >= 0)
        chmin(dp[i + 1][j], dp[i][j - v[i]] + w[i]);
    }
  }
  ll ans = 0;
  rep(i, MAX) {
    if (dp[N][i] <= W)
      ans = i;
  }
  cout << ans << endl;
}
Problem p03164 (C++)

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

using namespace std;

#define INF 100000000000

int N;
long long W, w[101], v[101];
long long dp[101][1000000];

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

  for (int i = 0; i < N; i++) {
    for (long long j = 0; j < 1000000; j++) {
      dp[i][j] = INF;
    }
  }
  dp[0][0] = 0;

  for (int i = 0; i <= N; i++) {
    for (long long j = 0; j < 1000000; 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 long ans = INF;
  for (long long i = 1; i < 1000000; i++)
    if (W >= dp[N][i]) {
      ans = i;
    }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;

#define INF 100000000000

int N;
long long W, w[102], v[102];
long long dp[102][1000000];

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

  for (int i = 0; i < N; i++) {
    for (long long j = 0; j < 1000000; j++) {
      dp[i][j] = INF;
    }
  }
  dp[0][0] = 0;

  for (int i = 0; i <= N; i++) {
    for (long long j = 0; j < 1000000; 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 long ans = INF;
  for (long long i = 1; i < 1000000; i++)
    if (W >= dp[N][i]) {
      ans = i;
    }
  cout << ans << endl;

  return 0;
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
/*
@author: sharrad99
*/
#include <bits/stdc++.h>
using namespace std;
const long long oo = 1000000000000000;
int main() {

#ifndef ONLINE_JUDGE
  freopen("C:\\Users\\sharr\\Documents\\Input.txt", "r", stdin);
  freopen("C:\\Users\\sharr\\Documents\\Output.txt", "w", stdout);
#endif

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

  long long n, W;
  cin >> n >> W;

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

  long long mx = *max_element(v.begin(), v.end());
  mx *= n;
  vector<long long> dp(mx + 1, oo);
  dp[0] = 0;
  for (long long i = 0; i < n; i++) {
    for (long long j = mx; j - v[i] >= 0; j--) {
      dp[j] = min(dp[j], dp[j - v[i]] + w[i]);
    }
  }

  long long res = oo;
  for (long long j = 0; j <= mx; j++) {
    if (dp[j] <= W)
      res = j;
  }

  cout << res << '\n';
}

Fixed submission:
/*
@author: sharrad99
*/
#include <bits/stdc++.h>
using namespace std;
const long long oo = 1000000000000000;
int main() {

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

  long long n, W;
  cin >> n >> W;

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

  long long mx = *max_element(v.begin(), v.end());
  mx *= n;
  vector<long long> dp(mx + 1, oo);
  dp[0] = 0;
  for (long long i = 0; i < n; i++) {
    for (long long j = mx; j - v[i] >= 0; j--) {
      dp[j] = min(dp[j], dp[j - v[i]] + w[i]);
    }
  }

  long long res = oo;
  for (long long j = 0; j <= mx; j++) {
    if (dp[j] <= W)
      res = j;
  }

  cout << res << '\n';
}
Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <vector>
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define REP(i, a, b) for (int i = a; i > b; i--)
#define vint vector<int>
#define vvint vector<vint>
#define ct(a) cout << a << endl
#define tget(a, b) get<a>(b)
using namespace std;
typedef long long ll;
const ll INF = 1e17;
const int MOD = 1e9 + 7;
ll lim;
ll N, W[100], V[100];
ll dp[101][100001];
int main(void) {
  cin >> N >> lim;
  FOR(i, 0, N) cin >> W[i] >> V[i];
  FOR(i, 0, 102) FOR(k, 0, 100002) dp[i][k] = INF;
  dp[0][0] = 0;
  FOR(i, 0, N) {
    for (ll k = 0; k <= 100000; k++) {
      if (k < V[i]) {
        dp[i + 1][k] = dp[i][k];
      } else {
        dp[i + 1][k] = min(dp[i][k], dp[i][k - V[i]] + W[i]);
      }
    }
  }
  ll ans = 0;
  FOR(i, 0, 100001) if (dp[N][i] <= lim) ans = i;
  ct(ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <vector>
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define REP(i, a, b) for (int i = a; i > b; i--)
#define vint vector<int>
#define vvint vector<vint>
#define ct(a) cout << a << endl
#define tget(a, b) get<a>(b)
using namespace std;
typedef long long ll;
const ll INF = 1e17;
const int MOD = 1e9 + 7;
ll lim;
ll N, W[100], V[100];
ll dp[101][100001];
int main(void) {
  cin >> N >> lim;
  FOR(i, 0, N) cin >> W[i] >> V[i];
  fill(dp[0], dp[0] + 100001, INF);
  dp[0][0] = 0;
  FOR(i, 0, N) {
    for (ll k = 0; k <= 100000; k++) {
      if (k < V[i]) {
        dp[i + 1][k] = dp[i][k];
      } else {
        dp[i + 1][k] = min(dp[i][k], dp[i][k - V[i]] + W[i]);
      }
    }
  }
  ll ans = 0;
  FOR(i, 0, 100001) if (dp[N][i] <= lim) ans = i;
  ct(ans);
  return 0;
}