Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
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 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 inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w(x)                                                                   \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x--)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

void c_p_c() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
const int m = 2000000;
vi arr(105);
vi wrr(105);
int dp[105][100005];
int memo(int n, int w) {
  if (n == 0 || w <= 0) {
    return dp[n][w] = 0;
  }
  if (dp[n][w] != -1)
    return dp[n][w];
  // if(arr[n]>w)
  // return dp[n][w]=memo(n-1,w);
  return dp[n][w] = max((memo(n - 1, w - arr[n]) + wrr[n]), memo(n - 1, w));
}

int32_t main() {
  c_p_c();
  memset(dp, -1, sizeof(dp));
  int n, w;
  cin >> n >> w;
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
    cin >> wrr[i];
  }

  int x = memo(n, w);

  cout << dp[n][w];
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
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 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 inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w(x)                                                                   \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x--)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

void c_p_c() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
const int m = 2000000;
vi arr(105);
vi wrr(105);
int dp[105][100005];
int memo(int n, int w) {
  if (n == 0 || w <= 0) {
    return dp[n][w] = 0;
  }
  if (dp[n][w] != -1)
    return dp[n][w];
  if (arr[n] > w)
    return dp[n][w] = memo(n - 1, w);
  return dp[n][w] = max((memo(n - 1, w - arr[n]) + wrr[n]), memo(n - 1, w));
}

int32_t main() {
  c_p_c();
  memset(dp, -1, sizeof(dp));
  int n, w;
  cin >> n >> w;
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
    cin >> wrr[i];
  }

  int x = memo(n, w);

  cout << dp[n][w];
  return 0;
}