Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

#define ll long long
#define ld long double
typedef vector<ll> vi;           // Vector of long long
typedef vector<vi> vvi;          // Vector of vi
typedef vector<pair<ll, ll>> ii; // Vector of pairs
#define ff first                 // For pairs
#define ss second                // For pairs
#define sz(a) int((a).size())    // Function to determine size of any container
#define pb push_back             // Pushback to vector
#define mp make_pair             // Makes pairs to be stored as pair
#define all(c) (c).begin(), (c).end() // Mainly used by me in sorting
// ordered_set adds two new functions to set - (set).find_by_order([kth element
// based on zero indexing]) and order_of_key() order_of_key returns number of
// elements less that parameter. If element exists, that order is its index
#define ordered_set                                                            \
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
#define MAXW (ll)(1e5 + 5)
#define MAXN (ll)(1e2 + 5)
#define INF (ll)(1e12)

ll n, w;
ll dp[MAXN][MAXN];
ll mg[MAXN];
ll val[MAXN];

ll solve(ll N, ll W) {
  if (W < 0)
    return -INF;
  if (N < 0)
    return 0;

  if (dp[N][W] == -1)
    dp[N][W] = max(solve(N - 1, W - mg[N]) + val[N], solve(N - 1, W));
  return dp[N][W];
}

int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n >> w;
  for (int i = 0; i < n; i++)
    cin >> mg[i] >> val[i];

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

  cout << solve(n - 1, w) << "\n";
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

#define ll long long
#define ld long double
typedef vector<ll> vi;           // Vector of long long
typedef vector<vi> vvi;          // Vector of vi
typedef vector<pair<ll, ll>> ii; // Vector of pairs
#define ff first                 // For pairs
#define ss second                // For pairs
#define sz(a) int((a).size())    // Function to determine size of any container
#define pb push_back             // Pushback to vector
#define mp make_pair             // Makes pairs to be stored as pair
#define all(c) (c).begin(), (c).end() // Mainly used by me in sorting
// ordered_set adds two new functions to set - (set).find_by_order([kth element
// based on zero indexing]) and order_of_key() order_of_key returns number of
// elements less that parameter. If element exists, that order is its index
#define ordered_set                                                            \
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
#define MAXW (ll)(1e5 + 5)
#define MAXN (ll)(1e2 + 5)
#define INF (ll)(1e12)

ll n, w;
ll dp[MAXN][MAXW];
ll mg[MAXN];
ll val[MAXN];

ll solve(ll N, ll W) {
  if (W < 0)
    return -INF;
  if (N < 0)
    return 0;

  if (dp[N][W] == -1)
    dp[N][W] = max(solve(N - 1, W - mg[N]) + val[N], solve(N - 1, W));
  return dp[N][W];
}

int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n >> w;
  for (int i = 0; i < n; i++)
    cin >> mg[i] >> val[i];

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

  cout << solve(n - 1, w) << "\n";
}