Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

template <typename T> void _dump(const char *s, T &&head) {
  cerr << s << "=" << head << endl;
}

template <typename T, typename... Args>
void _dump(const char *s, T &&head, Args &&...tail) {
  while (*s != ',')
    cerr << *s++;
  cerr << "=" << head << ",";
  _dump(s + 1, tail...);
}

template <typename T> void _dumpv(const char *s, T &&v) {
  cerr << *s << "=";
  for (auto it = v.begin(); it != v.end(); it++) {
    cerr << *it << " ";
  }
  cerr << endl;
}

#ifdef LOCAL
#define dump(...) _dump(#__VA_ARGS__, __VA_ARGS__)
#define dumpv(...) _dumpv(#__VA_ARGS__, __VA_ARGS__)
#else
#define dump(...)
#define dumpv(...)
#endif

const ll MOD = 1000000007;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  freopen("d.3", "r", stdin);

  int N, W;
  cin >> N >> W;
  vector<vector<ll>> dp(N + 1, vector<ll>(W + 1, 0));
  vector<ll> weight(N + 1);
  vector<ll> value(N + 1);
  for (int i = 1; i <= N; i++) {
    cin >> weight[i] >> value[i];
  }
  // for (int i = 1; i <= N; i++) {
  //   for (int w = 0; w <= W; w++) {
  //     if (weight[i] <= w) {
  //       dp[i][w] = max(dp[i - 1][w - weight[i]] + value[i], dp[i - 1][w]);
  //     } else {
  //       dp[i][w] = dp[i - 1][w];
  //     }
  //   }
  // }
  for (int i = 1; i <= N; i++) {
    for (int w = W; 0 <= w; w--) {
      if (weight[i] <= w) {
        dp[0][w] = max(dp[0][w - weight[i]] + value[i], dp[0][w]);
      } else {
        // dp[0][w] = dp[i - 1][w];
      }
    }
  }
  dumpv(dp[0]);
  cout << dp[0][W] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

template <typename T> void _dump(const char *s, T &&head) {
  cerr << s << "=" << head << endl;
}

template <typename T, typename... Args>
void _dump(const char *s, T &&head, Args &&...tail) {
  while (*s != ',')
    cerr << *s++;
  cerr << "=" << head << ",";
  _dump(s + 1, tail...);
}

template <typename T> void _dumpv(const char *s, T &&v) {
  cerr << *s << "=";
  for (auto it = v.begin(); it != v.end(); it++) {
    cerr << *it << " ";
  }
  cerr << endl;
}

#ifdef LOCAL
#define dump(...) _dump(#__VA_ARGS__, __VA_ARGS__)
#define dumpv(...) _dumpv(#__VA_ARGS__, __VA_ARGS__)
#else
#define dump(...)
#define dumpv(...)
#endif

const ll MOD = 1000000007;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // freopen("d.3", "r", stdin);

  int N, W;
  cin >> N >> W;
  vector<vector<ll>> dp(N + 1, vector<ll>(W + 1, 0));
  vector<ll> weight(N + 1);
  vector<ll> value(N + 1);
  for (int i = 1; i <= N; i++) {
    cin >> weight[i] >> value[i];
  }
  // for (int i = 1; i <= N; i++) {
  //   for (int w = 0; w <= W; w++) {
  //     if (weight[i] <= w) {
  //       dp[i][w] = max(dp[i - 1][w - weight[i]] + value[i], dp[i - 1][w]);
  //     } else {
  //       dp[i][w] = dp[i - 1][w];
  //     }
  //   }
  // }
  for (int i = 1; i <= N; i++) {
    for (int w = W; 0 <= w; w--) {
      if (weight[i] <= w) {
        dp[0][w] = max(dp[0][w - weight[i]] + value[i], dp[0][w]);
      } else {
        // dp[0][w] = dp[i - 1][w];
      }
    }
  }
  dumpv(dp[0]);
  cout << dp[0][W] << endl;
  return 0;
}