Problem p03163 (C++)

Buggy submission (status: Runtime Error):

/*
Author: Racer5x
 ***************************************   UNAUTHORISED COPYING OF CODE
PROHIBITED   **********************************
*/

/*#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")*/

#include <bits/stdc++.h>
#define int long long
#define double long double
#define pb emplace_back
#define pf emplace_front
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define x first
#define y second
#define sz(x) (int)x.size()
#define endl '\n'
#define hell 998244353
#define PI 3.141592653589
#define tezz                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define MAX 1000000000000000000
using namespace std;

int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int power(int x, int n) {
  int result = 1;
  while (n > 0) {
    if (n % 2 == 1)
      result = result * x;
    x = x * x;
    n = n / 2;
  }
  return result;
}

int n, w, dp[105][100005];

signed main() {
  tezz

#ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  memset(dp, 0, sizeof(dp));
  cin >> n >> w;
  vii mat;
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    mat.pb(a, b);
  }
  sort(all(mat));
  /*for(auto it:mat) cout<<it.x<<' '<<it.y<<endl;
      cout<<endl;*/

  dp[0][mat[0].x] = mat[0].y;

  for (int i = 1; i < n; i++) {
    for (int j = 0; j <= w; j++) {
      if (j < mat[i].x) {
        dp[i][j] = dp[i - 1][j];
      } else if (j == mat[i].x) {
        dp[i][j] = max(dp[i - 1][j], mat[i].y);
      } else if (j > mat[i].x) {
        dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - mat[i].x] + mat[i].y);
      }
    }
  }

  /*for(int i=0;i<n;i++){
      for(int j=0;j<=w;j++) cout<<dp[i][j]<<' ';
          cout<<endl;
  }*/
  int mx = 0;
  for (int i = 0; i <= w; i++)
    mx = max(mx, dp[n - 1][i]);
  cout << mx;
}

Fixed submission:

/*
Author: Racer5x
 ***************************************   UNAUTHORISED COPYING OF CODE
PROHIBITED   **********************************
*/

/*#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")*/

#include <bits/stdc++.h>
#define int long long
#define double long double
#define pb emplace_back
#define pf emplace_front
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define x first
#define y second
#define sz(x) (int)x.size()
#define endl '\n'
#define hell 998244353
#define PI 3.141592653589
#define tezz                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define MAX 1000000000000000000
using namespace std;

int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int power(int x, int n) {
  int result = 1;
  while (n > 0) {
    if (n % 2 == 1)
      result = result * x;
    x = x * x;
    n = n / 2;
  }
  return result;
}

int n, w, dp[105][100005];

signed main() {
  tezz

      memset(dp, 0, sizeof(dp));
  cin >> n >> w;
  vii mat;
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    mat.pb(a, b);
  }
  sort(all(mat));
  /*for(auto it:mat) cout<<it.x<<' '<<it.y<<endl;
      cout<<endl;*/

  dp[0][mat[0].x] = mat[0].y;

  for (int i = 1; i < n; i++) {
    for (int j = 0; j <= w; j++) {
      if (j < mat[i].x) {
        dp[i][j] = dp[i - 1][j];
      } else if (j == mat[i].x) {
        dp[i][j] = max(dp[i - 1][j], mat[i].y);
      } else if (j > mat[i].x) {
        dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - mat[i].x] + mat[i].y);
      }
    }
  }

  /*for(int i=0;i<n;i++){
      for(int j=0;j<=w;j++) cout<<dp[i][j]<<' ';
          cout<<endl;
  }*/
  int mx = 0;
  for (int i = 0; i <= w; i++)
    mx = max(mx, dp[n - 1][i]);
  cout << mx;
}