Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using VI = vector<LL>;
using VVI = vector<VI>;
using VB = vector<bool>;
using VS = vector<string>;
using PII = pair<LL, LL>;
using VP = vector<PII>;
#define PB push_back
#define MP make_pair
#define SZ(a) LL((a).size())
#define EACH(x, c) for (auto x : (c))
#define ALL(c) (c).begin(), (c).end()
#define REVERSE(c) reverse(ALL(c))
#define SORT(c) stable_sort(ALL(c))
#define RSORT(c) stable_sort((c).rbegin(), (c).rend())
#define FSORT(c)                                                               \
  stable_sort(ALL(c), [](auto &x, auto &y) { return x.first < y.first; });
#define FRSORT(c)                                                              \
  stable_sort(ALL(c), [](auto &x, auto &y) { return x.first > y.first; });
#define SSORT(c)                                                               \
  stable_sort(ALL(c), [](auto &x, auto &y) { return x.second < y.second; });
#define SRSORT(c)                                                              \
  stable_sort(ALL(c), [](auto &x, auto &y) { return x.second > y.second; });
#define FOR(i, a, b) for (LL i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define $(x)                                                                   \
  { cout << #x << " = " << (x) << endl; }

int main() {
  LL N, W;
  cin >> N >> W;
  VI w(N + 1), v(N + 1);
  LL V = 0;
  FOR(i, 1, N + 1) {
    cin >> w[i] >> v[i];
    V += v[i];
  }

  VVI dp(N + 1, VI(V + 1, W + 1));
  dp[0][0] = 0;
  REP(i, N) {
    REP(j, V + 1) {
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
      dp[i + 1][j + v[i + 1]] =
          min(dp[i + 1][j + v[i + 1]], dp[i][j] + w[i + 1]);
    }
  }

  /*
  REP(i, N + 1) {
      REP(j, V + 1) {
          if (dp[i][j] == LLONG_MAX) {
              cout << "- ";
          } else {
              cout << dp[i][j] << " ";
          }
      }
      cout << endl;
  }
  */

  for (LL j = V; j >= 0; --j) {
    LL min_w = LLONG_MAX;
    REP(i, N + 1) min_w = min(min_w, dp[i][j]);
    if (min_w <= W) {
      cout << j << endl;
      break;
    }
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using VI = vector<LL>;
using VVI = vector<VI>;
using VB = vector<bool>;
using VS = vector<string>;
using PII = pair<LL, LL>;
using VP = vector<PII>;
#define PB push_back
#define MP make_pair
#define SZ(a) LL((a).size())
#define EACH(x, c) for (auto x : (c))
#define ALL(c) (c).begin(), (c).end()
#define REVERSE(c) reverse(ALL(c))
#define SORT(c) stable_sort(ALL(c))
#define RSORT(c) stable_sort((c).rbegin(), (c).rend())
#define FSORT(c)                                                               \
  stable_sort(ALL(c), [](auto &x, auto &y) { return x.first < y.first; });
#define FRSORT(c)                                                              \
  stable_sort(ALL(c), [](auto &x, auto &y) { return x.first > y.first; });
#define SSORT(c)                                                               \
  stable_sort(ALL(c), [](auto &x, auto &y) { return x.second < y.second; });
#define SRSORT(c)                                                              \
  stable_sort(ALL(c), [](auto &x, auto &y) { return x.second > y.second; });
#define FOR(i, a, b) for (LL i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define $(x)                                                                   \
  { cout << #x << " = " << (x) << endl; }

int main() {
  LL N, W;
  cin >> N >> W;
  VI w(N + 1), v(N + 1);
  LL V = 0;
  FOR(i, 1, N + 1) {
    cin >> w[i] >> v[i];
    V += v[i];
  }

  VVI dp(N + 1, VI(V + 1, W + 1));
  dp[0][0] = 0;
  REP(i, N) {
    REP(j, V + 1) {
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
      if (j + v[i + 1] <= V) {
        dp[i + 1][j + v[i + 1]] =
            min(dp[i + 1][j + v[i + 1]], dp[i][j] + w[i + 1]);
      }
    }
  }

  /*
  REP(i, N + 1) {
      REP(j, V + 1) {
          if (dp[i][j] == LLONG_MAX) {
              cout << "- ";
          } else {
              cout << dp[i][j] << " ";
          }
      }
      cout << endl;
  }
  */

  for (LL j = V; j >= 0; --j) {
    LL min_w = LLONG_MAX;
    REP(i, N + 1) min_w = min(min_w, dp[i][j]);
    if (min_w <= W) {
      cout << j << endl;
      break;
    }
  }

  return 0;
}