Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define llt long long int
#define pb push_back
#define pii pair<int, int>
#define mk make_pair
#define ff first
#define ss second
#define mod 1000000007
int dp[101][100001];
bool cmp(pii a, pii b) {
  if (a.ss < b.ss)
    return false;
  else if (a.ss == b.ss) {
    if (a.ff < b.ff)
      return false;
  }
  return true;
}
int32_t main() {
  int n, w, x, y;
  cin >> n >> w;
  vector<pii> v;
  for (int i = 0; i < n; i++) {
    cin >> x >> y;
    v.pb(mk(x, y));
  }
  sort(v.begin(), v.end(), cmp);
  /*	for(int i=0;i<n;i++){
              cout<<v[i].ff<<" "<<v[i].ss<<endl;
          }*/
  memset(dp, 0, sizeof dp);
  int mx = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < 100001; j++) {
      if (v[i - 1].ss > j) {
        dp[i][j] = dp[i - 1][j];
      } else if (v[i - 1].ss == j) {
        if (dp[i - 1][j] == 0)
          dp[i][j] = v[i - 1].ff;
        else
          dp[i][j] =
              min(dp[i - 1][j - v[i - 1].ss] + v[i - 1].ff, dp[i - 1][j]);
      } else {
        if (dp[i - 1][j - v[i - 1].ss] != 0) {
          if (dp[i - 1][j] == 0)
            dp[i][j] = dp[i - 1][j - v[i - 1].ss] + v[i - 1].ff;
          else
            dp[i][j] =
                min(dp[i - 1][j - v[i - 1].ss] + v[i - 1].ff, dp[i - 1][j]);
        } else {
          dp[i][j] = dp[i - 1][j];
        }
      }
      if (dp[i][j] <= w && dp[i][j] != 0)
        mx = max(mx, j);
    }
  }
  cout << mx;
  /*for(int i=0;i<=n;i++){
      for(int j=0;j<40;j++){
          cout<<dp[i][j]<<" ";
      }
      cout<<endl;
  }*/
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define pb push_back
#define pii pair<int, int>
#define mk make_pair
#define ff first
#define ss second
#define mod 1000000007
int dp[101][100001];
bool cmp(pii a, pii b) {
  if (a.ss < b.ss)
    return false;
  else if (a.ss == b.ss) {
    if (a.ff < b.ff)
      return false;
  }
  return true;
}
int32_t main() {
  int n, w, x, y;
  cin >> n >> w;
  vector<pii> v;
  for (int i = 0; i < n; i++) {
    cin >> x >> y;
    v.pb(mk(x, y));
  }
  sort(v.begin(), v.end(), cmp);
  /*	for(int i=0;i<n;i++){
              cout<<v[i].ff<<" "<<v[i].ss<<endl;
          }*/
  memset(dp, 0, sizeof dp);
  int mx = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < 100001; j++) {
      if (v[i - 1].ss > j) {
        dp[i][j] = dp[i - 1][j];
      } else if (v[i - 1].ss == j) {
        if (dp[i - 1][j] == 0)
          dp[i][j] = v[i - 1].ff;
        else
          dp[i][j] =
              min(dp[i - 1][j - v[i - 1].ss] + v[i - 1].ff, dp[i - 1][j]);
      } else {
        if (dp[i - 1][j - v[i - 1].ss] != 0) {
          if (dp[i - 1][j] == 0)
            dp[i][j] = dp[i - 1][j - v[i - 1].ss] + v[i - 1].ff;
          else
            dp[i][j] =
                min(dp[i - 1][j - v[i - 1].ss] + v[i - 1].ff, dp[i - 1][j]);
        } else {
          dp[i][j] = dp[i - 1][j];
        }
      }
      if (dp[i][j] <= w && dp[i][j] != 0)
        mx = max(mx, j);
    }
  }
  cout << mx;
  /*for(int i=0;i<=n;i++){
      for(int j=0;j<40;j++){
          cout<<dp[i][j]<<" ";
      }
      cout<<endl;
  }*/
  return 0;
}