Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define int long long
typedef long double ld;
#define pb push_back
#define f(i, a, b) for (int i = a; i < b; i++)
#define fd(i, a, b) for (int i = a - 1; i >= b; i--)
#define pf push_front
#define fi first
#define se second
#define INF 1e18
const int mod = 1e9 + 7;
int cnt = 0;
int dp[100 + 5][100000 + 5] = {};
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
void swap(int a, int b) {
  int temp = a;
  a = b;
  b = temp;
}
signed main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("INPUT.txt", "r", stdin);
  // for writing output to output.txt
  freopen("OUTPUT.txt", "w", stdout);
#endif

  // -------------------------------------Code starts
  // here---------------------------------------------------------------------
  int t = 1;
  // cin>>t;
  while (t--) {
    int n, w;
    cin >> n >> w;
    pair<int, int> p[n + 1];
    f(i, 1, n + 1) {
      cin >> p[i].fi;
      cin >> p[i].se;
    }

    f(i, 0, n + 1) { dp[i][0] = 0; }
    f(i, 0, w + 1) { dp[0][i] = 0; }
    f(i, 1, n + 1) {
      f(j, 1, w + 1) {
        if (j - p[i].fi >= 0)
          dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - p[i].fi] + p[i].se);
        else
          dp[i][j] = dp[i - 1][j];
      }
    }
    cout << dp[n][w] << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define int long long
typedef long double ld;
#define pb push_back
#define f(i, a, b) for (int i = a; i < b; i++)
#define fd(i, a, b) for (int i = a - 1; i >= b; i--)
#define pf push_front
#define fi first
#define se second
#define INF 1e18
const int mod = 1e9 + 7;
int cnt = 0;
int dp[100 + 5][100000 + 5] = {};
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
void swap(int a, int b) {
  int temp = a;
  a = b;
  b = temp;
}
signed main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  /*  #ifndef ONLINE_JUDGE
    // for getting input from input.txt
    freopen("INPUT.txt", "r", stdin);
    // for writing output to output.txt
    freopen("OUTPUT.txt", "w", stdout);
    #endif
    */
  // -------------------------------------Code starts
  // here---------------------------------------------------------------------
  int t = 1;
  // cin>>t;
  while (t--) {
    int n, w;
    cin >> n >> w;
    pair<int, int> p[n + 1];
    f(i, 1, n + 1) {
      cin >> p[i].fi;
      cin >> p[i].se;
    }

    f(i, 0, n + 1) { dp[i][0] = 0; }
    f(i, 0, w + 1) { dp[0][i] = 0; }
    f(i, 1, n + 1) {
      f(j, 1, w + 1) {
        if (j - p[i].fi >= 0)
          dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - p[i].fi] + p[i].se);
        else
          dp[i][j] = dp[i - 1][j];
      }
    }
    cout << dp[n][w] << endl;
  }
}