Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define lli long long int
#define ulli unsigned long long int
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define pf pop_front()
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define rep(i, a, b) for (lli i = a; i < b; i++)
#define repe(i, a, b) for (lli i = a; i <= b; i++)
#define all(v) v.begin(), v.end()
#define mod 1000000007
#define inf 100000000000000000
#define lld long double
#define pll pair<long long int, long long int>
#define pii pair<int, int>
#define vll vector<lli>
#define vii vector<int>
#define endl '\n'
#define pie acosl(-1.0);
#define setp(k, x) fixed << setprecision(k) << (x)
#define deb(x) cerr << #x << ": " << x << " " << endl;
#define debug(x)                                                               \
  cout << "m here-" << #x << " ..."                                            \
       << "\n";
#define printa(a, n)                                                           \
  for (lli i = 0; i < n; i++)                                                  \
    cout << a[i] << " ";                                                       \
  cout << "\n";
#define printv(a)                                                              \
  for (lli i = 0; i < a.size(); i++)                                           \
    cout << a[i] << " ";                                                       \
  cout << "\n";
bool sortcol(const vector<lli> &v1,
             const vector<lli> &v2) // sort on basis of column //use-
                                    // sort(vect.begin(), vect.end(),sortcol);
{
  return v1[1] < v2[1];
} //[] o based indexing in column
bool sortpair(const pair<lli, lli> &a, const pair<lli, lli> &b) {
  return a.second < b.second;
}
struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    // http://xorshift.di.unimi.it/splitmix64.c
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }

  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set                                                            \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>

//////////////////////////////////////////////////////////////////////////////////////////////
void solve();
//////////////////////////////////////////////////////////////////////////////////////////////

int main() {
  // fastio
  lli t;
  cin >> t;
  while (t--) {
    solve();
  }
}
///////////////////////////////////////////
void solve() {
  lli n, i, d, a, b, c, j, k, w;

  cin >> n >> w;
  lli wt[n], val[n];
  for (i = 0; i < n; i++) {
    cin >> wt[i] >> val[i];
  }
  lli dp[n + 1][w + 1];
  for (i = 0; i < n + 1; i++) {
    dp[i][0] = 0;
  }

  for (i = 0; i < w + 1; i++) {
    dp[0][i] = 0;
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= w; j++) {

      if (wt[i - 1] <= j) {
        dp[i][j] = max(val[i - 1] + (dp[i - 1][j - wt[i - 1]]), (dp[i - 1][j]));

      } else
        dp[i][j] = dp[i - 1][j];
    }
  }

  // for(i=0;i<n;i++)
  //   for(j=0;j<w;j++)
  //     cout<<dp[i][j]<<" ";
  cout << dp[n][w] << "\n";
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define lli long long int
#define ulli unsigned long long int
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define pf pop_front()
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define rep(i, a, b) for (lli i = a; i < b; i++)
#define repe(i, a, b) for (lli i = a; i <= b; i++)
#define all(v) v.begin(), v.end()
#define mod 1000000007
#define inf 100000000000000000
#define lld long double
#define pll pair<long long int, long long int>
#define pii pair<int, int>
#define vll vector<lli>
#define vii vector<int>
#define endl '\n'
#define pie acosl(-1.0);
#define setp(k, x) fixed << setprecision(k) << (x)
#define deb(x) cerr << #x << ": " << x << " " << endl;
#define debug(x)                                                               \
  cout << "m here-" << #x << " ..."                                            \
       << "\n";
#define printa(a, n)                                                           \
  for (lli i = 0; i < n; i++)                                                  \
    cout << a[i] << " ";                                                       \
  cout << "\n";
#define printv(a)                                                              \
  for (lli i = 0; i < a.size(); i++)                                           \
    cout << a[i] << " ";                                                       \
  cout << "\n";
bool sortcol(const vector<lli> &v1,
             const vector<lli> &v2) // sort on basis of column //use-
                                    // sort(vect.begin(), vect.end(),sortcol);
{
  return v1[1] < v2[1];
} //[] o based indexing in column
bool sortpair(const pair<lli, lli> &a, const pair<lli, lli> &b) {
  return a.second < b.second;
}
struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    // http://xorshift.di.unimi.it/splitmix64.c
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }

  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set                                                            \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>

//////////////////////////////////////////////////////////////////////////////////////////////
void solve();
//////////////////////////////////////////////////////////////////////////////////////////////

int main() {
  // fastio
  // 	lli  t;
  //	cin>>t;
  // while(t--)
  { solve(); }
}
///////////////////////////////////////////
void solve() {
  lli n, i, d, a, b, c, j, k, w;

  cin >> n >> w;
  lli wt[n], val[n];
  for (i = 0; i < n; i++) {
    cin >> wt[i] >> val[i];
  }
  lli dp[n + 1][w + 1];
  for (i = 0; i < n + 1; i++) {
    dp[i][0] = 0;
  }

  for (i = 0; i < w + 1; i++) {
    dp[0][i] = 0;
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= w; j++) {

      if (wt[i - 1] <= j) {
        dp[i][j] = max(val[i - 1] + (dp[i - 1][j - wt[i - 1]]), (dp[i - 1][j]));

      } else
        dp[i][j] = dp[i - 1][j];
    }
  }

  // for(i=0;i<n;i++)
  //   for(j=0;j<w;j++)
  //     cout<<dp[i][j]<<" ";
  cout << dp[n][w] << "\n";
}