Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define Ull unsigned long long
#define pb push_back
#define INF 2e16
#define PI acos(-1.0)
#define mk make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define min4(a, b, c, d) min(a, min(b, min(c, d)))
#define max4(a, b, c, d) max(a, max(b, max(c, d)))
#define SQR(a) ((a) * (a))
#define MEM(a, x) memset(a, x, sizeof(a))
#define ABS(x) ((x) < 0 ? -(x) : (x))

#define SORT(v) sort(v.begin(), v.end())
#define REV(v) reverse(v.begin(), v.end())
#define FastRead                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(nullptr);

const int N = 2;
const ll MOD = 1e9 + 7;

void Solve() {
  ll n, w;
  cin >> n >> w;
  ll dp[n][w + 1];
  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j <= w; j++)
      dp[i][j] = 0;
  }

  ll wt[n], val[n];

  for (ll i = 0; i < n; i++)
    cin >> wt[i] >> val[i];

  for (ll i = 0; i <= w; i++) {
    if (i >= wt[0])
      dp[0][i] = val[0];
  }

  for (ll i = 1; i <= n; i++) {
    for (ll j = 0; j <= w; j++) {
      if (j >= wt[i])
        dp[i][j] = max(dp[i - 1][j], val[i] + dp[i - 1][j - wt[i]]);

      else
        dp[i][j] = dp[i - 1][j];
    }
  }
  /*for (ll i = 0; i < n; i++)
  {
          for (ll j = 0; j <= w; j++)
          {
                  cout << dp[i][j] << " ";
          }
          cout << "\n";
  }*/
  cout << dp[n - 1][w] << "\n";
}

int main() {

  FastRead;
  int T = 1;
  // cin>>T;
  while (T--) {
    Solve();
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define Ull unsigned long long
#define pb push_back
#define INF 2e16
#define PI acos(-1.0)
#define mk make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define min4(a, b, c, d) min(a, min(b, min(c, d)))
#define max4(a, b, c, d) max(a, max(b, max(c, d)))
#define SQR(a) ((a) * (a))
#define MEM(a, x) memset(a, x, sizeof(a))
#define ABS(x) ((x) < 0 ? -(x) : (x))

#define SORT(v) sort(v.begin(), v.end())
#define REV(v) reverse(v.begin(), v.end())
#define FastRead                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(nullptr);

const int N = 2;
const ll MOD = 1e9 + 7;

void Solve() {
  ll n, w;
  cin >> n >> w;
  ll dp[n][w + 1];
  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j <= w; j++)
      dp[i][j] = 0;
  }

  ll wt[n], val[n];

  for (ll i = 0; i < n; i++)
    cin >> wt[i] >> val[i];

  for (ll i = 0; i <= w; i++) {
    if (i >= wt[0])
      dp[0][i] = val[0];
  }

  for (ll i = 1; i < n; i++) {
    for (ll j = 0; j <= w; j++) {
      if (j >= wt[i])
        dp[i][j] = max(dp[i - 1][j], val[i] + dp[i - 1][j - wt[i]]);

      else
        dp[i][j] = dp[i - 1][j];
    }
  }
  /*for (ll i = 0; i < n; i++)
  {
          for (ll j = 0; j <= w; j++)
          {
                  cout << dp[i][j] << " ";
          }
          cout << "\n";
  }*/
  cout << dp[n - 1][w] << "\n";
}

int main() {

  FastRead;
  int T = 1;
  // cin>>T;
  while (T--) {
    Solve();
  }

  return 0;
}