Problem p03163 (C++)

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

#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define si set<int>
#define usi unordered_set<int>
#define mii map<int, int>
#define umii unordered_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 inf 1e18
#define endl '\n'
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w(t)                                                                   \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

void jaiswal_sahil() {
  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
}

const int N = 1e2;
int dp[N][N];
int n;
int maxVal(int i, int w, int *wt, int *v) {

  if (i == n)
    return 0;

  if (dp[i][w] != -1)
    return dp[i][w];

  int q1 = -inf, q2 = -inf;
  if (w >= wt[i]) {
    q1 = v[i] + maxVal(i + 1, w - wt[i], wt, v);
  }
  q2 = maxVal(i + 1, w, wt, v);

  return dp[i][w] = max(q1, q2);
}
int32_t main() {
  jaiswal_sahil();

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

  memset(dp, -1, sizeof(dp));
  int ans = maxVal(0, w, wt, v);
  cout << ans << endl;
  return 0;
}

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

#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define si set<int>
#define usi unordered_set<int>
#define mii map<int, int>
#define umii unordered_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 inf 1e18
#define endl '\n'
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w(t)                                                                   \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

void jaiswal_sahil() {
  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
}

const int N = 1e2 + 2, W = 1e5 + 5;
int dp[N][W];
int n;
int maxVal(int i, int w, int *wt, int *v) {

  if (i == n)
    return 0;

  if (dp[i][w] != -1)
    return dp[i][w];

  int q1 = -inf, q2 = -inf;
  if (w >= wt[i]) {
    q1 = v[i] + maxVal(i + 1, w - wt[i], wt, v);
  }
  q2 = maxVal(i + 1, w, wt, v);

  return dp[i][w] = max(q1, q2);
}
int32_t main() {
  jaiswal_sahil();

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

  memset(dp, -1, sizeof(dp));
  int ans = maxVal(0, w, wt, v);
  cout << ans << endl;
  return 0;
}