Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
using vl = vector<ll>;

/* short */
#define pb push_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) begin(v), end(v)
#define RALL(v) rbegin(v), rend(v)

/* REPmacro */
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORR(i, a, b) for (int i = (a); i >= (b); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOREACH(x, a) for (auto x : a)

/* exchange */
#define CHMIN(a, b) (a) = min((ll)(a), (ll)(b))
#define CHMAX(a, b) (a) = max((ll)(a), (ll)(b))

/* function */
#define IN(x) cin >> x
#define DEBUG(x) cerr << (x) << " "
#define LN() cerr << "\n"
#define PRINT(x) cout << (x) << endl
#define BR cout << endl

/* const */
const int ARRAY = 100005;
const int INF = 1001001001;          // 10^9
const ll LINF = 1001001001001001001; // 10^18
const int MOD = 1e9 + 7;

ll ret = 0;
string s;
ll w[101];
ll v[101];
ll dp[101][ARRAY];

ll knapsack(ll i, ll j) {
  if (i < 0 && j <= 0)
    return 0;
  if (i < 0 && j > 0)
    return INF;
  if (dp[i][j] != -1)
    return dp[i][j];
  ll ans = min(knapsack(i - 1, j), w[i] + knapsack(i - 1, j - v[i]));
  dp[i][j] = ans;
  return ans;
}

int main(void) {

  ll N, W;
  IN(N);
  IN(W);
  REP(i, N) {
    IN(w[i]);
    IN(v[i]);
  }

  REP(i, 101) {
    REP(j, ARRAY) { dp[i][j] = -1; }
  }

  REP(i, ARRAY) {
    ll weight = knapsack(N - 1, i);
    // DEBUG(N-1);
    // DEBUG(i);
    // DEBUG("=>");
    // DEBUG(weight);
    // LN();
    if (weight > W) {
      ret = i - 1;
      break;
    }
  }

  PRINT(ret);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
using vl = vector<ll>;

/* short */
#define pb push_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) begin(v), end(v)
#define RALL(v) rbegin(v), rend(v)

/* REPmacro */
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORR(i, a, b) for (int i = (a); i >= (b); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOREACH(x, a) for (auto x : a)

/* exchange */
#define CHMIN(a, b) (a) = min((ll)(a), (ll)(b))
#define CHMAX(a, b) (a) = max((ll)(a), (ll)(b))

/* function */
#define IN(x) cin >> x
#define DEBUG(x) cerr << (x) << " "
#define LN() cerr << "\n"
#define PRINT(x) cout << (x) << endl
#define BR cout << endl

/* const */
const int ARRAY = 100005;
const int INF = 1001001001;          // 10^9
const ll LINF = 1001001001001001001; // 10^18
const int MOD = 1e9 + 7;

ll ret = 0;
string s;
ll w[101];
ll v[101];
ll dp[101][ARRAY];

ll knapsack(ll i, ll j) {
  if (j <= 0)
    return 0;
  if (i < 0 && j > 0)
    return INF;
  if (dp[i][j] != -1)
    return dp[i][j];
  ll ans = min(knapsack(i - 1, j), w[i] + knapsack(i - 1, j - v[i]));
  dp[i][j] = ans;
  return ans;
}

int main(void) {

  ll N, W;
  IN(N);
  IN(W);
  REP(i, N) {
    IN(w[i]);
    IN(v[i]);
  }

  REP(i, 101) {
    REP(j, ARRAY) { dp[i][j] = -1; }
  }

  REP(i, ARRAY) {
    ll weight = knapsack(N - 1, i);
    // DEBUG(N-1);
    // DEBUG(i);
    // DEBUG("=>");
    // DEBUG(weight);
    // LN();
    if (weight > W) {
      ret = i - 1;
      break;
    }
  }

  PRINT(ret);
}