Problem p03163 (C++)

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

long long N;
long long W;
std::vector<long long> w;
std::vector<long long> v;
long long SUM_W;

long long dfs(long long n, long long used_w) {
  if (n == N) {
    return 0;
  }
  long long no_used = dfs(n + 1, used_w);
  if (used_w + w[n] <= W) {
    long long used = dfs(n + 1, used_w + w[n]) + v[n];
    return max(used, no_used);
  } else {
    return no_used;
  }
}

long long dp() {
  auto table = vector<vector<long long>>(N + 1, vector<long long>(SUM_W + 1));
  int i, i1;
  for (int i_ = N - 1; i_ >= 0; i_--) {
    i = i_ % 2;
    i1 = (i + 1) % 2;
    for (int j = 0; j <= W + 1; j++) {
      if (j + w[i_] <= W) {
        table[i][j] = max(table[i1][j + w[i_]] + v[i_], table[i1][j]);
      } else {
        table[i][j] = (table[i1][j]);
      }
    }
  }

  return *max_element(table[i].begin(), table[i].end());
}

void solve(long long N_, long long W_, std::vector<long long> w_,
           std::vector<long long> v_) {
  N = N_;
  W = W_;
  w = w_;
  v = v_;
  SUM_W = accumulate(w.begin(), w.end(), 0);
  // long long res = dfs(0, 0);
  long long res = dp();
  cout << res << endl;
}

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You
// use the default template now. You can remove this line by using your custom
// template)
int main() {
  long long N;
  scanf("%lld", &N);
  long long W;
  scanf("%lld", &W);
  std::vector<long long> w(N);
  std::vector<long long> v(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &w[i]);
    scanf("%lld", &v[i]);
  }
  solve(N, W, std::move(w), std::move(v));
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

long long N;
long long W;
std::vector<long long> w;
std::vector<long long> v;
long long SUM_W;

long long dfs(long long n, long long used_w) {
  if (n == N) {
    return 0;
  }
  long long no_used = dfs(n + 1, used_w);
  if (used_w + w[n] <= W) {
    long long used = dfs(n + 1, used_w + w[n]) + v[n];
    return max(used, no_used);
  } else {
    return no_used;
  }
}

long long dp() {
  auto table = vector<vector<long long>>(N + 1, vector<long long>(W + 1));
  int i, i1;
  for (int i_ = N - 1; i_ >= 0; i_--) {
    i = i_ % 2;
    i1 = (i + 1) % 2;
    for (int j = 0; j <= W + 1; j++) {
      if (j + w[i_] <= W) {
        table[i][j] = max(table[i1][j + w[i_]] + v[i_], table[i1][j]);
      } else {
        table[i][j] = (table[i1][j]);
      }
    }
  }

  return *max_element(table[i].begin(), table[i].end());
}

void solve(long long N_, long long W_, std::vector<long long> w_,
           std::vector<long long> v_) {
  N = N_;
  W = W_;
  w = w_;
  v = v_;
  SUM_W = accumulate(w.begin(), w.end(), 0);
  // long long res = dfs(0, 0);
  long long res = dp();
  cout << res << endl;
}

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You
// use the default template now. You can remove this line by using your custom
// template)
int main() {
  long long N;
  scanf("%lld", &N);
  long long W;
  scanf("%lld", &W);
  std::vector<long long> w(N);
  std::vector<long long> v(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &w[i]);
    scanf("%lld", &v[i]);
  }
  solve(N, W, std::move(w), std::move(v));
  return 0;
}