Problem p03163 (C++)

Buggy submission (status: Runtime Error):
// Program.cpp
// Author : Aaryan Srivastava
#include <bits/stdc++.h>
#include <chrono>
#include <random>
#define pb push_back
#define popb pop_back
#define ull unsigned long long
#define beg begin
#define mp make_pair
#define pii pair<int, int>
#define piii pair<pii, int>
#define rep(i, n) for (int(i) = 0; i < (n); i++)
#define repA(i, x, y) for (int i = (x); i <= (y); i++)
#define repD(i, x, y) for (int i = (x); i >= (y); i--)
#define all(c) (c).begin(), (c).end()
#define ff first
#define ss second
#define fill(a, val) memset(a, val, sizeof(a))
#define Randomize                                                              \
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
using ll = long long;
#define int ll
const int N = 1e5 + 5;
const int mod = 1e9 + 5;
const int inf = 1e18;
const int SZ = 101;
const double eps = 1e-9;
using namespace std;
#ifdef AaryanS
#define mycout cout
#define mycerr cerr
#endif

#ifndef AaryanS
#define mycout                                                                 \
  if (false)                                                                   \
  cout
#define mycerr                                                                 \
  if (false)                                                                   \
  cerr
#define AaryanS 0
#endif

#define debug(x) mycout << #x << " " << x << endl;

// 17:54 stop
//  19:08 start
// 19:16 stop
pii arr[105];
int dp[105][(int)1e4 + 5], n, W;
int knap(int curr, int w) {
  if (curr >= n || w >= W)
    return 0;
  if (dp[curr][w] != -1)
    return dp[curr][w];
  int res = knap(curr + 1, w);
  if (arr[curr].ff + w <= W)
    res = max(res, arr[curr].ss + knap(curr + 1, arr[curr].ff + w));
  return dp[curr][w] = res;
}
void solve() {
  cin >> n >> W;
  repA(i, 0, n - 1) { cin >> arr[i].ff >> arr[i].ss; }
  repA(i, 0, n + 2) {
    repA(j, 0, W + 2) { dp[i][j] = -1; }
  }
  sort(arr, arr + n);
  // reverse(arr+1,arr+n+1) ;
  //  repD(i,n-1,0){
  //  	repA(j,0,w){
  //  		if(j+arr[i+1].ff > w)
  //  			break ;
  //  		dp[i][j] += max(dp[i+1][j], arr[i].ss + dp[i+1][j+arr[i].ff]) ;//take
  //  i+1 or not.
  //  	}
  //  }
  cout << knap(0, 0) << endl;
  rep(i, n + 1) {
    rep(j, W + 1) mycout << dp[i][j] << " ";
    mycout << endl;
  }
}

int32_t main(int32_t argc, char *argv[]) {
  double t1 = clock();
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int TC = 1, t = 0;
  // cin >> TC ;
  while (t++ < TC) {
    // cout << "Case " << t << ":\n" ;
    solve();
  }
  mycerr << "Time : " << 1000 * (clock() - t1) / CLOCKS_PER_SEC << " ms\n";
  return 0;
}

Fixed submission:
// Program.cpp
// Author : Aaryan Srivastava
#include <bits/stdc++.h>
#include <chrono>
#include <random>
#define pb push_back
#define popb pop_back
#define ull unsigned long long
#define beg begin
#define mp make_pair
#define pii pair<int, int>
#define piii pair<pii, int>
#define rep(i, n) for (int(i) = 0; i < (n); i++)
#define repA(i, x, y) for (int i = (x); i <= (y); i++)
#define repD(i, x, y) for (int i = (x); i >= (y); i--)
#define all(c) (c).begin(), (c).end()
#define ff first
#define ss second
#define fill(a, val) memset(a, val, sizeof(a))
#define Randomize                                                              \
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
using ll = long long;
#define int ll
const int N = 1e5 + 5;
const int mod = 1e9 + 5;
const int inf = 1e18;
const int SZ = 101;
const double eps = 1e-9;
using namespace std;
#ifdef AaryanS
#define mycout cout
#define mycerr cerr
#endif

#ifndef AaryanS
#define mycout                                                                 \
  if (false)                                                                   \
  cout
#define mycerr                                                                 \
  if (false)                                                                   \
  cerr
#define AaryanS 0
#endif

#define debug(x) mycout << #x << " " << x << endl;

// 17:54 stop
//  19:08 start
// 19:16 stop
pii arr[105];
int dp[105][(int)1e5 + 5], n, W;
int knap(int curr, int w) {
  if (curr >= n || w >= W)
    return 0;
  if (dp[curr][w] != -1)
    return dp[curr][w];
  int res = knap(curr + 1, w);
  if (arr[curr].ff + w <= W)
    res = max(res, arr[curr].ss + knap(curr + 1, arr[curr].ff + w));
  return dp[curr][w] = res;
}
void solve() {
  cin >> n >> W;
  repA(i, 0, n - 1) { cin >> arr[i].ff >> arr[i].ss; }
  repA(i, 0, n + 2) {
    repA(j, 0, W + 2) { dp[i][j] = -1; }
  }
  sort(arr, arr + n);
  // reverse(arr+1,arr+n+1) ;
  //  repD(i,n-1,0){
  //  	repA(j,0,w){
  //  		if(j+arr[i+1].ff > w)
  //  			break ;
  //  		dp[i][j] += max(dp[i+1][j], arr[i].ss + dp[i+1][j+arr[i].ff]) ;//take
  //  i+1 or not.
  //  	}
  //  }
  cout << knap(0, 0) << endl;
  rep(i, n + 1) {
    rep(j, W + 1) mycout << dp[i][j] << " ";
    mycout << endl;
  }
}

int32_t main(int32_t argc, char *argv[]) {
  double t1 = clock();
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int TC = 1, t = 0;
  // cin >> TC ;
  while (t++ < TC) {
    // cout << "Case " << t << ":\n" ;
    solve();
  }
  mycerr << "Time : " << 1000 * (clock() - t1) / CLOCKS_PER_SEC << " ms\n";
  return 0;
}