Problem p03163 (C++)

Buggy submission (status: Runtime Error):
/*


                 """Bismillahir Rahmanur Rahim"""

*/
#include <bits/stdc++.h>
using namespace std;
#define pi 2 * acos(0.0)
#define ll long long int
#define pb push_back
#define pf push_front
const ll sz = 1000001;
#define ek(x) __builtin_popcount(x)
#define ses '\n'
#define stm istringstream
#define sob(x) x.begin(), x.end()
#define ghora                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);
#define gcd __gcd
ll lcm(ll x, ll y) { return (x * y) / gcd(x, y); }
#define tin                                                                    \
  ll T;                                                                        \
  cin >> T;                                                                    \
  for (ll o = 1; o <= T; o++)
#define tout cout << "Case " << o << ": ";
ll val[102];
ll wt[103];
ll dp[102][103];
ll n, W;
ll knapsack(ll i, ll w) {
  if (i > n - 1)
    return 0;
  if (dp[i][w] > 0)
    return dp[i][w];
  ll x = -5, y;
  if (w + wt[i] <= W)
    x = val[i] + knapsack(i + 1, w + wt[i]);
  y = knapsack(i + 1, w);
  return dp[i][w] = max(x, y);
}
int main() {

  memset(dp, -1, sizeof(dp));
  cin >> n >> W;
  for (ll i = 0; i < n; i++) {
    cin >> wt[i] >> val[i];
  }
  ll x = knapsack(0, 0);
  cout << x << ses;

  return 0;
}
/*   --------------------
    |      ~SOHAN~       |
    |   ~Chandler68~     |
     --------------------
    || VALAR MORGULIS||==|| ALL MEN MUST DIE ||
    \\ Power Is Power//
    || I Can Do This All day ||
    // We are on a Break \\ // How you doin'? \\
    || Say My Name || ~~ || I Am The Who Knocks ||
    // I Am Ted Mosby Architect \\
    || It Is Legen --wait for it -- dary ,Legendary ||
    \\ Penny - Penny - Penny // -- Bazinga

*/


Fixed submission:
/*


                 """Bismillahir Rahmanur Rahim"""

*/
#include <bits/stdc++.h>
using namespace std;
#define pi 2 * acos(0.0)
#define ll long long int
#define pb push_back
#define pf push_front
const ll sz = 1000001;
#define ek(x) __builtin_popcount(x)
#define ses '\n'
#define stm istringstream
#define sob(x) x.begin(), x.end()
#define ghora                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);
#define gcd __gcd
ll lcm(ll x, ll y) { return (x * y) / gcd(x, y); }
#define tin                                                                    \
  ll T;                                                                        \
  cin >> T;                                                                    \
  for (ll o = 1; o <= T; o++)
#define tout cout << "Case " << o << ": ";
ll val[102];
ll wt[103];
ll dp[105][100050];
ll n, W;
ll knapsack(ll i, ll w) {
  if (i > n - 1)
    return 0;
  if (dp[i][w] > 0)
    return dp[i][w];
  ll x = -5, y;
  if (w + wt[i] <= W)
    x = val[i] + knapsack(i + 1, w + wt[i]);
  y = knapsack(i + 1, w);
  return dp[i][w] = max(x, y);
}
int main() {

  memset(dp, -1, sizeof(dp));
  cin >> n >> W;
  for (ll i = 0; i < n; i++) {
    cin >> wt[i] >> val[i];
  }
  ll x = knapsack(0, 0);
  cout << x << ses;

  return 0;
}
/*   --------------------
    |      ~SOHAN~       |
    |   ~Chandler68~     |
     --------------------
    || VALAR MORGULIS||==|| ALL MEN MUST DIE ||
    \\ Power Is Power//
    || I Can Do This All day ||
    // We are on a Break \\ // How you doin'? \\
    || Say My Name || ~~ || I Am The Who Knocks ||
    // I Am Ted Mosby Architect \\
    || It Is Legen --wait for it -- dary ,Legendary ||
    \\ Penny - Penny - Penny // -- Bazinga

*/