Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define fio                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define itc                                                                    \
  ll t;                                                                        \
  cin >> t;                                                                    \
  while (t--)
#define otc                                                                    \
  ll t = 1;                                                                    \
  while (t--)
#define PI 3.141592653589793238
// ang=(PI*ang)/180.0 in radian
#define ll long long int
#define ld long double
#define pb push_back
#define gcd(a, b) __gcd(a, b)
#define fi first
#define se second
int dx[4] = {0, -1, 0, 1};
int dy[4] = {1, 0, -1, 0};
ll n, w;
ll dp[105][100005];
ll knapsack(ll pos, ll cap, ll ar[][2]) {
  if (pos >= n || cap <= 0)
    return 0;

  if (dp[pos][cap] != -1)
    return dp[pos][cap];

  if (ar[pos][0] > cap)
    dp[pos][cap] = knapsack(pos + 1, cap, ar);
  else
    dp[pos][cap] = max((ar[pos][1] + knapsack(pos + 1, cap - ar[pos][0], ar)),
                       knapsack(pos + 1, cap, ar));

  return dp[pos][cap];
}
void solve() {
  cin >> n >> w;
  ll ar[n][2];
  for (ll i = 0; i < n; i++)
    cin >> ar[i][0] >> ar[i][1];

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

  cout << knapsack(0, w, ar) << endl;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fio otc { solve(); }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fio                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define itc                                                                    \
  ll t;                                                                        \
  cin >> t;                                                                    \
  while (t--)
#define otc                                                                    \
  ll t = 1;                                                                    \
  while (t--)
#define PI 3.141592653589793238
// ang=(PI*ang)/180.0 in radian
#define ll long long int
#define ld long double
#define pb push_back
#define gcd(a, b) __gcd(a, b)
#define fi first
#define se second
int dx[4] = {0, -1, 0, 1};
int dy[4] = {1, 0, -1, 0};
ll n, w;
ll dp[105][100005];
ll knapsack(ll pos, ll cap, ll ar[][2]) {
  if (pos >= n || cap <= 0)
    return 0;

  if (dp[pos][cap] != -1)
    return dp[pos][cap];

  if (ar[pos][0] > cap)
    dp[pos][cap] = knapsack(pos + 1, cap, ar);
  else
    dp[pos][cap] = max((ar[pos][1] + knapsack(pos + 1, cap - ar[pos][0], ar)),
                       knapsack(pos + 1, cap, ar));

  return dp[pos][cap];
}
void solve() {
  cin >> n >> w;
  ll ar[n][2];
  for (ll i = 0; i < n; i++)
    cin >> ar[i][0] >> ar[i][1];

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

  cout << knapsack(0, w, ar) << endl;
}
int main() {
  /*#ifndef ONLINE_JUDGE
          freopen("input.txt", "r", stdin);
          freopen("output.txt", "w", stdout);
  #endif
          fio
          otc
          {*/
  solve();
  //}
  return 0;
}