Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define fastii                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define endl "\n"
#define f0(i, n) for (int i = 0; i < n; i++)
#define f1(i, n) for (int i = 1; i <= n; i++)
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define ll long long int
#define pi pair<int, int>
#define ai(v) v.begin(), v.end()
#define air(v) v.rbegin(), v.rend()
#define pii 3.14159265358979323
#define inf LLONG_MAX
#define fill(a, b) memset(a, b, sizeof(a))
#define mod 1000000007

ll static t[101][101];

ll mod_pow(ll a, ll b, ll m) {
  ll res = 1;
  while (b) {
    if (b & 1) {
      res = (res * a) % m;
    }
    a = (a * a) % m;
    b >>= 1;
  }
  return res;
}
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return (a % b, b);
}

void sieveofEratosthenes(ll n) {
  bool prime[n + 1];
  fill(prime, true);
  for (int i = 2; i * i <= n; i++) {
    if (prime[i] == true) {
      for (int j = i * i; j <= n; j += i)
        prime[j] = false;
    }
  }
  for (int i = 2; i <= n; i++) {
    if (prime[i])
      cout << i << " ";
  }
}

ll mod_inverse(ll a) { return mod_pow(a, mod - 2, mod); }
ll knapsack(ll wt[], ll val[], ll W, ll n) {

  if (n == 0 || W == 0)
    return 0;
  if (t[n][W] != -1)
    return t[n][W];
  if (wt[n - 1] <= W)
    return t[n][W] = max(val[n - 1] + knapsack(wt, val, W - wt[n - 1], n - 1),
                         knapsack(wt, val, W, n - 1));
  else if (wt[n - 1] > W)
    return (t[n][W] = knapsack(wt, val, W, n - 1));
}
void solve() {
  ll n, W;
  cin >> n >> W;
  ll wt[n], val[n];
  f0(i, n) {
    fill(t, -1);
    cin >> wt[i];
    cin >> val[i];
  }

  cout << knapsack(wt, val, W, n) << endl;
  ;
}
signed main() {
  fastii;
  int T = 1;

  while (T--) {

    solve();
  }
  return 0;
}

/*Author :- Ankit Raj
 *Site:-
 *Topic:-
 * Competative Programming
 *email :-
 *Technocrats Institute Of Technology
 *5th Sem
 */


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fastii                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define endl "\n"
#define f0(i, n) for (int i = 0; i < n; i++)
#define f1(i, n) for (int i = 1; i <= n; i++)
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define ll long long int
#define pi pair<int, int>
#define ai(v) v.begin(), v.end()
#define air(v) v.rbegin(), v.rend()
#define pii 3.14159265358979323
#define inf LLONG_MAX
#define fill(a, b) memset(a, b, sizeof(a))
#define mod 1000000007

ll static t[101][100001];

ll mod_pow(ll a, ll b, ll m) {
  ll res = 1;
  while (b) {
    if (b & 1) {
      res = (res * a) % m;
    }
    a = (a * a) % m;
    b >>= 1;
  }
  return res;
}
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return (a % b, b);
}

void sieveofEratosthenes(ll n) {
  bool prime[n + 1];
  fill(prime, true);
  for (int i = 2; i * i <= n; i++) {
    if (prime[i] == true) {
      for (int j = i * i; j <= n; j += i)
        prime[j] = false;
    }
  }
  for (int i = 2; i <= n; i++) {
    if (prime[i])
      cout << i << " ";
  }
}

ll mod_inverse(ll a) { return mod_pow(a, mod - 2, mod); }
ll knapsack(ll wt[], ll val[], ll W, ll n) {

  if (n == 0 || W == 0)
    return 0;
  if (t[n][W] != -1)
    return t[n][W];
  if (wt[n - 1] <= W)
    return t[n][W] = max(val[n - 1] + knapsack(wt, val, W - wt[n - 1], n - 1),
                         knapsack(wt, val, W, n - 1));
  else if (wt[n - 1] > W)
    return (t[n][W] = knapsack(wt, val, W, n - 1));
}
void solve() {
  ll n, W;
  cin >> n >> W;
  ll wt[n], val[n];
  f0(i, n) {
    fill(t, -1);
    cin >> wt[i];
    cin >> val[i];
  }

  cout << knapsack(wt, val, W, n) << endl;
  ;
}
signed main() {
  fastii;
  int T = 1;

  while (T--) {

    solve();
  }
  return 0;
}

/*Author :- Ankit Raj
 *Site:-
 *Topic:-
 * Competative Programming
 *email :-
 *Technocrats Institute Of Technology
 *5th Sem
 */