Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define EPS 1e-7
#define LLMAX 9223372036854775807
#define INTMAX 2147483647
#define ULLMAX 18446744073709551615
#define precision(n) cout << setprecision(n) << fixed
#define forn(i, n) for (int i = 0; i < (int)n; ++i)
#define forr(i, t, n) for (int i = t; i < n; ++i)
#define rforr(i, t, n) (int i = n - 1; i >= t; --i)
#define rfor(i, n) for (int i = n - 1; i >= 0; --i)
#define rmod(x, y) (((x % y) + y) % y)
#define pb push_back
#define emp emplace_back
using namespace std;
typedef vector<int>::iterator vit;
typedef unsigned int uint;
typedef unsigned short ushort;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const int MAXN = 101;
const int MAXW = 1e5 + 1;

// STRUCTS

// OPERATORS

// GLOBALS
int n, cap;
ll dp[MAXN + 1][MAXW + 1];
int w[MAXN], v[MAXN];

// FUNCTIONS
/*
ll dyn()
{
        forn(i, n+1)
        {
                forn(j, cap+1)
                {
                        if (i == 0 or j == 0) dp[i][j] = 0;
                        else if (j<w[i-1]) dp[i][j] = dp[i-1][j];
                        else dp[i][j] = max(dp[i-1][j], dp[i-1][j-w[i-1]] +
ll(v[i-1]));
                }
        }
        return dp[n][cap];
}*/

ll td(int curn, int curw, ll curs) {
  if (curn == 0 or curw == 0)
    return curs;
  if (dp[curn][curw] != -1)
    return dp[curn][curw];
  if (curw - w[curn - 1] >= 0) {
    dp[curn][curw] =
        max(td(curn - 1, curw, curs),
            ll(v[curn - 1]) + td(curn - 1, curw - w[curn - 1], curs));
  } else
    dp[curn][curw] = td(curn - 1, curw, curs);
  return dp[curn][curw];
}
// PROBLEM:
// LINK:
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen("_input.txt", "r", stdin);
  freopen("_output.txt", "w", stdout);
#endif
  memset(dp, -1, sizeof(dp));
  cin >> n >> cap;
  forn(i, n) cin >> w[i] >> v[i];
  cout << td(n, cap, 0);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define EPS 1e-7
#define LLMAX 9223372036854775807
#define INTMAX 2147483647
#define ULLMAX 18446744073709551615
#define precision(n) cout << setprecision(n) << fixed
#define forn(i, n) for (int i = 0; i < (int)n; ++i)
#define forr(i, t, n) for (int i = t; i < n; ++i)
#define rforr(i, t, n) (int i = n - 1; i >= t; --i)
#define rfor(i, n) for (int i = n - 1; i >= 0; --i)
#define rmod(x, y) (((x % y) + y) % y)
#define pb push_back
#define emp emplace_back
using namespace std;
typedef vector<int>::iterator vit;
typedef unsigned int uint;
typedef unsigned short ushort;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const int MAXN = 101;
const int MAXW = 1e5 + 1;

// STRUCTS

// OPERATORS

// GLOBALS
int n, cap;
ll dp[MAXN + 1][MAXW + 1];
int w[MAXN], v[MAXN];

// FUNCTIONS
/*
ll dyn()
{
        forn(i, n+1)
        {
                forn(j, cap+1)
                {
                        if (i == 0 or j == 0) dp[i][j] = 0;
                        else if (j<w[i-1]) dp[i][j] = dp[i-1][j];
                        else dp[i][j] = max(dp[i-1][j], dp[i-1][j-w[i-1]] +
ll(v[i-1]));
                }
        }
        return dp[n][cap];
}*/

ll td(int curn, int curw, ll curs) {
  if (curn == 0 or curw == 0)
    return curs;
  if (dp[curn][curw] != -1)
    return dp[curn][curw];
  if (curw - w[curn - 1] >= 0) {
    dp[curn][curw] =
        max(td(curn - 1, curw, curs),
            ll(v[curn - 1]) + td(curn - 1, curw - w[curn - 1], curs));
  } else
    dp[curn][curw] = td(curn - 1, curw, curs);
  return dp[curn][curw];
}
// PROBLEM:
// LINK:
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  memset(dp, -1, sizeof(dp));
  cin >> n >> cap;
  forn(i, n) cin >> w[i] >> v[i];
  cout << td(n, cap, 0);

  return 0;
}