Problem p03163 (C++)

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

using namespace std;

#define SZ(items) (int)items.size()
#define CLR(a) memset(a, 0, sizeof(a))
#define SET(a) memset(a, -1, sizeof(a))
#define nl "\n";

int dx[] = {0, 0, 1, -1, -1, -1, 1, 1};
int dy[] = {1, -1, 0, 0, -1, 1, 1, -1};

template <class T> inline T biton(T n, T pos) { return n | ((T)1 << pos); }
template <class T> inline T bitoff(T n, T pos) { return n & ~((T)1 << pos); }
template <class T> inline T ison(T n, T pos) {
  return (bool)(n & ((T)1 << pos));
}
template <class T> inline T gcd(T a, T b) {
  while (b) {
    a = a % b;
    swap(a, b);
  }
  return a;
}
template <class T> inline T bigmod(T p, T e, T m) {
  T ret = 1;
  for (; e > 0; e >>= 1) {
    if (e & 1)
      ret = (ret * p) % m;
    p = (p * p) % m;
  }
  return (T)ret;
};

#ifdef DEBUG
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " is " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " is " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define debug(...)
#endif

int dp[101][10005];
int n, tw;
vector<pair<long long, long long>> items;

long long recur(int pos, int w) {

  if (pos == n) {
    return 0;
  }

  if (dp[pos][w] != -1) {
    return dp[pos][w];
  }

  long long mx = 0;

  if (w + items[pos].first <= tw) {
    mx = max(mx, items[pos].second + recur(pos + 1, w + items[pos].first));
  }

  mx = max(mx, recur(pos + 1, w));

  return dp[pos][w] = mx;
}

void solve() {
  SET(dp);

  cin >> n >> tw;

  items.resize(n);

  for (int i = 0; i < n; i++) {
    cin >> items[i].first >> items[i].second;
  }

  cout << recur(0, 0) << nl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(10);
  cout.tie(nullptr);
  cin.tie(nullptr);

  int t = 1;

  while (t--) {
    solve();
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define SZ(items) (int)items.size()
#define CLR(a) memset(a, 0, sizeof(a))
#define SET(a) memset(a, -1, sizeof(a))
#define nl "\n";

int dx[] = {0, 0, 1, -1, -1, -1, 1, 1};
int dy[] = {1, -1, 0, 0, -1, 1, 1, -1};

template <class T> inline T biton(T n, T pos) { return n | ((T)1 << pos); }
template <class T> inline T bitoff(T n, T pos) { return n & ~((T)1 << pos); }
template <class T> inline T ison(T n, T pos) {
  return (bool)(n & ((T)1 << pos));
}
template <class T> inline T gcd(T a, T b) {
  while (b) {
    a = a % b;
    swap(a, b);
  }
  return a;
}
template <class T> inline T bigmod(T p, T e, T m) {
  T ret = 1;
  for (; e > 0; e >>= 1) {
    if (e & 1)
      ret = (ret * p) % m;
    p = (p * p) % m;
  }
  return (T)ret;
};

#ifdef DEBUG
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " is " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " is " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define debug(...)
#endif

long long dp[101][100005];
int n, tw;
vector<pair<long long, long long>> items;

long long recur(int pos, int w) {

  if (pos == n) {
    return 0;
  }

  if (dp[pos][w] != -1) {
    return dp[pos][w];
  }

  long long mx = 0;

  if (w + items[pos].first <= tw) {
    mx = max(mx, items[pos].second + recur(pos + 1, w + items[pos].first));
  }

  mx = max(mx, recur(pos + 1, w));

  return dp[pos][w] = mx;
}

void solve() {
  SET(dp);

  cin >> n >> tw;

  items.resize(n);

  for (int i = 0; i < n; i++) {
    cin >> items[i].first >> items[i].second;
  }

  cout << recur(0, 0) << nl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(10);
  cout.tie(nullptr);
  cin.tie(nullptr);

  int t = 1;

  while (t--) {
    solve();
  }

  return 0;
}