Problem p03163 (C++)

Buggy submission (status: Runtime Error):
// --------------------<optimizations>--------------------
#pragma GCC optimize("O3")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("unroll-loops")
// -------------------</optimizations>--------------------
#include <bits/stdc++.h>
#define ll long long
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp> //lower_bound , upper_bound
// using namespace __gnu_pbds;//kth smallest : *pd.find_by_order(k-1) , no. of
// elem < X : pd.order_of_key(X) , delete X : pd.erase(X). typedef tree<ll,
// null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;
#define f(i, a, b) for (ll i = a; i < b; i++)
#define fb(i, a, b) for (ll i = a; i >= b; i--)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define nl '\n'
#define pl pair<ll, ll>
#define all(v) v.begin(), v.end()
const ll mod = (1e+9) + 7;
const ll sz = 2e5 + 9;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll power(ll a, ll b, ll m = mod) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % m;
    b = b >> 1;
    a = (a * a) % m;
  }
  return res;
}

void solve() {
  ll tt = 1, n;
  cin >> tt;
  while (tt--) {
    ll cap;
    cin >> n >> cap;
    vector<ll> wt(n), val(n);
    f(i, 0, n) { cin >> wt[i] >> val[i]; }
    ll ans[n + 1][cap + 1];

    f(i, 0, n + 1) {
      f(w, 0, cap + 1) {
        if (i == 0 || w == 0)
          ans[i][w] = 0;
        else if (wt[i - 1] <= w)
          ans[i][w] =
              max(val[i - 1] + ans[i - 1][w - wt[i - 1]], ans[i - 1][w]);
        else
          ans[i][w] = ans[i - 1][w];
      }
    }
    cout << ans[n][cap] << nl;
  }
}

int main() {
#ifdef JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  clock_t beg = clock();
  solve();
  clock_t end = clock();
  fprintf(stderr, "%.3f sec, Copyright %c 2019 PyThor. \n",
          double(end - beg) / CLOCKS_PER_SEC, 184);
  return 0;
}


Fixed submission:
// --------------------<optimizations>--------------------
#pragma GCC optimize("O3")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("unroll-loops")
// -------------------</optimizations>--------------------
#include <bits/stdc++.h>
#define ll long long
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp> //lower_bound , upper_bound
// using namespace __gnu_pbds;//kth smallest : *pd.find_by_order(k-1) , no. of
// elem < X : pd.order_of_key(X) , delete X : pd.erase(X). typedef tree<ll,
// null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;
#define f(i, a, b) for (ll i = a; i < b; i++)
#define fb(i, a, b) for (ll i = a; i >= b; i--)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define nl '\n'
#define pl pair<ll, ll>
#define all(v) v.begin(), v.end()
const ll mod = (1e+9) + 7;
const ll sz = 2e5 + 9;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll power(ll a, ll b, ll m = mod) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % m;
    b = b >> 1;
    a = (a * a) % m;
  }
  return res;
}

void solve() {
  ll tt = 1, n;
  // cin>>tt;
  while (tt--) {
    ll cap;
    cin >> n >> cap;
    vector<ll> wt(n), val(n);
    f(i, 0, n) { cin >> wt[i] >> val[i]; }
    ll ans[n + 1][cap + 1];

    f(i, 0, n + 1) {
      f(w, 0, cap + 1) {
        if (i == 0 || w == 0)
          ans[i][w] = 0;
        else if (wt[i - 1] <= w)
          ans[i][w] =
              max(val[i - 1] + ans[i - 1][w - wt[i - 1]], ans[i - 1][w]);
        else
          ans[i][w] = ans[i - 1][w];
      }
    }
    cout << ans[n][cap] << nl;
  }
}

int main() {
#ifdef JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  clock_t beg = clock();
  solve();
  clock_t end = clock();
  fprintf(stderr, "%.3f sec, Copyright %c 2019 PyThor. \n",
          double(end - beg) / CLOCKS_PER_SEC, 184);
  return 0;
}