Problem p03163 (C++)

Buggy submission (status: Runtime Error):
// srinivas
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;
// using namespace __gnu_pbds;
using namespace std;

#define all(c) (c).begin(), (c).end()
#define endl "\n"
#define ff first
#define ss second
#define allr(c) (c).rbegin(), (c).rend()
#define fr(x, in, n, r) for (x = in; x < n; x += r)
#define ifr(x, n) for (x = 0; x < n; x++)
#define dfr(x, n) for (x = n - 1; x >= 0; x--)
#define pb(a) push_back(a)
#define pf(a) push_front(a)
#define pof(a) pop_front(a)
#define pob(a) pop_back(a)
#define eb(a) emplace_back(a)
#define ef(a) emplace_front(a)
typedef long long ll;
typedef map<ll, ll> mll;
typedef map<string, ll> msll;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
typedef long double ld;
#define mod 1000000007

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  freopen("input.txt", "r", stdin);
  ll n, w, i, j;
  cin >> n >> w;
  ll a[n], b[n];
  ifr(i, n) cin >> a[i] >> b[i];
  ll dp[n][w + 1];
  ifr(i, n) ifr(j, w + 1) dp[i][j] = 0;
  fr(i, a[0], w + 1, 1) dp[0][i] = b[0];
  fr(i, 1, n, 1) {
    ifr(j, w + 1) {
      if (j < a[i])
        dp[i][j] = dp[i - 1][j];
      else {
        if (j - a[i] >= 0)
          dp[i][j] = max(dp[i - 1][j - a[i]] + b[i], dp[i - 1][j]);
        else
          dp[i][j] = dp[i - 1][j];
      }
    }
  }
  // ifr(i,n){
  // 	ifr(j,w+1){
  // 		cout<<dp[i][j]<<" ";
  // 	}
  // 	cout<<endl;
  // }
  cout << dp[n - 1][w] << endl;
  return 0;
}

Fixed submission:
// srinivas
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;
// using namespace __gnu_pbds;
using namespace std;

#define all(c) (c).begin(), (c).end()
#define endl "\n"
#define ff first
#define ss second
#define allr(c) (c).rbegin(), (c).rend()
#define fr(x, in, n, r) for (x = in; x < n; x += r)
#define ifr(x, n) for (x = 0; x < n; x++)
#define dfr(x, n) for (x = n - 1; x >= 0; x--)
#define pb(a) push_back(a)
#define pf(a) push_front(a)
#define pof(a) pop_front(a)
#define pob(a) pop_back(a)
#define eb(a) emplace_back(a)
#define ef(a) emplace_front(a)
typedef long long ll;
typedef map<ll, ll> mll;
typedef map<string, ll> msll;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
typedef long double ld;
#define mod 1000000007

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  // freopen("input.txt","r",stdin);
  ll n, w, i, j;
  cin >> n >> w;
  ll a[n], b[n];
  ifr(i, n) cin >> a[i] >> b[i];
  ll dp[n][w + 1];
  ifr(i, n) ifr(j, w + 1) dp[i][j] = 0;
  fr(i, a[0], w + 1, 1) dp[0][i] = b[0];
  fr(i, 1, n, 1) {
    ifr(j, w + 1) {
      if (j < a[i])
        dp[i][j] = dp[i - 1][j];
      else {
        if (j - a[i] >= 0)
          dp[i][j] = max(dp[i - 1][j - a[i]] + b[i], dp[i - 1][j]);
        else
          dp[i][j] = dp[i - 1][j];
      }
    }
  }
  // ifr(i,n){
  // 	ifr(j,w+1){
  // 		cout<<dp[i][j]<<" ";
  // 	}
  // 	cout<<endl;
  // }
  cout << dp[n - 1][w] << endl;
  return 0;
}