Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define tc(t)                                                                  \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define for0(i, n) for (int i = 0; i < n; i++)
#define loop(i, a, b) for (int i = a; i <= b; i++)
#define endl '\n'
#define inf 1e18
#define fi first
#define se second

int XX[8] = {+1, +1, +1, 0, 0, -1, -1, -1};
int YY[8] = {+1, 0, -1, +1, -1, +1, 0, -1};
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, 1, -1};

int wt[101], val[101];
int dp[101][1001];
int n, w;

int knapsack(int ind, int v) {
  if (ind == n and v == 0)
    return 0;
  if (ind == n or v < 0)
    return inf;
  if (dp[ind][v] != -1)
    return dp[ind][v];
  return dp[ind][v] = min(wt[ind] + knapsack(ind + 1, v - val[ind]),
                          knapsack(ind + 1, v));
}

signed main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

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

  cin >> n >> w;

  int sum = 0;

  for0(i, n) {
    cin >> wt[i] >> val[i];
    sum += val[i];
  }

  memset(dp, -1, sizeof(dp));

  for (int i = sum; i >= 0; i--) {
    if (knapsack(0, i) <= w) {
      cout << i << endl;
      return 0;
    }
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define tc(t)                                                                  \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define for0(i, n) for (int i = 0; i < n; i++)
#define loop(i, a, b) for (int i = a; i <= b; i++)
#define endl '\n'
#define inf 1e18
#define fi first
#define se second

int XX[8] = {+1, +1, +1, 0, 0, -1, -1, -1};
int YY[8] = {+1, 0, -1, +1, -1, +1, 0, -1};
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, 1, -1};

int wt[101], val[101];
int dp[101][100001];
int n, w;

int knapsack(int ind, int v) {
  if (ind == n and v == 0)
    return 0;
  if (ind == n or v < 0)
    return inf;
  if (dp[ind][v] != -1)
    return dp[ind][v];
  return dp[ind][v] = min(wt[ind] + knapsack(ind + 1, v - val[ind]),
                          knapsack(ind + 1, v));
}

signed main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

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

  cin >> n >> w;

  int sum = 0;

  for0(i, n) {
    cin >> wt[i] >> val[i];
    sum += val[i];
  }

  memset(dp, -1, sizeof(dp));

  for (int i = sum; i >= 0; i--) {
    if (knapsack(0, i) <= w) {
      cout << i << endl;
      return 0;
    }
  }

  return 0;
}