Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define FOR(ii, ss, ee) for (ll ii = (ss); ii <= (ll)(ee); ++ii)
#define DEC(ii, ss, ee) for (ll ii = (ss); ii >= (ll)(ee); --ii)
#define VALS(args...)                                                          \
  {                                                                            \
    string _s = #args;                                                         \
    replace(_s.begin(), _s.end(), ',', ' ');                                   \
    stringstream _ss(_s);                                                      \
    istream_iterator<string> _it(_ss);                                         \
    err(_it, args);                                                            \
  }
#define IAMSPEED                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);
#define pb push_back
#define pf push_front
#define INF 1234567890ll
#define EPS (1e-7)
#define PI (acos((ld)-1.0))
#define ll long long int
#define ld long double
#define all(x) (x).begin(), (x).end()
#define f first
#define s second
typedef pair<int, int> pi;
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << '\n';
  err(++it, args...);
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
string lts(ll x) {
  stringstream s;
  s << x;
  return s.str();
}
ll stl(string x) {
  stringstream s(x);
  ll temp;
  s >> temp;
  return temp;
}
const int MOD = 1e9 + 7;

ll val[505], weight[505], dp[505];

int main() {
  IAMSPEED
  ll n, w;
  cin >> n >> w;
  for (ll i = 0; i < n; i++) {
    cin >> weight[i] >> val[i];
  }
  for (ll i = 0; i < n; i++) {
    for (ll j = w; j >= weight[i]; j--) {
      dp[j] = max(dp[j], val[i] + dp[j - weight[i]]);
    }
  }
  cout << dp[w];
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(ii, ss, ee) for (ll ii = (ss); ii <= (ll)(ee); ++ii)
#define DEC(ii, ss, ee) for (ll ii = (ss); ii >= (ll)(ee); --ii)
#define VALS(args...)                                                          \
  {                                                                            \
    string _s = #args;                                                         \
    replace(_s.begin(), _s.end(), ',', ' ');                                   \
    stringstream _ss(_s);                                                      \
    istream_iterator<string> _it(_ss);                                         \
    err(_it, args);                                                            \
  }
#define IAMSPEED                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);
#define pb push_back
#define pf push_front
#define INF 1234567890ll
#define EPS (1e-7)
#define PI (acos((ld)-1.0))
#define ll long long int
#define ld long double
#define all(x) (x).begin(), (x).end()
#define f first
#define s second
typedef pair<int, int> pi;
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << '\n';
  err(++it, args...);
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
string lts(ll x) {
  stringstream s;
  s << x;
  return s.str();
}
ll stl(string x) {
  stringstream s(x);
  ll temp;
  s >> temp;
  return temp;
}
const int MOD = 1e9 + 7;

ll val[505], weight[505], dp[100005];

int main() {
  IAMSPEED
  ll n, w;
  cin >> n >> w;
  for (ll i = 0; i < n; i++) {
    cin >> weight[i] >> val[i];
  }
  for (ll i = 0; i < n; i++) {
    for (ll j = w; j >= weight[i]; j--) {
      dp[j] = max(dp[j], val[i] + dp[j - weight[i]]);
    }
  }
  cout << dp[w];
}