Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define flash                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define rep(i, begin, end)                                                     \
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define all(v) v.begin(), v.end()
#define f first
#define s second

using namespace std;
const ll V = 1e5;

ll dp[105][V + 5];
std::vector<int> wt(105);
std::vector<int> v(105);
int main() {
  flash ll n, w;
  cin >> n >> w;

  for (int i = 1; i <= n; i++) {

    cin >> wt[i] >> v[i];
  }

  // std::vector<std::vector<int>> dp( n+1 , std::vector<int>(w+1,0));

  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= V; j++) {
      dp[i][j] = -1;
    }
  }

  for (int i = 0; i <= n; i++)
    dp[i][0] = 0;

  for (int i = 1; i <= n; i++) {

    for (int j = 1; j <= V; j++) {

      if (v[i - 1] > j)
        dp[i][j] = dp[i - 1][j];
      else {
        ll w1 = dp[i - 1][j];
        ll w2 = dp[i - 1][j - v[i]];
        if (w1 == -1 && w2 == -1) {
          dp[i][j] = -1;
        } else if (w1 == -1) {
          dp[i][j] = w2 + wt[i];
        } else if (w2 == -1)
          dp[i][j] = w1;
        else
          dp[i][j] = min(w2 + wt[i], w1);
      }
    }
  }
  ll result;
  for (int i = 0; i <= V; i++) {
    if (dp[n][i] == -1)
      continue;
    else if (dp[n][i] <= w) {
      // cout<<dp[n][i]<<endl;
      result = i;
    }
  }
  cout << result << "\n";
}


Fixed submission:
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define flash                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define rep(i, begin, end)                                                     \
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define all(v) v.begin(), v.end()
#define f first
#define s second

using namespace std;
const ll V = 1e5;

ll dp[105][V + 5];
std::vector<int> wt(105);
std::vector<int> v(105);
int main() {
  flash ll n, w;
  cin >> n >> w;

  for (int i = 1; i <= n; i++) {

    cin >> wt[i] >> v[i];
  }

  // std::vector<std::vector<int>> dp( n+1 , std::vector<int>(w+1,0));

  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= V; j++) {
      dp[i][j] = -1;
    }
  }

  for (int i = 0; i <= n; i++)
    dp[i][0] = 0;

  for (int i = 1; i <= n; i++) {

    for (int j = 1; j <= V; j++) {

      if (v[i] > j)
        dp[i][j] = dp[i - 1][j];
      else {
        ll w1 = dp[i - 1][j];
        ll w2 = dp[i - 1][j - v[i]];
        if (w1 == -1 && w2 == -1) {
          dp[i][j] = -1;
        } else if (w1 == -1) {
          dp[i][j] = w2 + wt[i];
        } else if (w2 == -1)
          dp[i][j] = w1;
        else
          dp[i][j] = min(w2 + wt[i], w1);
      }
    }
  }
  ll result;
  for (int i = 0; i <= V; i++) {
    if (dp[n][i] == -1)
      continue;
    else if (dp[n][i] <= w) {
      // cout<<dp[n][i]<<endl;
      result = i;
    }
  }
  cout << result << "\n";
}