Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
#include <vector>
#define REP(i, start, n) for (int i = start; i < n; ++i)
#define debug(var)                                                             \
  do {                                                                         \
    std::cout << #var << " : ";                                                \
    view(var);                                                                 \
  } while (0)
#define debug2d(var)                                                           \
  do {                                                                         \
    std::cout << #var << " : \n";                                              \
    view(var);                                                                 \
  } while (0)
template <typename T> void view(T e) { std::cout << e << std::endl; }
template <typename T> void view(const std::vector<T> &v) {
  for (const auto &e : v) {
    std::cout << e << " ";
  }
  std::cout << std::endl;
}
template <typename T> void view(const std::vector<std::vector<T>> &vv) {
  for (const auto &v : vv) {
    view(v);
  }
}

using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int N, W;
  cin >> N >> W;

  vector<P> wv(N);
  vector<vector<ll>> dp(N + 1, vector<ll>(W + 1));

  REP(i, 1, N + 1) cin >> wv[i].first >> wv[i].second;

  REP(j, 0, W + 1) { dp[0][j] = 0; }

  REP(i, 1, N + 1) REP(j, 1, W + 1) {
    if (wv[i].first <= j) {
      dp[i][j] = max(dp[i - 1][j - wv[i].first] + wv[i].second, dp[i - 1][j]);
    } else {
      dp[i][j] = dp[i - 1][j];
    }
  }

  cout << dp[N][W] << endl;

  return 0;
}

Fixed submission:
#include <iostream>
#include <string>
#include <vector>
#define REP(i, start, n) for (int i = start; i < n; ++i)
#define debug(var)                                                             \
  do {                                                                         \
    std::cout << #var << " : ";                                                \
    view(var);                                                                 \
  } while (0)
#define debug2d(var)                                                           \
  do {                                                                         \
    std::cout << #var << " : \n";                                              \
    view(var);                                                                 \
  } while (0)
template <typename T> void view(T e) { std::cout << e << std::endl; }
template <typename T> void view(const std::vector<T> &v) {
  for (const auto &e : v) {
    std::cout << e << " ";
  }
  std::cout << std::endl;
}
template <typename T> void view(const std::vector<std::vector<T>> &vv) {
  for (const auto &v : vv) {
    view(v);
  }
}

using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int N, W;
  cin >> N >> W;

  vector<P> wv(N + 1);
  vector<vector<ll>> dp(N + 1, vector<ll>(W + 1));

  REP(i, 1, N + 1) cin >> wv[i].first >> wv[i].second;

  REP(j, 0, W + 1) { dp[0][j] = 0; }

  REP(i, 1, N + 1) REP(j, 1, W + 1) {
    if (wv[i].first <= j) {
      dp[i][j] = max(dp[i - 1][j - wv[i].first] + wv[i].second, dp[i - 1][j]);
    } else {
      dp[i][j] = dp[i - 1][j];
    }
  }

  cout << dp[N][W] << endl;

  return 0;
}